Java 小程序布局未按预期显示 o_O
Java Applet Layout Doesn't Appear As It Is Supposed To o_O
我这学期要参加 Java class,并且正在做一个带回家的测验。
在其中我们创建了一个计算器,它应该看起来像附加的第一张图片
如您所见,基本面板应该分为 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);
}
当我 运行 小程序显示为:
如您所见,似乎 topPanel 和 lowerPanel 都位于子面板的顶行。
出于好奇,我尝试将 subPanel 更改为 BorderLayout,并将 topPanel 定位到 NORTH,将 lowerPanel 定位到 SOUTH。然而,当我 运行 小程序时,它仍然显示完全一样!
我仔细研究了代码,试图找出我做错了什么,但我完全被难住了。
我希望有一双新的眼睛能看到我犯了什么粗心的错误。
欢迎提出任何建议。
你有很多问题...
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);
这可能会导致类似...
我这学期要参加 Java class,并且正在做一个带回家的测验。
在其中我们创建了一个计算器,它应该看起来像附加的第一张图片
如您所见,基本面板应该分为 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);
}
当我 运行 小程序显示为:
如您所见,似乎 topPanel 和 lowerPanel 都位于子面板的顶行。
出于好奇,我尝试将 subPanel 更改为 BorderLayout,并将 topPanel 定位到 NORTH,将 lowerPanel 定位到 SOUTH。然而,当我 运行 小程序时,它仍然显示完全一样!
我仔细研究了代码,试图找出我做错了什么,但我完全被难住了。 我希望有一双新的眼睛能看到我犯了什么粗心的错误。
欢迎提出任何建议。
你有很多问题...
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);
这可能会导致类似...