2012-05-04 19 views
0

私と友人は、Javaのボタンでmp3プレーヤーを作ろうとしていますが、最初のボタンをクリックすると、2番目のメニューのすべてのバートンのサイズが変更されます。 ボタンをrezisingから保護する方法についての情報は非常に高く評価されます。ボタンは自分自身のサイズを変更しますか?

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()を使用して)コンポーネントのサイズと位置を強制的に変更する場合は、レイアウトマネージャを削除する必要があります。

しかし、レイアウトマネージャは、異なるプラットフォームにまたがって優れたUIエクスペリエンスを提供し、違いに対応します。 LayoutManagerは、preferredSizeとconstraintsに基づいてコンポーネントのサイジングと配置を実行します。あなたがそれらを使用していないか、彼らから聞いたことがない場合は、実際にそれらに探して検討する必要があります。http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

+0

ありがとうございました! –

+0

@LucasRibeiroよろしくお願いします。レイアウトマネージャを使用することを検討する必要がありますので、私は自分の答えを編集しましたが、それはあなた次第です。 –

0

だけでなく、私は、メニューのコードを見ていません。ただし、デフォルトではPanelのレイアウトマネージャはフローレイアウトです。レイアウトを指定していないので、Flow Layoutとみなされ、指定したサイジングはほとんど無視されます。ギヨームが示唆するよう

ので、nullに設定されているので、あなたは絶対に物事を配置することができます。また、必要に応じてより複雑なレイアウトを使用することもできます。 swing tutorialでレイアウトマネージャを使用する方法を見てください。は、GUIビルダーのようなものを使用しない限り、最も複雑です(使いにくい)。その他の候補は、BorderLayout,GridLayoutなどです。あなたのケースに合ったものがどれかを見てみましょう。

関連する問題