2014-07-21 29 views
4

我即將學習Java。我試圖用一個GridBagLayout爲一個簡單的計算器編寫一個GUI。GridBagLayout不接受我的rowspan命令(gridtheight)

問題是我的「+」按鈕沒有超過2行高。我不知道爲什麼?我試圖用一次做最後一排:

c.gridwidth = 1; 
    c.gridy = 4; 
    c.gridx = 3; 
    c.gridheight = 2; 
    c.ipady = 26; 
    subpanel.add(bplus, c); 

和2最後一排一次英寸

c.gridwidth = 1; 
    c.gridy = 3; 
    c.gridx = 3; 
    c.gridheight = 2; 
    c.ipady = 26; 
    subpanel.add(bplus, c); 

這裏是GUI的孔代碼,如果你想測試它,也許你不得不調整爲主。

package gui; 

import classes.Rechner; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

/** 
* Created by tq67 on 17.07.2014. 
*/ 
public class CalculatorGUI extends JFrame { 

public static void main(String[] args) { 

    CalculatorGUI gui = new CalculatorGUI(); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gui.setVisible(true); 


} 

    public CalculatorGUI() { 
     setTitle("Calculator"); 
     setBounds(300, 300, 220, 225); 

     JPanel subpanel = new JPanel(); 
     JPanel headpanel = new JPanel(); 
     final JTextField input = new JTextField(); 

     input.addKeyListener(new KeyListener() { 
      @Override 
      public void keyTyped(KeyEvent e) { 

      } 

      @Override 
      public void keyPressed(KeyEvent e) { 

      } 

      @Override 
      public void keyReleased(KeyEvent e) { 
       if (10 == e.getKeyCode()) { 
        Rechner rechner = new Rechner(input.getText()); 
        input.setText(rechner.rechnen().toPlainString()); 
       } 
       if (27 == e.getKeyCode()) { 
        input.setText(""); 
       } 
      } 
     }); 
     ActionListener buttonListener = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       input.setText(input.getText() + e.getActionCommand()); 
      } 
     }; 
     ActionListener resultListener = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (e.getActionCommand().equals("=")) { 
        Rechner rechner = new Rechner(input.getText()); 
        input.setText(rechner.rechnen().toPlainString()); 
       } else { 
        input.setText(""); 
       } 

      } 

      private void calc() { 
       Rechner rechner = new Rechner(input.getText()); 
       input.setText(rechner.rechnen().toPlainString()); 
      } 
     }; 

     JButton b1 = new JButton("1"); 
     JButton b2 = new JButton("2"); 
     JButton b3 = new JButton("3"); 
     JButton b4 = new JButton("4"); 
     JButton b5 = new JButton("5"); 
     JButton b6 = new JButton("6"); 
     JButton b7 = new JButton("7"); 
     JButton b8 = new JButton("8"); 
     JButton b9 = new JButton("9"); 
     JButton b0 = new JButton("0"); 

     JButton bdot = new JButton("."); 
     JButton bplus = new JButton("+"); 
     JButton bminus = new JButton("-"); 
     JButton bdiv = new JButton("/"); 
     JButton bmult = new JButton("*"); 
     JButton bresult = new JButton("="); 

     JButton bcancel = new JButton("C"); 
     b1.addActionListener(buttonListener); 
     b2.addActionListener(buttonListener); 
     b3.addActionListener(buttonListener); 
     b4.addActionListener(buttonListener); 
     b5.addActionListener(buttonListener); 
     b6.addActionListener(buttonListener); 
     b7.addActionListener(buttonListener); 
     b8.addActionListener(buttonListener); 
     b9.addActionListener(buttonListener); 
     b0.addActionListener(buttonListener); 
     bdot.addActionListener(buttonListener); 
     bplus.addActionListener(buttonListener); 
     bminus.addActionListener(buttonListener); 
     bdiv.addActionListener(buttonListener); 
     bmult.addActionListener(buttonListener); 

     bresult.addActionListener(resultListener); 
     bcancel.addActionListener(resultListener); 

     headpanel.setLayout(new GridBagLayout()); 
     subpanel.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 




     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 1; 
     c.weighty = 3; 
     c.gridx = 0; 
     c.gridy = 0; 
     c.ipady = 10; 
     headpanel.add(input, c); 

     setLayout(new BorderLayout()); 
     add(headpanel, BorderLayout.NORTH); 
     add(subpanel, BorderLayout.SOUTH); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 0; 
     c.gridx = 0; 
     c.gridy = 0; 
     subpanel.add(b7, c); 
     c.gridx++; 
     subpanel.add(b8, c); 
     c.gridx++; 
     subpanel.add(b9, c); 
     c.gridx++; 
     subpanel.add(bmult, c); 

     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 1; 
     subpanel.add(b4, c); 
     c.gridx++; 
     subpanel.add(b5, c); 
     c.gridx++; 
     subpanel.add(b6, c); 
     c.gridx++; 
     subpanel.add(bdiv, c); 


     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 2; 
     subpanel.add(b1, c); 
     c.gridx++; 
     subpanel.add(b2, c); 
     c.gridx++; 
     subpanel.add(b3, c); 
     c.gridx++; 
     subpanel.add(bminus, c); 


     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridheight = 1; 
     c.ipady = 0; 
     c.gridx = 0; 
     c.gridy = 3; 
     subpanel.add(bdot, c); 
     c.gridwidth = 2; 
     c.gridx++; 
     subpanel.add(b0, c); 
     c.gridwidth = 1; 
     c.gridx = 3; 
     c.gridheight = 2; 
     c.ipady = 26; 
     subpanel.add(bplus, c); 


     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridwidth = 1; 
     c.ipady = 0; 
     c.gridx = 0; 
     c.gridy = 4; 
     subpanel.add(bcancel, c); 
     c.gridx = 1; 
     c.gridwidth = 2; 
     subpanel.add(bresult, c); 

    } 
} 

我讀到這裏的#1,當你烏斯的gridx是更大然後0你將有gridheight問題的錯誤...

此刻的GUI看起來像這樣:

calc.png

Thx for your help。

回答

4

兩件事情,第一,重置gridheight你用過之後

c.gridwidth = 1; 
c.gridx = 3; 
c.gridheight = 2; 
c.ipady = 26; 
subpanel.add(bplus, c); 

c.fill = GridBagConstraints.HORIZONTAL; 
c.gridwidth = 1; 
c.ipady = 0; 
c.gridx = 0; 
c.gridy = 4; 
// Still using the gridheight of 2... 
subpanel.add(bcancel, c); 

應該更像......

c.fill = GridBagConstraints.HORIZONTAL; 
c.gridwidth = 1; 
c.ipady = 0; 
c.gridx = 0; 
c.gridy = 4; 
c.gridheight = 1; 
subpanel.add(bcancel, c); 

我也想擺脫c.ipady = 26;,但這就是我;)

第二,利用BOTH填充約束...

c.fill = GridBagConstraints.BOTH; 
c.gridwidth = 1; 
c.gridx = 3; 
c.gridheight = 2; 
c.ipady = 26; 
subpanel.add(bplus, c); 

Calculator

也...

public void keyReleased(KeyEvent e) { 
    if (10 == e.getKeyCode()) { 
     Rechner rechner = new Rechner(input.getText()); 
     input.setText(rechner.rechnen().toPlainString()); 
    } 
    if (27 == e.getKeyCode()) { 
     input.setText(""); 
    } 
} 

keyCode並不代表ASCII字符,而是代表了虛擬鍵碼。你應該使用KeyEvent.VK_ENTER(我認爲)和KeyEvent.VK_ESCAPE。請注意,一個JTextField已經有能力處理輸入ActionListener接口。在任何情況下,你應該考慮使用Key Bindings API如果可能的話

+0

THX很多!我的TF不會接受輸入代碼:(如果我不實現像這樣......但我會嘗試用KeyEvent的...您的解決方案,是KeyEvent的 – Synoon

+1

它將解決方案的優勢,你需要添加['ActionListener'到外地(http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html)(和[如何使用文本字段(http://docs.oracle .COM/JavaSE的/教程/ uiswing /組件/ textfield.html)。使用'KeyEvent.VK_ *'方法的優點是它是如何想使用,實際的硬件按鍵行程是由Java的轉換爲虛擬鍵表示 – MadProgrammer

+0

我也擺脫了c.ipady表達是非常不現實..的存在有一個簡單的方法來做到這一點沒有? – Synoon