如何生成带有多个按钮和标有递增数字的标签的图形用户界面?

huangapple go评论73阅读模式
英文:

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

问题

以下是翻译好的部分:

第一个尝试的代码:

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

    public ButtonScreen(){
        super("欢迎使用ButtonScreen程序!");
        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);
    }
}

第二个尝试的代码:

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

    public LabArr(){
        super("标题");
        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]);
            }
        }
    }

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

请注意,翻译结果可能会因格式限制而导致代码出现错乱。如果需要完整的代码格式,请参考原始英文内容。

英文:

I am trying to generate a GUI using swing that creates a frame with 100 buttons and every button has a "label" on it from on 1 to a 100.

What I have tried:

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(&quot;Welcome to the ButtonScreen Program!&quot;);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    bpanel.setLayout(new GridLayout(rows,cols));


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

This works just fine, but it creates buttons with "labels" (meaning string parameters at line 26) and also these labels, are not one string or one integer but it is a dillusion of the of i right next to j counter. So my second attempt, after some corrections, was:

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(&quot;title&quot;);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(rows,cols));

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

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


    }//end of constructor

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

My goal using that was to create an array of JLabel objects and then match every JLabel element to one from the JButton array.

So, first of all I would like to know to which object the methods setLayout and setDefaultCloseOperation refer to.

Second, "Does the toString method need to refer to an object at the line I am using it?". And finally, what am I missing?

答案1

得分: 1

这将创建一个带有从1到100的按钮的单个框架。

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

public class HundredButtonGrid {

    public static void main(String[] args) {
        JFrame frame = new JFrame("100个按钮");
        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);
    }
}

我已经处理了一些事情。

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

This will create a single frame with buttons from 1 to 100.

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

public class HundredButtonGrid{

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

    }

}

I've addressed a few things.

  • I just use 1 index because the GridLayout takes care of the x/y values.
  • I used parenthesis to add the index to 1 so that it doesn't end up concatenating them by string.
  • I've set the default close operation to exit, you can have it do other things too. Like JFrame.DISPOSE_ON_CLOSE so the window will go away, but not terminate the application.
  • I didn't extend JFrame, I created a separate instance. In your case you have extended JFrame, so when you call setLayout it is being called on the instance you're creating. An alternative way to say it is this.setLayout same with setDefaultCloseOperation.

答案2

得分: 0

希望这能帮助您解决问题,以下是代码部分:

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) {
        LabArr la = new LabArr();
        la.setVisible(true);
    }

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

    public String toString(int k){
        String s;
        s = "" + k;
        return s;
    }
}

您一直在使用 toString() 方法名来创建带有标签的按钮。

英文:

Hope this will help you with your problem, kindly see the code below:


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(&quot;title&quot;);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(rows,cols));

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

        for(int i=0;i&lt;button.length;i++){
            for(int j=0;j&lt;button.length;j++){
                button[i][j]= new JButton(counter + &quot;&quot;);

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


    }//end of constructor

    public String toString(int k){

        String s;
        s=&quot;&quot;+k;
        return s;
    }

}

You've been creating the buttons with the label toString() method name.

huangapple
  • 本文由 发表于 2020年9月14日 22:01:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63885885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定