【问题标题】:buttons resizing them selves?按钮自己调整大小?
【发布时间】:2012-05-05 02:37:48
【问题描述】:

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


}

【问题讨论】:

    标签: java user-interface


    【解决方案1】:

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

    base.setLayout(null);
    

    如果你想强制组件的大小和位置(使用 setBounds()),那么你需要移除布局管理器。

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

    【讨论】:

    • @LucasRibeiro 不客气。我编辑了我的答案,因为您可能应该考虑使用布局管理器,但这取决于您。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-11-20
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多