如何使用动作侦听器重新启动屏幕?

时间:2013-11-26 03:52:33

标签: java swing actionlistener

当我意识到我还没有制作我的标题画面时,我正在查看我的Java项目。但是我想到了一个问题:如何在按下新文件按钮的位置进行操作时,它会清除屏幕上的所有内容并将新内容放入其中?

简单来说,如何制作一个动作监听器,当他们点击按钮时屏幕会清除并打开一个新屏幕?

package Main_Config;

    import java.awt.*;

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

    public class SET_UP extends JFrame {

            private JPanel contentPane;
            private JTextField textField;
            private JLabel consol;
            public static Dimension size = new Dimension(800, 700);


            /**
             * Launch the application.
             */
            public static void main(String[] args) {

            EventQueue.invokeLater(new Runnable() {
                    public void run() {

                            try {

                                    SET_UP frame = new SET_UP();
                                    frame.setVisible(true);
                            } catch (Exception e) {
                                    e.printStackTrace();
                            }
                    }
            });
    }

    /**
     * Create the frame.
     */
    public SET_UP() {

            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(370, 70, 0, 0);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
            setSize(size);
            setLocationRelativeTo(null);

            textField = new JTextField();
            textField.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                            String input = textField.getText(); 
                                consol.setText(input);

                    }
            });
            textField.setBounds(10, 452, 243, 20);
            contentPane.add(textField);
            textField.setColumns(10);

            JButton enter = new JButton("Enter");
            enter.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent arg0) {
                }
            });

                enter.setBounds(253, 452, 89, 20);
            contentPane.add(enter);

            JLabel consol = new JLabel("");
            consol.setBounds(0, 483, 335, 189);
            contentPane.add(consol);

            JButton btnNewButton = new JButton("New button");
            btnNewButton.setBounds(352, 451, 200, 23);
            contentPane.add(btnNewButton);

            JButton btnNewButton_1 = new JButton("New button");
            btnNewButton_1.setBounds(584, 451, 200, 23);
            contentPane.add(btnNewButton_1);

            JButton btnNewButton_2 = new JButton("New button");
            btnNewButton_2.setBounds(0, 0, 89, 23);
            contentPane.add(btnNewButton_2);
    }
}

1 个答案:

答案 0 :(得分:0)

要清除屏幕,请执行以下操作:

Button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                frame.setVisible(false);
                frame.remove(panelTwo);
                frame.remove(panelThree);
                frame.add(panelFour);
                frame.setVisible(true);
            }
        });