2012-05-04 19 views
0

我和一個朋友正在嘗試使用java中的按鈕製作MP3播放器,但是一旦第一個按鈕被點擊,它就會調整第二個菜單上的所有按鈕。 任何有關如何保持按鈕不受重新調整的信息將不勝感激。按鈕調整他們自己?

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

    public class Player extends JFrame { 


    class CustomPanel extends JPanel{  //create image 
    public void paintComponent (Graphics painter){ 
     Image pic = Toolkit.getDefaultToolkit().getImage("playerBase.jpg"); 
     if(pic != null) painter.drawImage(pic, 0, 0, this); 
    } 
    } 

    public static void main(String[] args) { 
    Player gui = new Player(); 
    gui.go(); 
    } 

    public void go() { 
    JFrame frame = new JFrame("MP3 Player."); //Creates window. 
    CustomPanel base = new CustomPanel(); //Makes the image into a panel. 
    JButton button1 = new JButton("Artists"); 
    JButton button2 = new JButton("Genres"); 
    JButton button3 = new JButton("Songs"); 
    JButton button4 = new JButton("TEST"); 
    JButton button5 = new JButton("TEST"); 
    button1.setHorizontalAlignment(SwingConstants.LEFT); 
    button2.setHorizontalAlignment(SwingConstants.LEFT); 
    button3.setHorizontalAlignment(SwingConstants.LEFT); 
    button4.setHorizontalAlignment(SwingConstants.LEFT); 
    button5.setHorizontalAlignment(SwingConstants.LEFT); 
    button1.addActionListener(new Button1Listener()); 
    button2.addActionListener(new Button2Listener()); 
    button3.addActionListener(new Button3Listener()); 
    button4.addActionListener(new Button4Listener()); 
    button5.addActionListener(new Button5Listener()); 


    base.add(button1); 
    base.add(button2); 


     base.add(button3); 
     base.add(button4); 
     base.add(button5); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(304, 360); 
     frame.setResizable(false); 
     frame.add(base); 
     frame.setVisible(true); 

     button1.setSize(280, 30); 
     button1.setLocation(10,10); 
     button1.setBackground(Color.BLACK); 
     button1.setForeground(Color.white); 
     button2.setSize(280, 30); 
     button2.setLocation(10,40); 
     button2.setBackground(Color.BLACK); 
     button2.setForeground(Color.white); 
     button3.setSize(280, 30); 
     button3.setLocation(10,70); 
     button3.setBackground(Color.BLACK); 
     button3.setForeground(Color.white); 
     button4.setSize(280, 30); 
     button4.setLocation(10,100); 
     button4.setBackground(Color.BLACK); 
     button4.setForeground(Color.white); 
     button5.setSize(280, 30); 
     button5.setLocation(10,130); 
     button5.setBackground(Color.BLACK); 
     button5.setForeground(Color.white); 

     } 


    //These are the actions for the 5 buttons. 
    //Need to get buttons straight once first button is clicked 
     class Button1Listener implements ActionListener { 
     public void actionPerformed(ActionEvent event) { 
      if (event.getSource() instanceof JButton) { 
       JButton clickedButton = (JButton) event.getSource(); 
       clickedButton.setSize(280, 30); 
       clickedButton.setLocation(10,10); 
       clickedButton.setBackground(Color.BLACK); 
       clickedButton.setForeground(Color.white); 



       String buttonText = clickedButton.getText(); 
       if (buttonText.equals("Artists")) { 
        System.out.println("Artists"); 
        clickedButton.setText("Back"); 

       } 
       else if (buttonText.equals("Back")) { 
        System.out.println("Back"); 
       } 
      } 
     } 
     } 
    //these are just place holders for the other buttons. 
     class Button2Listener implements ActionListener { 
     public void actionPerformed(ActionEvent event) { 
      System.out.println("Genres"); 
     } 
     } 

     class Button3Listener implements ActionListener { 
     public void actionPerformed(ActionEvent event) { 
      System.out.println("Songs"); 
     } 
     } 

     class Button4Listener implements ActionListener { 
     public void actionPerformed(ActionEvent event) { 
      System.out.println("TEST"); 
     } 
     } 

     class Button5Listener implements ActionListener { 
     public void actionPerformed(ActionEvent event) { 
      System.out.println("TEST"); 
     } 
     } 


} 

回答

0

在您的CustomPanel base上將佈局管理器設置爲null。

base.setLayout(null); 

如果要強制組件的大小和位置(使用的setBounds()),那麼你需要刪除的佈局管理器。

但是,LayoutManagers跨不同平臺提供更好的UI體驗,因爲它們將適應差異。 LayoutManager根據preferredSize和約束來執行組件的大小調整和定位。如果你從來沒有使用它們或從他們聽到,你應該考慮到他們:http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

+0

非常感謝您的先生! –

+0

@LucasRibeiro不客氣。我編輯了我的答案,因爲您應該考慮使用佈局管理器,但這取決於您。 –

0

好吧,我沒有看到菜單代碼。但是,默認情況下,Panel的佈局管理器是流佈局。由於您沒有指定任何佈局,因此假設爲Flow Layout,並且您指定的任何大小將很大程度上被忽略。

因此,正如Guillaume所建議的那樣,將其設置爲null,以便您可以絕對地定位事物。或根據您的需要使用更復雜的佈局。查看swing tutorial中如何使用佈局管理器。 GridBagLayout是最複雜的(難以使用),除非你使用某種gui builder。其他候選人有BorderLayout,GridLayout等。通讀例子,看看哪一個適合你的情況。