关闭帧java后重新启用按钮

时间:2014-08-23 04:58:13

标签: java swing jframe jbutton jdialog

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

我的主框架

    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 :))

2 个答案:

答案 0 :(得分:4)

  

当我点击一个按钮时,它必须打开anohter框架

您不应该创建另一个JFrame。

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

  

接受有关我的编码的任何建议

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

请勿使用setBounds(...)。 Swing旨在与layout managers一起使用!

答案 1 :(得分:1)

首先,请阅读Java naming conventions

您需要将MainMenu的引用传递给PersonalInfo才能实现您的需求,这里:

button1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent a){ 
                PersonalInfo objPI = new PersonalInfo(this);
                objPI.menu1();
                button1.setEnabled(false);
            }   
        });

您需要向PersonalInfo添加构造函数:

private MainMenu m;
public PersonalInfo(MainMenu m) {
    this.m = m;
}

向MainMenu添加公共方法:

public void enableMyButton() {
        button1.setEnabled(true);
    }

现在您可以向PersonalInfo添加一个事件监听器以启用MainMenu框架的按钮:

frame1.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    this.m.enableMyButton();
                }
            });
相关问题