質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

UI

UIはUser Interfaceの略であり、人間がコンピュータとやりとりをするためのシステムです。

Q&A

解決済

1回答

965閲覧

GridBagLayout が正しい比率で分割してくれない

skytomo

総合スコア35

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

UI

UIはUser Interfaceの略であり、人間がコンピュータとやりとりをするためのシステムです。

0グッド

1クリップ

投稿2019/11/09 11:40

編集2019/11/09 12:00

前提・実現したいこと

Java で電卓を作ろうとしています。
GridBagLayout が正しい比率でコンポーネントを表示してくれることを実現したいです。

発生している問題

イメージ説明
私は左のJPanelと右のJTextPaneの幅の比率が 2:1 になるように設定しました。
しかし、上の図は明らかに 2:1 になっていません。
つまり、 GridBagLayout は正しい比率でパネルを分割してくれません。

該当のソースコード

java

1 2class CalculatorForm extends JFrame { 3 private List<JButton> buttons; 4 private List<JButton> functionButtons; 5 6 GridBagLayout gbl = new GridBagLayout(); 7 JTabbedPane tabbedPane = new JTabbedPane(); 8 JPanel buttonPanel = new JPanel(); 9 JPanel functionButtonPanel = new JPanel(); 10 JTextPane inputTextPane = new JTextPane(); 11 JTextPane logTextPane = new JTextPane(); 12 13 CalculatorForm() { 14 super("FrameTest"); 15 setSize(600, 400); 16 setLayout(gbl); 17 18 // >>>>>>>>>>>>>>> ここで幅や高さの比率を設定している 19 addComportnent(inputTextPane, 0, 0, 2, 1, 2, 1); 20 addComportnent(tabbedPane, 0, 1, 2, 2, 2, 2); 21 addComportnent(logTextPane, 2, 0, 1, 3, 1, 3); 22 // <<<<<<<<<<<<<<< 23 24 tabbedPane.add("標準", buttonPanel); 25 tabbedPane.add("関数", functionButtonPanel); 26 27 buttons = Arrays.asList(new JButton("AC"), new JButton("C"), new JButton("✗"), new JButton("÷"), 28 new JButton("7"), new JButton("8"), new JButton("9"), new JButton("×"), new JButton("4"), 29 new JButton("5"), new JButton("6"), new JButton("-"), new JButton("1"), new JButton("2"), 30 new JButton("3"), new JButton("+"), new JButton("+/-"), new JButton("0"), new JButton("."), 31 new JButton("=")); 32 functionButtons = Arrays.asList(new JButton("or"), new JButton("xor"), new JButton("not"), new JButton("and"), 33 new JButton("mod"), new JButton("%"), new JButton("2nd"), new JButton("x^2"), new JButton("x^3"), 34 new JButton("x^y"), new JButton("e^x"), new JButton("10^x"), new JButton("1/x"), new JButton("√x"), 35 new JButton("3√x"), new JButton("y√x"), new JButton("ln"), new JButton("log10"), new JButton("x!"), 36 new JButton("sin"), new JButton("cos"), new JButton("tan"), new JButton("e"), new JButton("π"), 37 new JButton("rand"), new JButton("sinh"), new JButton("cosh"), new JButton("tanh"), new JButton("("), 38 new JButton(")")); 39 40 for (JButton jButton : buttons) { 41 buttonPanel.add(jButton); 42 } 43 buttonPanel.setLayout(new GridLayout(5, 4)); 44 for (JButton jButton : functionButtons) { 45 functionButtonPanel.add(jButton); 46 47 } 48 functionButtonPanel.setLayout(new GridLayout(5, 6)); 49 } 50 51 void addComportnent(Component c, int x, int y, int w, int h, double wx, double wy) { 52 GridBagConstraints gbc = new GridBagConstraints(); 53 gbc.fill = GridBagConstraints.BOTH; 54 gbc.gridx = x; 55 gbc.gridy = y; 56 gbc.gridwidth = w; 57 gbc.gridheight = h; 58 gbc.weightx = wx; 59 gbc.weighty = wy; 60 gbl.setConstraints(c, gbc); 61 add(c); 62 } 63}

幅や高さの比率を設定しているところをもう少し細かく図に示したものを以下に書きます。

java

1addComportnent(inputTextPane, 0, 0, 2, 1, 2, 1); 2/* 開始点:(0, 0) 3 * サイズ:2 × 1 4 * ■■□ 5 * □□□ 6 * □□□ 7 */ 8addComportnent(tabbedPane, 0, 1, 2, 2, 2, 2); 9/* 開始点:(0, 1) 10 * サイズ:2 × 2 11 * □□□ 12 * ■■□ 13 * ■■□ 14 */ 15addComportnent(logTextPane, 2, 0, 1, 3, 1, 3); 16/* 開始点:(2, 0) 17 * サイズ:1 × 3 18 * □□■ 19 * □□■ 20 * □□■ 21 */ 22

補足情報

JDK 11.0.5
Windows 10

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

うろ覚えですいません.
GridBagLayout は, 各コンポーネントの位置関係をグリッドを占める範囲によって指定するもので, 大きさの比率を指定するものでは無かったように思います.
weightx/y は, コンテナの大きさが変化したときに, その変化分を各コンポーネントに配分する比率では無かったでしょうか.

投稿2019/11/09 19:24

jimbe

総合スコア12543

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

skytomo

2019/11/10 11:48

言われた通り、 GridBagLayout は均等に分けるものと書かれていませんでした。そこで、GridBagLayout を諦め、 GridLayout を用いたらうまくいきました。 ```java //setLayout(gbl); ``` setLayout(gbl); をコメントアウトし、 ```java // addComportnent(inputTextPane, 0, 0, 1, 1, 1, 1); // addComportnent(tabbedPane, 0, 1, 1, 2, 1, 1); // addComportnent(logTextPane, 1, 0, 1, 3, 1, 1); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); p1.setLayout(new GridLayout(1, 2, 5, 0)); p2.setLayout(new GridLayout(2, 1, 5, 0)); p1.add(p2); p1.add(logTextPane); p2.add(inputTextPane); p2.add(tabbedPane); getContentPane().add(p1, BorderLayout.CENTER); ``` のようにすればきれいに配置されました。 諦めも肝心ですね。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問