gpt4 book ai didi

Java,网格布局问题。难以放置元件

转载 作者:行者123 更新时间:2023-12-02 05:52:49 26 4
gpt4 key购买 nike

我认为 GridLayout 有问题。我尝试将 25 个按钮放置到 Jpanel“lightJpanel”上,但所有按钮都只位于 lightJpanel 左上角的一个按钮上。我尝试了很多东西,但我没有发现问题......按钮已创建,但不可见。解决问题的办法?

这是我的代码当前给我的内容:

enter image description here

这是我的代码:

public class Window extends JFrame {

/**
* Main JPanel
*/
private JPanel container = new JPanel();
/**
* Message area
*/
private JLabel screen = new JLabel();
/**
* table light
*/
private Light light[][] = new Light[5][5];
/**
* button who allows to configure the start of game
*/
private JButton configure = new JButton("Configure the lights");
/**
* button who allows to start the game
*/
private JButton play = new JButton("Play");
/**
* button who allows to place random lights
*/
private JButton random = new JButton("Random configuration");
/**
* stop button for stop the game
*/
private JButton stop = new JButton("Stop");


/**
* construct a window
*
* @param controller the controller of the window
*/
public Window() {
this.setSize(500, 500);
this.setTitle("Game of life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
initComponents();
this.setContentPane(container);
this.setVisible(true);
}

/**
* Initialization of graphical components
*/
private void initComponents() {

//for the message
Font font = new Font("Arial", Font.BOLD, 20);
screen = new JLabel("Game of life");
screen.setFont(font);
screen.setForeground(Color.white);

JPanel panScreen = new JPanel();
panScreen.setPreferredSize(new Dimension(480, 40));

JPanel menuButton = new JPanel();
menuButton.setPreferredSize(new Dimension(480, 100));

JPanel lightJpanel = new JPanel();
lightJpanel.setPreferredSize(new Dimension(300, 300));

//listeners for menu buttons
MenuListener menuListener = new MenuListener();

lightJpanel.setLayout(new GridLayout(5, 5, 0, 0));

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
light[i][j] = new Light(i, j);
lightJpanel.add(light[i][j]);
}
}

//size button
stop.setPreferredSize(new Dimension(70, 40));
play.setPreferredSize(new Dimension(70, 40));
random.setPreferredSize(new Dimension(200, 40));
configure.setPreferredSize(new Dimension(200, 40));

//add listener
play.addActionListener(menuListener);
random.addActionListener(menuListener);
configure.addActionListener(menuListener);
stop.addActionListener(menuListener);

//add buttons to JPanel menuButton
menuButton.add(configure);
menuButton.add(random);
menuButton.add(play);
menuButton.add(stop);

stop.setEnabled(false);

//decoration JLabel screen
panScreen.add(screen);
panScreen.setBackground(Color.blue);
panScreen.setBorder(new javax.swing.border.BevelBorder(BevelBorder.RAISED));
Border borderLine = BorderFactory.createLineBorder(Color.BLACK);
panScreen.setBorder(borderLine);

//test jpanel
lightJpanel.setBackground(Color.CYAN);
menuButton.setBackground(Color.black);
container.setBackground(Color.green);

//positioning different JPanel to main JPanel
container.add(panScreen);
container.add(menuButton);
container.add(lightJpanel);
}

这是我的 Light 类:

    import java.awt.Color;

import javax.swing.JButton;

public class Light extends JButton{

/**
* coordonates of the light according to the GridLayout
*/
private int x;
/**
* coordonates of the light according to the GridLayout
*/
private int y;
/**
* define if the light is lit or not
*/
private boolean lit;

/**
* construct a light
* @param abs the abscissa of the light
* @param ord the ordered of the light
*/
public Light(int abs, int ord){
super();
this.x = abs;
this.y = ord;
// Default color
this.setBackground(Color.gray);
this.lit = false;
}

public boolean getLit(){
return lit;
}

public int getX(){
return this.x;
}
public int getY(){
return this.y;
}

/**
* turn on the light
*/
public void setOn(){
this.setBackground(Color.green);
this.lit = true;
}
/**
* turn off the light
*/
public void setOff(){
this.setBackground(Color.gray);
this.lit = false;
}

public void changeState(){
if(lit)
setOff();
else
setOn();
}
}

最佳答案

没有竞争的例子,很难说;但您可以将您的方法与显示的方法进行比较 here ,如下图所示。特别是,

  • 验证 Light 是否具有预期的首选大小和位置。

  • 当您确实想要覆盖 getPreferredSize() 时,请勿使用 setPreferredSize(),如图 here .

image

关于Java,网格布局问题。难以放置元件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23405006/

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