在JFrame中重复JButton

时间:2016-12-28 20:57:06

标签: java swing

我创建了一个&#34;主菜单&#34; JFrame具有不同JButton的不同时期。如果我点击其中一个按钮,则会打开一个新的JFrame,但每当我处理它并返回主菜单并将其打开时,它似乎更多的按钮,我不知道为什么。< / p>

Main Menu

    public class GUI extends JFrame implements ActionListener {
      public static final int WIDTH=1000;
      public static final int LENGTH=900;
      public static JFrame myFrame = new JFrame("TOK Discussion Participation");
      static JPanel myPanel = new JPanel();
      public static JComponent buttonPanel;
      public static JPanel IPanel = new JPanel();
      public static JFrame Period7 = new JFrame("Period 7");
      JPanel myPanel1 = new JPanel();

      public static Object createPanelPERIOD7;
      public JFrame getMyFrame()
      {
      return myFrame;
      }

      public void setWindow()
      {  
      //// Creates Icon in frame
      JLabel Image = new JLabel();
      ImageIcon myIcon = new ImageIcon(new ImageIcon("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg").getImage().getScaledInstance(200, 200, 1)); 
      Image.setIcon(myIcon); 
      //set window
      Color backgroundColor = new Color(5,149,251);
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setBounds(550,200,1000,600);
      myFrame.getContentPane().setBackground(Color.LIGHT_GRAY);
      myFrame.setLayout(new BorderLayout());
      myFrame.add(myPanel1,BorderLayout.NORTH);
      myFrame.add(new JLabel(new ImageIcon("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg")), BorderLayout.CENTER);
      //myFrame.setIconImage("/users/Antti/Desktop/GUI/TOKParticipation/src/TOK.jpg");
      myFrame.getContentPane().setBackground(backgroundColor);
      myFrame.setVisible(true); 


      //set label
      JLabel myLabel = new JLabel("TOK Discussion Participation");
      JLabel myLabel1= new JLabel("");
      JLabel myLabel2 = new JLabel("");

      //set button (PERIOD 7) 
      JButton Period_7_BUTTON = new JButton("Period 7");
      //set Listener (PERIOD 7)
      Period_7_BUTTON.addActionListener(new GUI());

      //2nd button (PERIOD 5)
      JButton Period_5_BUTTON = new JButton("Period 5");
      //set Listener PERIOD 5)
      Period_5_BUTTON.addActionListener(new GUI());

      //2nd button (PERIOD 2)
      JButton Period_2_BUTTON = new JButton("Period 2");
      //set Listener PERIOD 2)
      Period_2_BUTTON.addActionListener(new GUI());
      //2nd button (PERIOD 2)
      JButton Period_4_BUTTON = new JButton("Period 4");
      //set Listener PERIOD 4)
      Period_4_BUTTON.addActionListener(new GUI());

      //Setting Button FONT/SIZE
      Period_2_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
      Period_4_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
      Period_5_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));
      Period_7_BUTTON.setFont(new Font("Ubuntu", Font.BOLD,24));


      //Bundle button to panel
      myPanel1.setLayout(new BoxLayout(myPanel1, BoxLayout.X_AXIS));
      myPanel1.add(Period_7_BUTTON);
      myPanel1.add(Period_5_BUTTON);
      myPanel1.add(Period_2_BUTTON);
      myPanel1.add(Period_4_BUTTON);

      //add content to window and make it visible
      myFrame.add(myPanel1,BorderLayout.NORTH);
      myFrame.setVisible(true);
      }
      public void dispose(){
       myFrame.dispose();
       }

重新打开按钮时重复按钮的其他JFrame

 //Method to create Period 7 Frame


public static void createFramePERIOD7() {

   //Creates Panel + Buttons
   JPanel P7_Panel = new JPanel();
   JButton Column = new JButton("Add Columns");
   JButton Insert = new JButton("Insert Student 7");
   JButton Back = new JButton("Home");
   //Adding Buttons and Layout to Period 7 Frame
   Period7.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   Period7.setBounds(550,200,1000,600);
   Period7.setLayout(new FlowLayout());        
   Period7.add(Back);
   Period7.add(Column);
   Period7.add(Insert);
   //Add ActionListener to Buttons
   Back.addActionListener(new GUI());
   Insert.addActionListener(new GUI());
   Column.addActionListener(new GUI());
   //Adding Panel to Frame
   //Period7.add(P7_Panel);
   Period7.setVisible(true);
   }

最后我的结束听众代码

    public void actionPerformed(ActionEvent e) 
   {

   String buttonCommand = e.getActionCommand();      

   //Main menu Period 7 button
   if(buttonCommand.equals("Period 7"))
   {
   myFrame.dispose();
    createFramePERIOD7();
   } 
if(buttonCommand.equals("Home"))


{
   setWindow();
   Period7.dispose();
   }

1 个答案:

答案 0 :(得分:0)

您的方法每次都会添加3个按钮...您可以通过更改

来修复它
createFramePERIOD7()  method

插入下面所示的行。

  public static void createFramePERIOD7() {

   //Creates Panel + Buttons
   JPanel P7_Panel = new JPanel();
   JButton Column = new JButton("Add Columns");
   JButton Insert = new JButton("Insert Student 7");
   JButton Back = new JButton("Home");
   Period7 = new JFrame(); //<-------------------------- Insert this line
   //Adding Buttons and Layout to Period 7 Frame
   Period7.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   Period7.setBounds(550,200,1000,600);
   Period7.setLayout(new FlowLayout());        
   Period7.add(Back);
   Period7.add(Column);
   Period7.add(Insert);
   //Add ActionListener to Buttons
   Back.addActionListener(new GUI());
   Insert.addActionListener(new GUI());
   Column.addActionListener(new GUI());
   //Adding Panel to Frame
   //Period7.add(P7_Panel);
   Period7.setVisible(true);
   }