首页 > 解决方案 > 如何生成带有多个按钮和递增标签的 GUI?

问题描述

我正在尝试使用 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 行的字符串参数)以及这些标签,不是一个字符串或一个整数,而是 j 计数器旁边的 i 的幻觉。因此,经过一些更正后,我的第二次尝试是:

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 方法是否需要引用我正在使用的行中的对象?”。最后,我错过了什么?

标签: javaarraysswingjlabeltostring

解决方案


这将创建一个带有 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这样窗口会消失,但不会终止应用程序。
  • 我没有扩展 JFrame,我创建了一个单独的实例。在您的情况下,您已经扩展了 JFrame,因此当您调用setLayout它时,它会在您正在创建的实例上被调用。另一种说法this.setLayout与 相同setDefaultCloseOperation

推荐阅读