gpt4 book ai didi

Java Applet 布局未按预期显示 o_O

转载 作者:行者123 更新时间:2023-12-01 11:03:51 25 4
gpt4 key购买 nike

我这学期正在学习 Java 类(class),并正在做一个带回家的测验。在其中我们创建了一个计算器,它应该看起来像所附的第一张图片 calculator_applet_intended

正如您所看到的,基础面板应该分为 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);
}

当我运行小程序时,它显示如下: enter image description here
正如您所看到的,看起来好像 topPanel 和 lowerPanel 都在 subPanel 的顶行中。

出于好奇,我尝试将 subPanel 更改为 BorderLayout,并将 topPanel 定位到北部,将 lowerPanel 定位到南部。然而,当我运行该小程序时,它看起来仍然完全相同!

我仔细研究了代码,试图找出我做错了什么,但我完全被难住了。我希望有一双新的眼睛能够看到我所犯的粗心错误。

非常欢迎任何和所有建议。

最佳答案

您遇到了一系列问题...

JPanel 默认情况下使用 FlowLayout,因此由于您从未更改 WholePanel 的布局管理器,因此它会继续使用。

您可能应该将其更改为使用类似 BorderLayout

public WholePanel() {                            // constructor
setLayout(new BorderLayout());

现在,根据原始布局,我可能会想对 subPanel 使用 GridBagLayout

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);

这可能会导致类似...

Layout

关于Java Applet 布局未按预期显示 o_O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com