按下按钮时会弹出多个帧

时间:2017-05-13 13:33:42

标签: java button

我正在尝试创建一个用户菜单,使用户可以选择不同的选项,然后程序打开我想要的帧,当我按下正确的按钮,按下按钮1后,它会弹出相同的帧2按下下一个按钮后继续这样做。

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

public class Menu {


    public static void main(String[] args) {
        JFrame frame = new JFrame("Maze");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,200);
        JPanel panel = new JPanel();
        frame.add(panel);
        JButton button1, button2;
        button1 = new JButton("Insert a File");
        button2 = new JButton("Generate");
        panel.add(button1);
        panel.add(button2);
        button1.addActionListener(new thehandler());
        button2.addActionListener(new thehandler2());

        thehandler handler = new thehandler();
        button1.addActionListener(handler);

        thehandler2 handler2 = new thehandler2();
        button2.addActionListener(handler2);

    }

    static class thehandler implements ActionListener {

        public void actionPerformed (ActionEvent event) {
            JFrame frame2 = new JFrame("Select");
            frame2.setVisible(true);
            frame2.setSize(300,150);
            JPanel panel2 = new JPanel();
            frame2.add(panel2);
            JButton button3, button4;
            button3 = new JButton("Solving");
            button4 = new JButton("Play");
            panel2.add(button3);
            panel2.add(button4);

            button3.addActionListener(new thehandler3());
            button4.addActionListener(new thehandler4());

            thehandler handler3 = new thehandler();
            button3.addActionListener(handler3);

            thehandler2 handler4 = new thehandler2();
            button4.addActionListener(handler4);

    }


    }

    static class thehandler3 implements ActionListener {

        public void actionPerformed (ActionEvent event) {
            String stringInteger = JOptionPane.showInputDialog(null, "Insert 
File: ", "Question", JOptionPane.INFORMATION_MESSAGE);

        }
    }

    static class thehandler4 implements ActionListener {

        public void actionPerformed (ActionEvent event) {
            String stringInteger = JOptionPane.showInputDialog(null, "Insert 
File: ", "Question", JOptionPane.INFORMATION_MESSAGE);

        }
    }

    static class thehandler2 implements ActionListener {

        public void actionPerformed (ActionEvent event) {
            String stringInteger = JOptionPane.showInputDialog(null, "Insert 
the size: ", "Question", JOptionPane.INFORMATION_MESSAGE);

        }
    }


}

到目前为止我编写的所有代码。 see the problem here 我真的不知道为什么会这样,并会感谢任何解决问题的建议。

1 个答案:

答案 0 :(得分:0)

这可能是因为您在ActionListener上添加了两次相同的JButton(不知道为什么要这样做)。现在JButton有两个ActionListener虽然相同,但每次点击都会产生两个框架。

    button1.addActionListener(new thehandler());
    button2.addActionListener(new thehandler2());

    //No need of below lines of codes, these are just repetition of above functionality
    /*thehandler handler = new thehandler(); 
    button1.addActionListener(handler);

    thehandler2 handler2 = new thehandler2();
    button2.addActionListener(handler2); */

您必须在代码中恰好发生此类重复的两个位置进行更改。