1

如何设置按钮以链接到完全不同的网格窗格?例如,如果我单击 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();
}

}
4

1 回答 1

3

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

这是一个简单的例子

在此处输入图像描述

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

阅读如何使用 CardLayout

于 2013-09-30T21:54:10.867 回答