【问题标题】:How to change the size of these buttons?如何更改这些按钮的大小?
【发布时间】:2018-02-17 20:33:49
【问题描述】:

如何改变这些按钮的大小?

import java.awt.Color;
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;

public class xog extends JFrame implements ActionListener {
    private JPanel mainPanel;

     JButton[][] butArray=new JButton[3][3];
     private int counter1=0;
    int size=3;
    int firstrow,firstcol,secondrow,secomdcol;

    public xog(){

        setTitle("XO");
        mainPanel=new JPanel();

        for(int r=0; r<this.size; r++)
            for(int c=0;c<this.size;c++){   
                this.butArray[r][c]=new JButton();
                this.butArray[r][c].setSize(100, 100);
                this.butArray[r][c].addActionListener(this);
                this.mainPanel.add(this.butArray[r][c]);

            }
        validate();

        getContentPane().add(mainPanel);
        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        for(int r=0; r<size; r++)
            for(int c=0;c<size;c++){
            if(e.getSource()==butArray[r][c]){
                counter1++;

            if(counter1%2==0){
                butArray[r][c].setText("x");
                this.butArray[r][c].setBackground(Color.red);
            }
            if(counter1%2==1){
                butArray[r][c].setText("o");
                this.butArray[r][c].setBackground(Color.blue);
            }
            }   
    }
    }
    public static void main(String[] args)
    {
        java.awt.EventQueue.invokeLater(new Runnable(){
        public void run()
        {   
            new xog().setVisible(true); 
        }
        });     
    }
}

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。使用edit 链接改进您的问题 - 不要通过 cmets 添加更多信息。谢谢!
  • JButton[][] butArray=new JButton[3][3]; 我怀疑这是否意味着按钮应该在视觉上排列成 3 宽 x 3 高的组?

标签: java swing graphics jbutton


【解决方案1】:

首先,setSize(和setBounds)是没有意义的,因为 Swing 正在使用布局管理器(在您的情况下为 FlowLayout)来决定组件的布局大小和位置。

其次,setPreferredSize 是一个非常糟糕的选择。为什么?按钮的首选大小会受到许多因素的影响,包括文本的大小/数量以及运行程序的系统的当前字体指标和 DPI 渲染特性。

所有这些设置都可以改变,你会被一个基本上看起来像垃圾的程序卡住。

你可以...

使用Button#setMargin 将边距添加到按钮当前首选大小的按钮。

这种方法很有用,因为它会影响按钮本身并将该信息提供给布局管理器,否则他们可能无法自己提供填充支持

你可以...

使用布局管理器,它允许您提供附加的尺寸提示,这些提示会添加到组件的首选尺寸中,例如,GridBagLayout

setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
// This will add 50 pixels to the preferred size's width and height
gbc.ipadx = 50;
gbc.ipady = 50;
JButton[][] butArray = new JButton[3][3];
int size = 3;
for (int r = 0; r < size; r++) {
    for (int c = 0; c < size; c++) {

        butArray[r][c] = new JButton();
        add(butArray[r][c], gbc);

    }
}

GridBagLayout 是可用的最灵活和最复杂的布局之一,应根据您的所有需求做出使用决定,而不仅仅是您为组件提供填充的需求。

How to Use GridBagLayout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2017-11-24
    相关资源
    最近更新 更多