2011-03-17 41 views
0

想法是在屏幕頂部有5個JButton。每次按下它都會刪除舊菜單並生成一個新菜單。對我而言,我的代碼看起來很好。問題是當我點擊「添加CD」沒有任何反應,但如果我手動調整窗口大小(移動我的鼠標到窗口的邊緣,並改變它的大小)新菜單彈出...任何人都可以給我建議如何解決這個問題...謝謝,整個可運行代碼如下。使用JButton更改Java中的界面

package ui; 

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



public class mainInterface extends JFrame implements ActionListener 
{ 

    // Variables declaration - do not modify 
    private JButton jButton1; 
    private JButton jButton2; 
    private JButton jButton3; 
    private JButton jButton4; 
    private JButton jButton5; 
    private JButton jButton6; 
    private JPanel jPanel1; 

    private JTextField text1; 
    private JTextField text2; 
    private JTextField text3; 
    private JTextField text4; 
    private JTextField text5; 
    private JTextField text6; 
    private JTextField text7; 
    private JTextField text8; 

    private JLabel label1; 
    private JLabel label2; 
    private JLabel label3; 
    private JLabel label4; 
    private JLabel label5; 
    private JLabel label6; 
    private JLabel label7; 
    private JLabel label8; 

    private JPanel bottomPanel; 
    private JPanel topPanel;    // declaring panels 
    private JPanel holdAll; 

    private JPanel one; 
    private JPanel two; 
    private JPanel three; 
    private JPanel four; 

//---------------------------------------------------------- 

//---------------------------------------------------------- 
    public static void main(String args[]) 
    { 
     java.awt.EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       mainInterface myApplication = new mainInterface(); 

       myApplication.setLocation(100, 100);     

       myApplication.setSize(700, 400);      

       myApplication.setTitle("Kevin's Jukebox"); 

       myApplication.setVisible(true); 


      } 
     }); 
    } 
//---------------------------------------------------------- 

//---------------------------------------------------------- 
    /** Creates new form mainInterface */ 
    public mainInterface() 
    { 

     jButton1 = new JButton("Add CD"); 
     jButton2 = new JButton("Add Video"); 
     jButton3 = new JButton("Total Play Time"); 
     jButton4 = new JButton("Create Playlist"); 
     jButton5 = new JButton("Show Library"); 
     jButton6 = new JButton("Quit"); 

     topPanel = new JPanel(); 
     holdAll = new JPanel(); 
     bottomPanel = new JPanel(); 

     one = new JPanel(); 
     two = new JPanel(); 
     three = new JPanel(); 
     four = new JPanel(); 

     text1 = new JTextField(15); 
     label1 = new JLabel("Title: "); 
     text2 = new JTextField(15); 
     label2 = new JLabel("Artist: "); 
     text3 = new JTextField(15); 
     label3 = new JLabel("Length: "); 
     text4 = new JTextField(15); 
     label4 = new JLabel("Num of Tracks: "); 
     label5 = new JLabel("Welcome to Kevins Jukebox"); 

     int flag = 0; 
     drawApp(flag); 

     jButton1.addActionListener(this); 
     jButton2.addActionListener(this); 
     jButton3.addActionListener(this); 
     jButton4.addActionListener(this);       
     jButton5.addActionListener(this); 
     jButton6.addActionListener(this); 




     setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

    } 
//---------------------------------------------------------- 

//---------------------------------------------------------- 
    public void actionPerformed(ActionEvent e) 
    { 
     if (e.getSource() == jButton1)        
     { 

      int flag = 1; 
      drawApp(flag); 
     } 
    } 
//---------------------------------------------------------- 

//---------------------------------------------------------- 
    public void drawApp(int flag) 
    { 
     topPanel.setLayout(new FlowLayout()); 
      topPanel.add(jButton1); 
      topPanel.add(jButton2); 
      topPanel.add(jButton3); 
      topPanel.add(jButton4); 
      topPanel.add(jButton5); 
      topPanel.add(jButton6); 

     bottomPanel.add(label5); 

     holdAll.setLayout(new BorderLayout()); 
      holdAll.add(topPanel, BorderLayout.NORTH); 
      holdAll.add(bottomPanel, BorderLayout.CENTER); 

     if (flag == 0) 
      bottomPanel.add(label5); 
     else 
      bottomPanel.remove(label5); 

     if (flag == 1) 
     { 

      one.add(label1); 
      one.add(text1); 
      bottomPanel.add(one); 
      two.add(label2); 
      two.add(text2); 
      bottomPanel.add(two); 
      three.add(label3); 
      three.add(text3); 
      bottomPanel.add(three); 
      four.add(label4); 
      four.add(text4); 
      bottomPanel.add(four); 

      bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS)); 
     } 
     getContentPane().add(holdAll, BorderLayout.CENTER); // places everything on the frame 


    } 
//---------------------------------------------------------- 

} 
+1

作爲一個快速解決方案,我建議在添加組件和設置佈局後,在你的「if(flag == 1)」中調用'bottomPanel.validate()'。問題在於您的組件已經實現,因此,對GUI所做的任何更改(例如單擊按鈕以填充面板)都不會顯示。 – mre 2011-03-17 00:50:15

回答

3

在更新UI的佈局後執行this.validate()。調用這個方法告訴UI重新佈局它的組件。

+0

非常感謝你 – Kevin 2011-03-17 00:55:54

+2

@Kevin:你可以點擊左邊的空白複選標記來接受這個答案。 – trashgod 2011-03-17 02:59:52