Java 调整窗口大小时未调整JButtons大小
我正在为一个刽子手游戏创建一个虚拟键盘,并试图设置它,以便在调整窗口大小时调整按钮的宽度。现在,当我启动程序时,我看不到所有的按钮——我需要调整窗口的宽度,这样我才能看到所有的按钮 我尝试使用Java 调整窗口大小时未调整JButtons大小,java,swing,layout-manager,Java,Swing,Layout Manager,我正在为一个刽子手游戏创建一个虚拟键盘,并试图设置它,以便在调整窗口大小时调整按钮的宽度。现在,当我启动程序时,我看不到所有的按钮——我需要调整窗口的宽度,这样我才能看到所有的按钮 我尝试使用按钮。setPreferredSize(新维度(35,35))=0){ pnlKeyboardAL.add(按钮); } else如果(ZM.indexOf(c)>=0){ pnlKeyboardZM.add(按钮); } setPreferredSize()方法不会导致2秒延迟。组件不会更改大小,因为您使
按钮。setPreferredSize(新维度(35,35))当我启动程序时,在任何按钮出现在屏幕上之前有2秒的延迟。我已经在createKeyboard()中注释了这一行的位置
调整窗口大小时,如何挤压按钮(保持它们的顺序相同)?或者,是否有任何方法可以使用setPreferredSize
而不引起延迟
public GUI() {
createWindow();
createPanels();
addPanels();
createKeyboard();
setMainFrameVisible();
}
public void createWindow() {
mainFrame = new JFrame();
mainFrame.setSize(500, 300); //500, 300
mainFrame.setTitle("Hangman");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createPanels() {
pnlMain = new JPanel(new BorderLayout());
pnlKeyboard = new JPanel(new GridLayout (4, 1));
pnlKeyboardQP = new JPanel(new FlowLayout());
pnlKeyboardAL = new JPanel(new FlowLayout());
pnlKeyboardZM = new JPanel(new FlowLayout());
}
public void addPanels() {
mainFrame.add(pnlMain);
pnlMain.add(pnlKeyboard, BorderLayout.SOUTH);
pnlKeyboard.add(pnlKeyboardQP);
pnlKeyboard.add(pnlKeyboardAL);
pnlKeyboard.add(pnlKeyboardZM);
}
public void createKeyboard() {
String keyboardFormat = "QWERTYUIOPASDFGHJKLZXCVBNM";
String buttonLabel;
char buttonLabelChar;
for (int i = 0; i < keyboardFormat.length(); i ++) {
buttonLabelChar = keyboardFormat.charAt(i);
buttonLabel = "" + buttonLabelChar;
JButton button = new JButton(buttonLabel);
button.setPreferredSize(new Dimension(35, 35)); // This is causing 2 second delay when game started
button.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("Button pressed");
}
}
);
addKeyboard(buttonLabelChar, button);
}
}
public void addKeyboard(char c, JButton button) {
String QP = "QWERTYUIOP";
String AL = "ASDFGHJKL";
String ZM = "ZXCVBNM";
if (QP.indexOf(c) >= 0) {
pnlKeyboardQP.add(button);
}
else if (AL.indexOf(c) >= 0) {
pnlKeyboardAL.add(button);
}
else if (ZM.indexOf(c) >= 0) {
pnlKeyboardZM.add(button);
}
publicgui(){
createWindow();
createPanels();
添加面板();
创建键盘();
setMainFrameVisible();
}
public void createWindow(){
大型机=新的JFrame();
mainFrame.setSize(500300);/500300
主机。设置标题(“刽子手”);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
公共面板(){
pnlMain=newjpanel(newborderlayout());
pnlKeyboard=新JPanel(新网格布局(4,1));
pnlKeyboardQP=新的JPanel(新的FlowLayout());
pnlKeyboardAL=newjpanel(newflowlayout());
pnlKeyboardZM=新的JPanel(新的FlowLayout());
}
公共事务委员会(){
大型机.add(pnlMain);
pnlMain.add(pnlKeyboard,BorderLayout.SOUTH);
pnlKeyboard.add(pnlKeyboardQP);
pnlKeyboard.add(pnlKeyboardAL);
pnlKeyboard.add(pnlKeyboardZM);
}
公用键盘(){
String keyboardFormat=“qwertyuiopasdfghjklzxcvnm”;
字符串按钮标签;
字符按钮标签;
对于(inti=0;i=0){
pnlKeyboardQP.add(按钮);
}
else如果(AL.indexOf(c)>=0){
pnlKeyboardAL.add(按钮);
}
else如果(ZM.indexOf(c)>=0){
pnlKeyboardZM.add(按钮);
}
setPreferredSize()方法不会导致2秒延迟。组件不会更改大小,因为您使用的是FlowLayout,FlowLayout始终尊重组件的大小。可能您需要GridLayout。按钮的大小将调整为相同大小,并占用所有可用空间。@camickr感谢GridLayout可以工作,但因为有不同的每行中的按钮数某些行的按钮比其他行大。您可以使用组件来填充空白,就像使用空JLabel一样,或者使用不同的布局manager@Ansharja谢谢你的建议,我确实考虑过了,但不确定它是否会被认为是最佳实践。这是一个最好的解决方案,但我在这个网站上看到过其他答案,也有人建议使用这个解决方案(使用空标签)。