0

我有一些 Swing 代码对多个面板使用卡片布局。当我尝试在主页面板上放置按钮时,它不起作用,就像我尝试使主页标题对齐时一样。无论如何可以使用 Box 布局进行这项工作吗?

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Main extends JFrame {
JFrame frame;

JPanel content;
JPanel Home;
JPanel Select;
JPanel Create;

CardLayout cardLayout;

JButton select;
JButton create;
JButton homebutton;
JButton homebutton2;
JTextField headerHome;
JTextField headerCreate;
JTextField headerSelect;
private JTextPane dfgdsfgdfgdfgTextPane;
private JPanel panel1;


public Main() {
    frame = new JFrame("Ice Cream Maker");
    content = new JPanel();
    Home = new JPanel();
    Select = new JPanel();
    Create = new JPanel();
    cardLayout = new CardLayout();

    homebutton = new JButton("Home");
    homebutton.setMaximumSize(new Dimension(50, 80));
    homebutton.setVisible(true);
    homebutton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cardLayout.show(content, "Home");
        }
    });


    homebutton2 = new JButton("Home");
    homebutton2.setMaximumSize(new Dimension(50, 80));
    homebutton2.setVisible(true);
    homebutton2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cardLayout.show(content, "Home");
        }
    });

    Create.add(homebutton);
    Select.add(homebutton2);


    select = new JButton("Select");
    select.setMaximumSize(new Dimension(50, 80));
    select.setVisible(true);
    select.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cardLayout.show(content, "Select");
        }
    });
    Home.add(select, Box.createHorizontalStrut(100));

    create = new JButton("Create");
    create.setMaximumSize(new Dimension(50, 80));
    create.setVisible(true);
    create.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cardLayout.show(content, "Create");
        }
    });
    Home.add(create);

    headerHome = new JTextField("Ice Cream Maker!");
    headerHome.setMaximumSize(new Dimension(100, 200));
    headerHome.setVisible(true);
    Home.add(headerHome, Box.CENTER_ALIGNMENT);

    headerCreate = new JTextField("Create an Ice Cream!");
    headerCreate.setMaximumSize(new Dimension(100, 200));
    headerCreate.setVisible(true);
    Create.add(headerCreate);

    headerSelect = new JTextField("Select an Ice Cream");
    headerSelect.setMaximumSize(new Dimension(100, 200));
    headerSelect.setVisible(true);
    Select.add(headerSelect);


    content.setLayout(cardLayout);
    content.add(Home, "Home");
    content.add(Select, "Select");
    content.add(Create, "Create");
    cardLayout.show(content, "Home");
    frame.add(content);
    frame.setSize(640, 480);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);


}


public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Main();
        }
    });
}
}
4

0 回答 0