使用JPanel制作计算器:如何使用布局? [英] Using JPanel to make a calculator: how to use layout?
问题描述
我正在研究一个简单的问题,要求陈述一个看起来像这个计算器的GUI,它不必运行就可以了.因此,我认为我正确地使用了JPanel
和JButton
组件,但是我无法组织字段使其正确显示.我很新,所以任何有关如何组织GUI的速成课程将不胜感激.
I'm working on a question that simply states to make an GUI that looks like This calculator, it doesn't have to function, just look like it. So I think I have the JPanel
and JButton
components right but I can't organize the fields to make it come out right. I'm pretty new so any crash course on how to organize a GUI would be appreciated.
这是我到目前为止所拥有的:
Here's what I have so far:
// 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
推荐答案
总之:每个组件都必须声明,
In short: every component has to be declared,
JButton button1;
已初始化
button1 = new JButton("Click me!");
并添加到层次结构中位于其上方的组件(在本例中为面板)
and added to the component above it in the hierarchy (in this case the panel)
panel1.add(button1);
如果您不将组件添加到面板中,也不将面板添加到框架中,则它们将不会成为GUI的一部分,因此不可见.
If you do not add the components to the panel and the panel to the frame they will not be part of the GUI, thus not visible.
可以使用 LayoutManager ,就像您对GridLayout所做的一样(它似乎适合计算器).我建议您阅读如何在此处使用网格布局
A JPanel can be set to adjust its layout in different ways using a LayoutManager as you have done with GridLayout (which seems fitting for a calculator). I suggest you read about how to use the grid layout here.
希望我能帮助您:)
这篇关于使用JPanel制作计算器:如何使用布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!