微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何生成带有多个按钮并在其上增加标签的GUI?

如何解决如何生成带有多个按钮并在其上增加标签的GUI?

我正在尝试使用swing来生成GUI,该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方法是否需要在我正在使用的行上引用对象?”。最后,我想念什么?

解决方法

这将创建一个带有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.setLayoutsetDefaultCloseOperation相同。
,

希望这可以帮助您解决问题,请参见以下代码:


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()方法名称的按钮。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。