简体   繁体   English

更改JButton大小的问题

[英]Issue with changing JButton size


I'm trying to make a mine sweeper game with Swing library (just for training). 我正在尝试使用Swing库制作扫雷游戏(仅用于培训)。 I spotted a problem when I tried to change size of a button with setSize() method (part of Container class). 当我尝试使用setSize()方法(Container类的一部分)更改按钮的大小时,发现一个问题。 One of my previous program was able to handle that (with ActionListener) 我以前的程序之一能够(使用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("");
}
}

But when I tried to use same solution here - nothing happened. 但是,当我尝试在此处使用相同的解决方案时-没有任何反应。

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
                }
    }
}
}

Previously I changed Board class to make it inhetitate after JFrame (set up as main GUI frame in whole code) but that gave me same output as original. 以前,我更改了Board类,使其在JFrame(在整个代码中设置为主GUI框架)之后变得犹豫不决,但这给了我与原始输出相同的输出。

Did I mess up something? 我搞砸了吗?

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

There is no reaction for big.setSize... because then in GridBagConstraints you set fill to horizontal, so button will be "stretched" to fill horizontally. 对于big.setSize...没有反应,因为然后在GridBagConstraints中将填充设置为水平,因此按钮将被“拉伸”以水平填充。

Instead use c.fill = GridBagConstraints.NONE 而是使用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). 有效值(定义为GridBagConstraints常量)包括NONE(默认值),HORIZONTAL(使组件足够宽以水平填充其显示区域,但不更改其高度),VERTICAL(使组件足够高以垂直填充其显示区域) ,但不要更改其宽度)和两者(使组件完全填满其显示区域)。

You can read more here: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html 您可以在此处了解更多信息: https : //docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM