使用JPanel制作计算器:如何使用布局?

时间:2018-04-10 20:35:07

标签: java swing jpanel layout-manager

我正在处理一个问题,该问题只是说明了一个看起来像这个计算器的GUI,它不需要运行,只是看起来像它。所以我认为我有JPanelJButton组件,但我无法组织字段以使其正确显示。我很新,所以任何有关如何组织GUI的速成课程都将受到赞赏。

这是我到目前为止所拥有的:

// Using a JPanel to help lay out components.
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class Calculator extends JFrame 
{
private final JPanel buttonJPanel2, buttonJPanel3, buttonJPanel4, 
buttonJPanel5; // panel to hold buttons
private final JButton[] buttons3, buttons4, buttons5; 
private final JButton[] buttons2;
private final JTextField buttonJPanel1;

// no-argument constructor
public Calculator()
{
  super("Calculator");
  buttonJPanel1 = new JTextField();

  add(buttonJPanel1, BorderLayout.NORTH); // add panel1 to JFrame

  buttons2 = new JButton[4];
  buttons2[0] = new JButton("7");
  buttons2[1] = new JButton("8");
  buttons2[2] = new JButton("9");
  buttons2[3] = new JButton("/");
  buttonJPanel2 = new JPanel(); 
  buttonJPanel2.setLayout(new GridLayout(1, buttons2.length));

  add(buttonJPanel2, BorderLayout.AFTER_LAST_LINE); // add panel2 to JFrame

  buttons3 = new JButton[4];
  buttons3[0] = new JButton("4");
  buttons3[1] = new JButton("5");
  buttons3[2] = new JButton("6");
  buttons3[3] = new JButton("*");
  buttonJPanel3 = new JPanel(); 
  buttonJPanel3.setLayout(new GridLayout(1, buttons3.length));

  add(buttonJPanel3, BorderLayout.AFTER_LAST_LINE); // add panel3 to JFrame

  buttons4 = new JButton[4];
  buttons4[0] = new JButton("1");
  buttons4[1] = new JButton("2");
  buttons4[2] = new JButton("3");
  buttons4[3] = new JButton("-");
  buttonJPanel4 = new JPanel(); 
  buttonJPanel4.setLayout(new GridLayout(1, buttons4.length));

  add(buttonJPanel4, BorderLayout.AFTER_LAST_LINE); // add panel4 to JFrame

  buttons5 = new JButton[4];
  buttons2[0] = new JButton("0");
  buttons5[1] = new JButton(".");
  buttons5[2] = new JButton("=");
  buttons5[3] = new JButton("+");
  buttonJPanel5 = new JPanel(); 
      buttonJPanel5.setLayout(new GridLayout(1, buttons5.length));

      add(buttonJPanel5, BorderLayout.AFTER_LAST_LINE); // add panel5 to 
//JFrame
   } 


public static void main(String[] args)
      { 
         PanelFrame panelFrame = new PanelFrame(); 
         panelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         panelFrame.setSize(700, 500); 
         panelFrame.setVisible(true); 
      } 

} // end class PanelFrame

2 个答案:

答案 0 :(得分:0)

简而言之:每个组件都必须声明,

JButton button1;

初​​始化

button1 = new JButton("Click me!");

并添加到层次结构中的组件上方(在本例中为面板)

panel1.add(button1);

如果您没有将组件添加到面板和面板到框架,它们将不会成为GUI的一部分,因此不可见。

可以设置JPanel以使用LayoutManager以不同方式调整其布局,就像使用GridLayout(这似乎适合计算器)一样。我建议你阅读how to use the grid layout here

希望我能提供帮助:)

答案 1 :(得分:0)

  1. 您需要将按钮添加到“JPanel”。例如:

    for(JButton b : buttons2) { buttonJPanel2.add(b); } 
    
  2. BorderLayout在每个位置接受一个组件,因此如果您将BorderLayout.AFTER_LAST_LINE设置为两次,则最后一次添加会覆盖前一个。