gpt4 book ai didi

Java 网格布局 GUI - 如何在事件中进入新 Pane ?

转载 作者:行者123 更新时间:2023-11-29 05:37:06 24 4
gpt4 key购买 nike

如何设置按钮链接到完全不同的网格 Pane ?例如,如果我单击 JButton“更多选项”,我希望它将我链接到具有更多 JButton 选项的新页面。现在,一切都是静态的。

当您按下“计算”时,程序现在只计算给定长度和宽度的矩形的面积。网格布局为 4 x 2,由下面列出的 JLabel、JTextField 和 JButton 表示。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class RectangleProgram extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 300;

private JLabel lengthL, widthL, areaL;
private JTextField lengthTF, widthTF, areaTF;
private JButton calculateB, exitB;

//Button handlers:
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;

public RectangleProgram()
{
lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
areaL = new JLabel("Area: ", SwingConstants.RIGHT);

lengthTF = new JTextField(10);
widthTF = new JTextField(10);
areaTF = new JTextField(10);

//SPecify handlers for each button and add (register) ActionListeners to each button.
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);

setTitle("Sample Title: Area of a Rectangle");
Container pane = getContentPane();
pane.setLayout(new GridLayout(4, 2));

//Add things to the pane in the order you want them to appear (left to right, top to bottom)
pane.add(lengthL);
pane.add(lengthTF);
pane.add(widthL);
pane.add(widthTF);
pane.add(areaL);
pane.add(areaTF);
pane.add(calculateB);
pane.add(exitB);

setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double width, length, area;

length = Double.parseDouble(lengthTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.
width = Double.parseDouble(widthTF.getText());
area = length * width;

areaTF.setText("" + area);
}
}

public class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

public static void main(String[] args)
{
RectangleProgram rectObj = new RectangleProgram();
}

}

最佳答案

您可以使用 CardLayout .它允许两个或多个组件共享相同的显示空间。

这是一个简单的例子

enter image description here

public class RectangleProgram {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {

JFrame frame = new JFrame("Area of a Rectangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextField lengthField = new JTextField(10);
JTextField widthField = new JTextField(10);
JTextField areaField = new JTextField(10);

JButton calculateButton = new JButton("Calculate");
JButton exitButton = new JButton("Exit");

final JPanel content = new JPanel(new CardLayout());

JButton optionsButton = new JButton("More Options");
optionsButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) content.getLayout();
cardLayout.next(content);
}
});

JPanel panel = new JPanel(new GridLayout(0, 2)) {
@Override
public Dimension getPreferredSize() {
return new Dimension(250, 100);
}
};
panel.add(new JLabel("Enter the length: ", JLabel.RIGHT));
panel.add(lengthField);

panel.add(new JLabel("Enter the width: ", JLabel.RIGHT));
panel.add(widthField);
panel.add(new JLabel("Area: ", JLabel.RIGHT));
panel.add(areaField);
panel.add(calculateButton);
panel.add(exitButton);

JPanel optionsPanel = new JPanel();
optionsPanel.add(new JLabel("Options", JLabel.CENTER));

content.add(panel, "Card1");
content.add(optionsPanel, "Card2");
frame.add(content);
frame.add(optionsButton, BorderLayout.PAGE_END);

frame.pack();
frame.setVisible(true);
}
});
}
}

阅读How to Use CardLayout

关于Java 网格布局 GUI - 如何在事件中进入新 Pane ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19104434/

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