如何生成带有多个按钮和递增标签的 GUI?

How to generate GUI with multiple buttons and incrementing labels on them?

我正在尝试使用 swing 生成一个 GUI,它创建一个包含 100 个按钮的框架,并且每个按钮上都有一个从 1 到 100 的“标签”。

我尝试过的:

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

public class ButtonScreen extends JFrame{
    JFrame frame = new JFrame();
    int rows=10;
    int cols=10;
    JButton[][] button = new JButton[rows][cols];
    JTextArea screen = new JTextArea();
    JPanel bpanel = new JPanel();
        public static void main(String[] args) {

        ButtonScreen bs = new ButtonScreen();
        bs.setVisible(true);
    }// end of main

   public ButtonScreen(){
    super("Welcome to the ButtonScreen Program!");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    bpanel.setLayout(new GridLayout(rows,cols));


    for(int i=0;i<button.length;i++){
        for(int j=0;j<button.length;j++){
            
                button[i][j]=new JButton(""+i+j);
                bpanel.add(button[i][j]);
         }
    }
    add(bpanel);
   }//end of constructor
}//end of class

这很好用,但它创建了带有“标签”的按钮(意思是第 26 行的字符串参数),而且这些标签不是一个字符串或一个整数,但它是 i 旁边的幻觉j 计数器。所以我的第二次尝试,经过一些更正后,是:

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


public class LabArr extends JFrame{
    JFrame frame = new JFrame();
    int rows=10;
    int cols= 10;
    int i=0;
    JButton[][] button = new JButton[rows][cols];
    JLabel[] label = new JLabel[100];

    public static void main(String[] args) {
        LabArr la = new LabArr();
        la.setVisible(true);
    }//end of main 

    public LabArr(){
        super("title");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(rows,cols));

        for(i=0;i<label.length;i++){
            label[i]= new JLabel(toString(i));
        }

        for(int i=0;i<button.length;i++){
            for(int j=0;j<button.length;j++){
                button[i][j]= new JButton(""+label[j]);
              
               add(button[i][j]);
            }
        }


    }//end of constructor

    public String toString(int k){
        k=i;
        String s;
        s=""+k;
        return s;
    }
    
}//end of class 

我使用它的目标是创建一个 JLabel 对象数组,然后将每个 JLabel 元素与 JButton 数组中的一个元素匹配。

所以,首先我想知道方法setLayout和setDefaultCloseOperation指的是哪个对象。

其次,“toString 方法是否需要在我使用它的那一行引用一个对象?”。最后,我错过了什么?

希望这对您的问题有所帮助,请看下面的代码:


public class LabArr extends JFrame{
    JFrame frame = new JFrame();
    int rows=10;
    int cols= 10;
    int counter = 0;

    JButton[][] button = new JButton[rows][cols];
    JLabel[] label = new JLabel[rows*cols];

    public static void main(String[] args) {
        DemoApplication la = new DemoApplication();
        la.setVisible(true);
    }//end of main

    public LabArr(){
        super("title");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(rows,cols));

        for(int i=0;i<label.length;i++){
            label[i]= new JLabel(toString(i));
        }

        for(int i=0;i<button.length;i++){
            for(int j=0;j<button.length;j++){
                button[i][j]= new JButton(counter + "");

                add(button[i][j]);
                counter++;
            }
        }


    }//end of constructor

    public String toString(int k){

        String s;
        s=""+k;
        return s;
    }

}

您一直在使用标签 toString() 方法名称创建按钮。

这将创建一个包含从 1 到 100 的按钮的框架。

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

public class HundredButtonGrid{

    public static void main(String[] args){
        JFrame frame = new JFrame("100 buttons");
        frame.setLayout(new GridLayout(10, 10));
        for(int i = 0; i<100; i++){
            frame.add( new JButton( "" + (i + 1) ) );
        }
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

我已经解决了一些问题。

  • 我只使用 1 个索引,因为 GridLayout 会处理 x/y 个值。
  • 我使用括号将索引添加到 1,这样它就不会最终通过字符串连接它们。
  • 我已经将默认关闭操作设置为退出,你也可以让它做其他事情。像 JFrame.DISPOSE_ON_CLOSE 这样 window 会消失,但不会终止应用程序。
  • 我没有扩展 JFrame,我创建了一个单独的实例。在您的情况下,您已经扩展了 JFrame,因此当您调用 setLayout 时,它是在您正在创建的实例上调用的。另一种说法是 this.setLayoutsetDefaultCloseOperation 相同。