我应该如何制作一个“后退按钮”来切换主JFrame类上的JPanels与多个JPanel类一起工作?

时间:2013-10-04 00:48:49

标签: java swing jframe jpanel jbutton

我正在为学校做一个游戏,而且我已经打了一堵砖墙。我已经创建了一个设置JFrame的主类,并且在该JFrame中有JPanel按钮,用于打开服务器JPanel类,客户端JPanel类,选项按钮以及退出游戏。现在我陷入困境的是我应该如何使用服务器/客户端JPanel类上的后退按钮返回主JPanel。这是我目前的代码:

MainUI.class(另一个类运行它):

public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;

public MainUI() {
    // Sets up the frame
    setTitle("Pong Legacy | Prototype v0.1.0");
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 500);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    // Starts the Server window
    JButton btnStartServer = new JButton("Start Server");
    btnStartServer.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JPanel server = new ServerUI();
            getContentPane().removeAll();
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(server);
            getContentPane().validate();
            getContentPane().repaint();
        }
    });
    btnStartServer.setBounds(97, 364, 100, 25);
    contentPane.add(btnStartServer);

    // Starts the Client window
    JButton btnStartClient = new JButton("Start Client");
    btnStartClient.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JPanel client = new ClientUI();
            getContentPane().removeAll();
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(client);
            getContentPane().validate();
            getContentPane().repaint();
        }
    });
    btnStartClient.setBounds(97, 400, 100, 25);
    contentPane.add(btnStartClient);

    // Opens the Options menu 
    // (To Do)
    JButton btnOptions = new JButton("Options");
    btnOptions.setBounds(37, 436, 100, 25);
    contentPane.add(btnOptions);

    // Quits the game
    JButton btnQuitGame = new JButton("Quit Game");
    btnQuitGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    btnQuitGame.setBounds(157, 436, 100, 25);
    contentPane.add(btnQuitGame);

    // Username Field
    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setBounds(121, 45, 52, 14);
    contentPane.add(lblUsername);

    textField = new JTextField();
    textField.setBounds(104, 67, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);
}
}

ServerUI.class:

public class MainUI extends JFrame {
private JPanel contentPane;
private JTextField textField;

public MainUI() {
    // Sets up the frame
    setTitle("Pong Legacy | Prototype v0.1.0");
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 500);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    // Starts the Server window
    JButton btnStartServer = new JButton("Start Server");
    btnStartServer.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JPanel server = new ServerUI();
            getContentPane().removeAll();
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(server);
            getContentPane().validate();
            getContentPane().repaint();
        }
    });
    btnStartServer.setBounds(97, 364, 100, 25);
    contentPane.add(btnStartServer);

    // Starts the Client window
    JButton btnStartClient = new JButton("Start Client");
    btnStartClient.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JPanel client = new ClientUI();
            getContentPane().removeAll();
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(client);
            getContentPane().validate();
            getContentPane().repaint();
        }
    });
    btnStartClient.setBounds(97, 400, 100, 25);
    contentPane.add(btnStartClient);

    // Opens the Options menu 
    // (To Do)
    JButton btnOptions = new JButton("Options");
    btnOptions.setBounds(37, 436, 100, 25);
    contentPane.add(btnOptions);

    // Quits the game
    JButton btnQuitGame = new JButton("Quit Game");
    btnQuitGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    btnQuitGame.setBounds(157, 436, 100, 25);
    contentPane.add(btnQuitGame);

    // Username Field
    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setBounds(121, 45, 52, 14);
    contentPane.add(lblUsername);

    textField = new JTextField();
    textField.setBounds(104, 67, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);
}
 }

ClientUI.class:

public class ClientUI extends JPanel {

private JTextField textField;

public ClientUI() {
    setLayout(null);

    JButton btnConnect = new JButton("Connect");
    btnConnect.setBounds(47, 400, 200, 25);
    add(btnConnect);

    JButton btnBack = new JButton("Back");
    btnBack.setBounds(117, 436, 60, 25);
    add(btnBack);

    JRadioButton rdbtnSelectAServer = new JRadioButton("Select a server from the list:");
    rdbtnSelectAServer.setBounds(66, 25, 161, 25);
    add(rdbtnSelectAServer);

    JRadioButton rdbtnManualConnection = new JRadioButton("Manual Connection:");
    rdbtnManualConnection.setBounds(87, 325, 120, 25);
    add(rdbtnManualConnection);

    textField = new JTextField();
    textField.setBounds(47, 355, 200, 25);
    add(textField);
    textField.setColumns(10);
}
}

我听说过我也可以使用CardLayout,但我想知道我是否可以这样做。

0 个答案:

没有答案