按钮调整自己?

时间:2012-05-04 18:13:39

标签: java user-interface

我和一个朋友正在尝试使用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");
         }
      }


}

2 个答案:

答案 0 :(得分:0)

CustomPanel base上将布局管理器设置为null。

base.setLayout(null);

如果要强制组件的大小和位置(使用setBounds()),则需要删除布局管理器。

然而,LayoutManagers在不同平台上提供更好的UI体验,因为它们将适应差异。 LayoutManager根据preferredSize和constraints执行组件的大小调整和定位。如果您从未使用过它们或从中听过它们,您应该考虑对它们进行研究:http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

答案 1 :(得分:0)

好吧,我没看到菜单代码。但是,默认情况下,Panel的布局管理器是Flow Layout。由于您未指定任何布局,因此假定Flow Layout,并且您指定的任何大小调整都将被忽略。

所以,正如Guillaume建议的那样,将它设置为null,这样你就可以绝对定位。或根据您的需要使用更复杂的布局。看看如何在swing tutorial中使用布局管理器。 GridBagLayout是最复杂的(难以使用),除非你使用某种gui构建器。其他候选人包括BorderLayoutGridLayout等。仔细阅读示例,看看哪一个适合您的情况。