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");
         }
      }


}
4

2 回答 2

0

在您的CustomPanel base.

base.setLayout(null);

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

但是,LayoutManagers 可以在不同平台上提供更好的 UI 体验,因为它们会适应差异。LayoutManager 基于 PreferredSize 和约束执行组件的大小调整和定位。如果您从未使用过它们或收到过它们的消息,那么您真的应该考虑研究一下它们:http ://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

于 2012-05-04T18:21:49.283 回答
0

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

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

于 2012-05-04T18:29:22.613 回答