JFrame的按钮布局问题

时间:2013-09-07 23:16:06

标签: java swing jframe jpanel layout-manager

Broken buttons 已包含图片我还有另一个问题,即用户输入其名称后我的按钮会转到右上角。此时,文本显示在中心左侧的GUI中,当我放入“CENTER”时,它似乎是“WEST”。代码:

public TheDungeon()
{
  setTitle("InsertGameNameHere");
  setSize(750, 600);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setLayout(new BorderLayout());
  setLocationRelativeTo(null);

  buildButtonPanel();             

  characterInfoPanel = new JLabel("<html>Character information will go here</html>");
  gameScreen = new JLabel();
  inventoryPanel = new JLabel("<html>This is for the inventory</html>");

  add(gameScreen, BorderLayout.CENTER);
  add(buttonPanel, BorderLayout.SOUTH);

  setVisible(true);    

  //Program run
  userName();
  gameScreen.setText("<html>Welcome "+name+", to the game that has no name!</html>");
  classWindow();
} 

private void buildButtonPanel()
{
  // Create a panel for the buttons.
  buttonPanel = new JPanel();

  // Create the buttons.
  b1 = new JButton("Button 1");
  b2 = new JButton("Button 2");
  b3 = new JButton("Button 3");
  b4 = new JButton("Button 4");
  b5 = new JButton("Button 5");

  // Add the buttons to the button panel.
  buttonPanel.add(b1);
  buttonPanel.add(b2);
  buttonPanel.add(b3);
  buttonPanel.add(b4);
  buttonPanel.add(b5);
}


private void userName() {
name = JOptionPane.showInputDialog("What will your name be?");
}

2 个答案:

答案 0 :(得分:4)

我不确定为什么你的程序表现得像我跑的那样,它没有这样做。您可能希望检查您的代码,以确保它是您在此处发布的代码。但无论如何,我确实有一些建议:

  • 最好不要设置任何内容的大小,而是让组件和布局管理器为您执行此操作。
  • 如果您需要更全面地控制组件的大小,请考虑是否必须覆盖getPreferredSize()
  • 在添加所有组件之后和调用pack()之前,在顶级窗口中调用setVisible(true)。这将告诉布局经理做他们的事情。
  • 避免扩展JFrame,因为您很少需要覆盖其固有行为之一。
  • 如果您在渲染顶级窗口后添加或删除组件或以某种方式更改其preferredSize,您将需要在组件的容器上调用revalidate()然后repaint()以重新容纳容器 - 布置它所拥有的组件,然后重新绘制它们。

例如:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.*;

public class TheDungeon2 extends JPanel {
   private static final int PREF_W = 750;
   private static final int PREF_H = 600;
   private static final String[] BUTTON_LABELS = {"Button 1", "Button 2", 
         "Button 3", "Button 4", "Button 5"};
   private static final String WELCOME_TEXT = "Welcome %s to the game that has no name!";

   private JLabel welcomeLabel = new JLabel("", SwingConstants.CENTER);
   private String name;   

   public TheDungeon2() {
      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      for (String buttonLabel : BUTTON_LABELS) {
         JButton button = new JButton(buttonLabel);
         buttonPanel.add(button);
      }

      setLayout(new BorderLayout());
      add(welcomeLabel, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.PAGE_END);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public void getAndSetName() {
      name = JOptionPane.showInputDialog(this, "What will your name be?");
      welcomeLabel.setText(String.format(WELCOME_TEXT, name));
   }

   private static void createAndShowGui() {
      TheDungeon2 dungeon2 = new TheDungeon2();

      JFrame frame = new JFrame("Nameless Game");

      dungeon2.getAndSetName();

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(dungeon2);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

答案 1 :(得分:3)

我用一个空的classWindow()方法测试了你的代码,并且这些按钮被正确地放在了南方,

对于CENTER问题,您应该在WEST中放置一些文本居中(甚至是空面板),否则CENTER将占据所有位置,

看看这个,我添加了这一行:

add(new JButton("a button for test"),BorderLayout.WEST);

以下是结果:

demo