gpt4 book ai didi

JAVA图形按钮

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

我在java上创建了一个矩阵,我需要在矩阵下方添加两个JButton“取消”和“提交”这是我的代码,但它缺少两个 JButton,请需要您的帮助。

public class matrice2 extends JFrame{

private static final long serialVersionUID = 1L;

private final int width;
private final int height;

private final JLabel[] horizon;
private final JLabel[] vertical;
private final JButton[][] centre;
//private final JButton[] VC;

private final ImageIcon ZERO = new ImageIcon("0.jpg");
private final ImageIcon ONE = new ImageIcon("1.jpg");

public matrice2(int width, int height) {
this.width = width;
this.height = height;

horizon = new JLabel[width];
vertical = new JLabel[height];
centre = new JButton[width][height];

initFrame();
fillContent();

this.setVisible(true);
}

private void initFrame() {
this.setSize(700, 700);
this.setTitle("Matrice du graphe");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private JLabel createColumnHeader(int x) {
horizon[x] = new JLabel("H" + (x + 1));
return horizon[x];
}

private JLabel createRowHeader(int y) {
vertical[y] = new JLabel("V" + (y + 1));
return vertical[y];
}

private JButton createCell(int x, int y) {
centre[x][y] = new JButton();
centre[x][y].addActionListener(new MatriceListener(this,x,y));
return centre[x][y];
}

public void fillContent() {

this.setLayout(new GridLayout(width+2, height+2));

this.add(new JLabel());

for (int x = 0; x < width; x++)
this.add(createColumnHeader(x));

for (int y = 0; y < height; y++) {
this.add(createRowHeader(y));
for (int x=0; x<width; x++)
this.add(createCell(x,y));


}

}

public void setIcon(int x, int y) {
centre[x][y].setIcon(ONE);
}

最佳答案

更改 fillContent() 方法:

public void fillContent() {

JPanel content = new JPanel();

content.setLayout(new GridLayout(width + 2, height + 2));

content.add(new JLabel());

for (int x = 0; x < width; x++) {
content.add(createColumnHeader(x));
}

for (int y = 0; y < height; y++) {
content.add(createRowHeader(y));
for (int x = 0; x < width; x++) {
content.add(createCell(x, y));
}

}
add(content, BorderLayout.CENTER);

JPanel buttons = new JPanel();
buttons.add(new JButton("Submit"));
buttons.add(new JButton("Cancel"));

add(buttons, BorderLayout.SOUTH);
}

关于JAVA图形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29185485/

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