gpt4 book ai didi

java - JFrame 的按钮布局问题

转载 作者:行者123 更新时间:2023-12-01 19:59:48 25 4
gpt4 key购买 nike

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

最佳答案

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

  • 最好不要设置任何东西的大小,而是让组件和布局管理器为您完成此操作。
  • 如果您需要更全面地控制组件的大小,请考虑是否必须重写 getPreferredSize()
  • 添加所有组件后、调用 setVisible(true) 之前,在顶级窗口上调用 pack()。这将告诉布局管理器去做他们的事情。
  • 避免扩展 JFrame,因为您很少需要覆盖其固有行为之一。
  • 如果您确实添加或删除组件,或者在渲染顶级窗口后以某种方式更改其首选尺寸,您将需要调用 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();
}
});
}
}

关于java - JFrame 的按钮布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18678833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com