gpt4 book ai didi

Java 基本 GUI(布局错误)

转载 作者:行者123 更新时间:2023-12-01 11:42:18 25 4
gpt4 key购买 nike

所以我正在创建一个包含 5x5 按钮矩阵的游戏,其中一个按钮后面是一个隐藏的奖品。我已经完成了游戏,但布局有问题。

我添加了图片: /image/r8q9Z.png

当我运行程序时,会出现左侧的图片,当我单击按钮时,会出现右侧的图片。

第一个 JFrame 看起来很奇怪,我的意思是它占据了整个屏幕的长度,所以我需要打包它以便以合适的尺寸显示按钮。

第二个,我需要第一行只显示标签,然后每行包含 5 个按钮。

我很确定我使用了错误的布局,但我不知道如何修复它。下面是代码:

MyJFrame类代码:

class MyJFrame extends JFrame {
JPanel panel2;

public MyJFrame() {
setLayout(new GridLayout(7, 7));

panel2 = new PanelJ2();

add(panel2);
JButton b = new JButton("Start the freaking game");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel2.setVisible(true);
b.setVisible(false);
PotLuck.random = (int)(Math.random() * 25 + 1);
}
});
add(b);
setTitle( "This is a freaking game");
pack();
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

PanelJ2代码:

class PanelJ2 extends JPanel {
JButton[] buttons;
JLabel label, l;
public static int count;

public PanelJ2() {
setLayout( new GridLayout(5,5) );
setPreferredSize( new Dimension( 300, 200)); //I just wrote this line, I am not sure of the purpose quite really.
count = 0;
label = new JLabel("Number of guesses so far: " + count);
add(label);
l = new JLabel("");

buttons = new JButton[25];
for( int i = 0; i <= 24; i ++) {

buttons[i] = new JButton(String.valueOf(i));
add(buttons[i]);
buttons[i].addActionListener( new ExampleActionListener1());
}
setBackground( Color.green);
setVisible(false); //this is set true when the button "Start the freaking game is pressed"
}
}

最佳答案

这个 setLayout(new GridLayout(7, 7)); 将导致主要问题。一个更简单的问题是使用 CardLayout

参见How to Use CardLayout了解更多详情

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

class MyJFrame extends JFrame {

JPanel panel2;

private CardLayout cl;

public MyJFrame() {
cl = new CardLayout();
setLayout(cl);
panel2 = new PanelJ2();

add(panel2, "LotsOfButtons");
JButton b = new JButton("Start the freaking game");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(getContentPane(), "LotsOfButtons");
}
});
add(b, "start");
cl.show(getContentPane(), "start");
setTitle("This is a freaking game");
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

static class PanelJ2 extends JPanel {

JButton[] buttons;
JLabel label, l;
public static int count;

public PanelJ2() {
setLayout(new GridLayout(5, 5));
// setPreferredSize(new Dimension(300, 200)); //I just wrote this line, I am not sure of the purpose quite really.
count = 0;
label = new JLabel("Number of guesses so far: " + count);
add(label);
l = new JLabel("");

buttons = new JButton[25];
for (int i = 0; i <= 24; i++) {

buttons[i] = new JButton(String.valueOf(i));
add(buttons[i]);
// buttons[i].addActionListener(new ExampleActionListener1());
}
setBackground(Color.green);
setVisible(false); //this is set true when the button "Start the freaking game is pressed"
}

}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

MyJFrame frame = new MyJFrame();
}
});
}
}

您可能想将按钮放入不同的容器中,也许使用GridBagLayout

关于Java 基本 GUI(布局错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29428232/

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