本学期我正在上Java课程并正在进行一项带回家的测验。 在其中我们创建了一个类似的计算器,它应该看起来像第一个附加的图像
如您所见,基本面板应分为2行1列。 上面板分为3行和2列。 下面板有4个按钮用于执行计算。
对于这个测验,我创建了2个文件,WholePanel.java和LayoutPanel.java。 以下是WholePanel.java的代码:
package layoutpanel;
import java.awt.BorderLayout;
import static java.awt.BorderLayout.CENTER;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author ross satchell
*/
public class WholePanel extends JPanel{
public JTextField textBox1, textBox2, resultBox;
public double num1, num2, result;
public JLabel label1, label2, label3;
public JButton addButton, subButton, multipButton, dividButton;
public WholePanel(){ // constructor
JPanel upperPanel, lowerPanel, subPanel;
upperPanel = new JPanel(); // the panel for the top section
lowerPanel = new JPanel(); // the panel for the lower section
subPanel = new JPanel(); // panel to hold both upperPanel and lowerPanel
label1 = new JLabel("Enter number 1:");
label2 = new JLabel("Enter number 2:");
label3 = new JLabel("Result:");
textBox1 = new JTextField(10);
textBox2 = new JTextField(10);
resultBox = new JTextField(10);
addButton =new JButton("+");
subButton = new JButton("-");
multipButton = new JButton("*");
dividButton = new JButton("/");
upperPanel.setLayout(new GridLayout(3,2));
upperPanel.add(label1);
upperPanel.add(textBox1); // set up layout and
upperPanel.add(label2); // components for upperPanel
upperPanel.add(textBox2);
upperPanel.add(label3);
upperPanel.add(resultBox);
lowerPanel.setLayout(new FlowLayout());
lowerPanel.add(addButton);
lowerPanel.add(subButton); // set up topLowerPanel
lowerPanel.add(multipButton); // components and layout
lowerPanel.add(dividButton);
subPanel.setLayout(new BorderLayout());
subPanel.add(upperPanel, BorderLayout.NORTH);
subPanel.add(lowerPanel, BorderLayout.SOUTH); // add each panel to the main subPanel
this.add(subPanel);
addButton.addActionListener(new ButtonListener()); // add button listeners
subButton.addActionListener(new ButtonListener()); // for each of the
multipButton.addActionListener(new ButtonListener()); // four buttons
dividButton.addActionListener(new ButtonListener()); //
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton buttonX = (JButton) e.getSource();
num1 = Double.parseDouble(textBox1.getText()); // read values in textBox1
num2 = Double.parseDouble(textBox2.getText()); // and textBox2
if (buttonX == addButton){
result = num1 + num2;
}else if (buttonX == subButton){ // performed required calculation
result = num1 - num2;
}else if (buttonX == multipButton){
result = num1 * num2;
}else if (buttonX == dividButton){
result = num1 / num2;
}
resultBox.setText("" + result);
} // end actionPerformed() method
} // end ButtonListener class
} // end WholePanel class
这是LayoutPanel.java的代码
package layoutpanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LayoutPanel extends JApplet{
public void init(){
WholePanel wholePanel = new WholePanel();
getContentPane().add(wholePanel);
setSize(300,200);
}
当我运行applet时,它显示为:
如您所见,看起来好像topPanel和lowerPanel都以某种方式都位于子面板的顶行。
为了好奇,我尝试将subPanel更改为BorderLayout,并将topPanel定位到NORTH,将lowerPanel定位到SOUTH。但是,当我运行applet时,它看起来仍然完全一样!
我仔细考虑了代码,试图看看我做错了什么,但我完全被难过了。 我希望一双新鲜的眼睛会看到我犯了什么粗心的错误。
欢迎任何和所有建议。
答案 0 :(得分:1)
你有一系列问题......
默认情况下, JPanel
使用FlowLayout
,因为您永远不会更改WholePanel
的布局管理器,这就是它继续使用的内容。
您应该将其更改为使用BorderLayout
public WholePanel() { // constructor
setLayout(new BorderLayout());
现在,根据原始布局,我可能会想GridBagLayout
subPanel
subPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
subPanel.add(upperPanel, gbc);
gbc.gridy = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.SOUTH;
subPanel.add(lowerPanel, gbc); // add each panel to the main subPanel
this.add(subPanel);
可能导致类似......