java - 更改 JButton 大小的问题

标签 java swing

<小时/>

我正在尝试使用 Swing 库制作一个扫雷游戏(仅用于训练)。 当我尝试使用 setSize() 方法(Container 类的一部分)更改按钮的大小时,我发现了一个问题。我以前的一个程序能够处理这个问题(使用 ActionListener)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ButtonChanger extends JFrame {

JButton big, small, dis;
JLabel message, poof;

public ButtonChanger(){
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    message = new JLabel("Click to make it");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    add(message, c);


    big =  new JButton("BIG");
    big.setSize(30, 30); // no reaction for this call 
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    add(big, c);


    small = new JButton("small");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    c.gridy = 1;
    add(small, c);


    dis = new JButton("disapear");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 3;
    c.gridy = 1;
    add(dis, c);

    poof = new JLabel("  poof!");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 3;
    c.gridy = 1;
    poof.setVisible(false);
    add(poof, c);

    big.setSize(300,300); // same story

    event a = new event();
    big.addActionListener(a);
    small.addActionListener(a);
    dis.addActionListener(a);
}

public class event implements ActionListener{
    public void actionPerformed(ActionEvent a){

        String op = a.getActionCommand();

        if(op.equals("BIG")){
            big.setSize(50,50); // finaly!
        } else if(op.equals("small")){
            small.setSize(10,10);
        } else if(op.equals("disapear")){
            dis.setVisible(false);
            poof.setVisible(true);
        } 

    }
}

public static void main(String args[]){
    ButtonChanger gui = new ButtonChanger();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.setSize(250,250);
    gui.setTitle("");
}
}

但是当我尝试在这里使用相同的解决方案时 - 什么也没发生。

import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Board extends JPanel
               implements ActionListener{
private Integer pixelWidth;
private Integer pixelHeight;
public Field[][] board;


private Random generator = new Random();

public Board(int width,int height){ // width - x axis scaled with "button" unit
    // height -  y axis scaled with "button" unit

    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    board = new Field[width][height];

    pixelWidth = width * Field.fieldW;
    pixelHeight = height * Field.fieldH;

    for(int i = 0;i < height;i++)
        for(int j = 0;j < width;j++){
            board[i][j] = new Field(generator.nextBoolean());
        }

    for(int i = 0;i < height;i++){
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridy = i;
        for(int j = 0;j < width;j++){
            c.gridx = j;
            add(board[i][j].getFieldButton(),c);
            add(board[i][j].getFieldLabel(),c);
        }
    }

/*  for(int i = 0;i < height;i++)
        for(int j = 0;j < width;j++){
            board[i][j].setField();
        }*/

}
public void actionPerformed(ActionEvent e){
    String co = e.getActionCommand();
    if(co.equals(" ")){ // button is single space sign  
        for(int i = 0;i < 10;i++)
            for(int j = 0;j < 10;j++){
                board[i][j].setField(); //inside this method is call for setSize() for fieldButton
                }
    }
}
}

之前我更改了 Board 类,使其在 JFrame(在整个代码中设置为主 GUI 框架)之后不犹豫,但这给了我与原始相同的输出。

我是不是搞砸了什么?

最佳答案

big =  new JButton("BIG");
big.setSize(30, 30); // no reaction for this call 
c.fill = GridBagConstraints.HORIZONTAL;

big.setSize... 没有任何反应,因为在 GridBagConstraints 中您将填充设置为水平,因此按钮将被“拉伸(stretch)”以水平填充。

改为使用c.fill = GridBagConstraints.NONE

fill

Used when the component's display area is larger than the component's requested size to determine whether and how to resize the component. Valid values (defined as GridBagConstraints constants) include NONE (the default), HORIZONTAL (make the component wide enough to fill its display area horizontally, but do not change its height), VERTICAL (make the component tall enough to fill its display area vertically, but do not change its width), and BOTH (make the component fill its display area entirely).

您可以在这里阅读更多信息:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

关于java - 更改 JButton 大小的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43105557/

相关文章:

java - 无法关闭滚动 Pane 内文本区域的工具提示

java - Matlab deploytool 可以编译带有 javax.swing 元素的文件吗?

java - 使用 JOptionPane 更新 mysql

java - 将 Java UUID 作为字节发送到 C++,然后通过 TCP 返回

java - android,如何将 Activity 的功能分离到多个类中?

java - Applet : visually bugs out only in browser, 并从 Swing 抛出异常

java - 设置使用热敏打印机打印 JFrame 内容

java - 如何在mysql中插入长数字

java - 如何在 Eclipse 中禁用消息 "Parameter x is not assigned and could be declared final"?

java - 为什么这个错误 Exception in thread "main"java.lang.IllegalArgumentException : adding a window to a container