calculator
をjava
にしようとしています。私はLayout Managers
に新しいです。 GridBagLayout
でJFrame
オブジェクトを作成しました。私は2 JPanel
を作成しました。一つは数字用、もう一つは操作用です。どちらもJPanel
GridLayout
を使用してください。JPanelは余分な領域を占めています
これは、JFrame
のように配置されています。
window=new JFrame("Calculator");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridBagLayout());
//Creating window
GridBagConstraints gbc=new GridBagConstraints();
btns=new JPanel(); //JPanel for numbers
operations=new JPanel(); //JPanel for operations
numdisp=new JTextField(15); //JTextFIeld which displays the number
gbc.gridx=1;
gbc.gridy=1;
window.add(numdisp,gbc);
addbtn(); //method which defines all the variables
gbc.gridx=1;
gbc.gridy=2;
window.add(btns,gbc);
gbc.gridx=2;
gbc.gridy=2;
window.add(operations,gbc);
btns.setLayout(new GridLayout(4,3,0,0)); //Layout for both JPanel's
operations.setLayout(new GridLayout(7,1,1,1));
window.pack();
window.setVisible(true);
しかし、私のnumber
パネルとnumber display
との間に大きなギャップが存在する理由は、これが結果は
...どのように見えるか、私は理解していないされています。なぜ問題が発生していますか?どのように解決することができますか?
1)より良いヘルプをもっと早く得るには、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 2)この[計算例](http://stackoverflow.com/a/7441804/418556)も参照してください。 'ScriptEngine'を使ってテキストフィールドの式を評価します。 3)['GridBagConstraints.fill'](http://docs.oracle.com/javase/8/docs/api/java/awt/GridBagConstraints.html#fill)も参照してください。 –