java - 关闭框架java后重新启用按钮

标签 java swing jframe jbutton jdialog

所以我正在为我的项目制作一个程序。 当我单击一个按钮时,它必须打开另一个框架并使该按钮不可单击。当您关闭弹出框时,按钮必须重新启用。所以这是

我的主框架

    package Option2;
    import javax.swing.event.*;
    import javax.swing.*;

    import java.awt.*;
    import java.awt.event.*;

    public class MainMenu {
        int intCtr1 = 0;
        JFrame frame1 = new JFrame("EXD LAN PARTY");
        JButton Button1 = new JButton();
        JButton Button2 = new JButton();
        JButton Button3 = new JButton();
        JButton Button4 = new JButton();
        JLabel Label1 = new JLabel();
        public void MainMenu(){
            //BUTTON1
            Button1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI2.jpg")));
            Button1.setBackground(Color.white);
            Button1.setBounds(50, 350, 150, 150);
            Button1.setToolTipText("Personal Info");
            //BUTTON1 END
            //BUTTON2
            Button2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PC.jpg")));
            Button2.setBackground(Color.white);
            Button2.setBounds(250, 350, 150, 150);
            Button2.setToolTipText("PC INFO");
            //BUTTON2 END
            //BUTTON3
            Button3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Games.jpg")));
            Button3.setBackground(Color.white);
            Button3.setBounds(450, 350, 150, 150);
            Button3.setToolTipText("Games");
            //BUTTON3 END
            //BUTTON4 END
            Button4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Players.jpg")));
            Button4.setBackground(Color.white);
            Button4.setBounds(650, 350, 150, 150);
            Button3.setToolTipText("Players");
            //BUTTON4 END
            //LABEL1
            Label1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/EXD.jpg")));
            Label1.setBounds(50, 50, 800, 250);
            //LABEL1 END
            //Frame1
            frame1.getContentPane().setBackground(Color.black);
            frame1.setResizable(false);
            frame1.setLayout(null);
            frame1.setSize(870,650);
            frame1.setVisible(true);
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.add(Label1);
            frame1.add(Button1);
            frame1.add(Button2);
            frame1.add(Button3);
            frame1.add(Button4);
            //Frame1 END
            Button1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent a){ 
                    PersonalInfo objPI = new PersonalInfo();
                    objPI.Menu1();
                    Button1.setEnabled(false);
                }   
            });
            Button2.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent a){ 
                    PCInfo objPCI = new PCInfo();
                    objPCI.Menu2();
                }   
            });
            Button3.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent a){ 
                    Games objGames = new Games();
                    objGames.Menu3();
                }   
            });
            Button4.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent a){ 
                    Players objPlayers = new Players();
                    objPlayers.Menu4();
                }   
            });
        }
        public void dim1(){
            if(intCtr1 == 1){
                MainMenu objMM  = new MainMenu();
                objMM.Button1.setEnabled(true);
                System.out.println("SD");
            }
        }
    }

**and this is my sub frame**
 package Option2;
import javax.swing.event.*;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class PersonalInfo {
    String[] arrSex = {"Male","Female"};
    JFrame frame1 = new JFrame("Personal Info");
    JLabel label1 = new JLabel("ID");
    JLabel label2 = new JLabel("Last Name");
    JLabel label3 = new JLabel("First Name");
    JLabel label4 = new JLabel("Middle Name");
    JLabel label5 = new JLabel("SEX");
    JLabel label6 = new JLabel();
    JTextField tf1 = new JTextField();
    JTextField tf2 = new JTextField();
    JTextField tf3 = new JTextField();
    JTextField tf4 = new JTextField();
    JComboBox CB1 = new JComboBox(arrSex);
    JButton Button1 = new JButton("NEW");
    JButton Button2 = new JButton("SAVE");
    JButton Button3 = new JButton("EDIT");
    JButton Button4 = new JButton("CANCEL");
    JButton Button5 = new JButton();
    JButton Button6 = new JButton();
    JButton Button7 = new JButton();
    JButton Button8 = new JButton();
    public void Menu1(){
                //Frame1
                frame1.add(label6);
                frame1.add(label1);
                frame1.add(tf1);
                frame1.add(label2);
                frame1.add(tf2);
                frame1.add(label3);
                frame1.add(tf3);
                frame1.add(label4);
                frame1.add(tf4);
                frame1.add(label5);
                frame1.add(CB1);
                frame1.add(Button1);
                frame1.add(Button2);
                frame1.add(Button3);
                frame1.add(Button4);
                frame1.add(Button5);
                frame1.add(Button6);
                frame1.add(Button7);
                frame1.add(Button8);
                frame1.setVisible(true);
                frame1.getContentPane().setBackground(Color.black);
                frame1.setSize(600,600);
                frame1.setResizable(false);
                frame1.setLayout(null);
                frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame1.addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        MainMenu objMM = new MainMenu();
                        objMM.intCtr1=1;
                        objMM.dim1();
                    }
                });
                //Frame1 End
                //LABEL6
                label6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI.jpg")));
                label6.setBounds(30, 5, 800, 250);
                //LABEL6 END
                //LABEl1 
                label1.setBounds(100, 220, 50,50);
                label1.setForeground(Color.white);
                label1.setFont(new Font("Serif", Font.BOLD, 18));
                //Label1 end
                //Tf
                tf1.setBounds(130, 230, 400,30);
                tf1.setEnabled(false);
                SmartC objSMC = new SmartC();
                tf1.setText(objSMC.SmartCounter("ABC123415XYZS"));
                //tf end
                //label2
                label2.setBounds(35, 255, 120,50);
                label2.setForeground(Color.white);
                label2.setFont(new Font("Serif", Font.BOLD, 18));
                //label2 end
                //Tf2
                tf2.setBounds(130, 270, 400,30);
                //tf2 end
                //label3
                label3.setBounds(35, 295, 120,50);
                label3.setForeground(Color.white);
                label3.setFont(new Font("Serif", Font.BOLD, 18));
                //label3 end
                //Tf3
                tf3.setBounds(130, 310 , 400, 30);
                //tf3 end
                //label4
                label4.setBounds(15, 335, 120,50);
                label4.setForeground(Color.white);
                label4.setFont(new Font("Serif", Font.BOLD, 18));
                //label4 end
                //Tf4
                tf4.setBounds(130, 350 , 400, 30);
                //tf4 end
                //label4
                label5.setBounds(85, 375, 120,50);
                label5.setForeground(Color.white);
                label5.setFont(new Font("Serif", Font.BOLD, 18));
                //label4 end
                //cb1
                CB1.setBounds(130, 390, 100, 30);
                CB1.setBackground(Color.white);
                //cb1 end
                //button1
                Button1.setBounds(35, 450, 100, 30);
                Button1.setBackground(Color.white);
                //
                //
                Button2.setBounds(150, 450, 100, 30);
                Button2.setBackground(Color.white);
                //
                //
                Button3.setBounds(335, 450, 100, 30);
                Button3.setBackground(Color.white);
                //
                //
                Button4.setBounds(450, 450, 100, 30);
                Button4.setBackground(Color.white);
                //
                //
                Button5.setBounds(35, 500, 100, 50);
                Button5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/First.jpg")));
                Button5.setBackground(Color.white);
                //
                //
                Button6.setBounds(150, 500, 100, 50);
                Button6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Previous.jpg")));
                Button6.setBackground(Color.white);
                //
                //
                Button7.setBounds(335, 500, 100, 50);
                Button7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Next.jpg")));
                Button7.setBackground(Color.white);
                //
                //              
                Button8.setBounds(450, 500, 100, 50);
                Button8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Last.jpg")));
                Button8.setBackground(Color.white);
                //
                //
    }
}

关于我的编码的任何建议都被接受。抱歉,还在学习 Java :)

最佳答案

and when i clicked a button it must open anohter frame

您不应该创建另一个 JFrame。

相反,您应该创建一个modal JDialog。在对话框关闭之前,对话框不允许您单击框架。

Any Suggestions about my coding is accepted

遵循 Java 命名约定。变量名称不应以大写字符开头。有时您会遵循此准则,有时则不会。保持一致!

不要使用 setBounds(...)。 Swing 旨在与布局管理器一起使用!

关于java - 关闭框架java后重新启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25458599/

相关文章:

java - 根据窗口的屏幕分辨率调整图像

java - 无法使用 getResource() 将图像添加到按钮(使用 ant 获取 NullPointerException- 构建项目)

java - 多个移动球 : Adding Ball works but removing does not

java - 无法清除 JFrame

java - 我应该将基本 Activity 添加到 list 文件中吗?

java - JFileChooser 事件

java - JComponent 内容 Pane

java - WebSocket session 之间共享的所有成员是否应该同步?

java - 当我运行这个小鼠标钩子(Hook)应用程序时,为什么我的鼠标出现滞后?

java - 使用编程在android上设置触摸输入