如何使String值调用Java中特定的现有JButton变量名? [英] How to make String value to call specific existed JButton variable name in java?

查看:81
本文介绍了如何使String值调用Java中特定的现有JButton变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道是这样的:

So, I know there is this:

int number = Integer.parseInt("5");
String numtxt = Integer.toString(12);
double number = Double.parseDouble("4.5");
String numbertxt = Double.toString(8.2);
String letter = Character.toString('B');
char letter = "stringText".charAt(0);

so on...

但是我不知道如何动态地调用String值来存在JButton变量名;甚至有可能吗?

but what I don't know how to make String value to call existed JButton variable name dynamically; is it even possible?

比方说,我有4个JButton,分别为btn1,btn2,btn3和btnFillNumber;

Let's say, I have 4 JButton called btn1, btn2, btn3 and btnFillNumber;

我创建了一个名为buttonName的字符串;

I create a String called buttonName;

package testing;

public class Testing extends javax.swing.JFrame {

String buttonName;
int num;

public Testing() {
    initComponents();
}

@SuppressWarnings("unchecked")
// Generated Code <<<-----

private void btnFillNumberActionPerformed(java.awt.event.ActionEvent evt) {                                              
    for(num = 1; num <= 3; num++){
        buttonName = "btn" + Integer.toString(num);
        JButton.parseJButton(buttonName).setText(num);
    }
}                                             

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    // Look and feel stteing code (optional) <<<-----

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Testing().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton btn1;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private javax.swing.JButton btnFillNumber;
// End of variables declaration                   
}

我知道这里没有JButton.parseJButton(),我只是不想做复杂的解释,我只是想从String转换为动态地调用JButton的变量名.

I know there's no JButton.parseJButton(), I just don't want to make complicated explaination, I simple want a convertion from String to call JButton's variable name dynamically.

看到这个:

    for(num = 1; num <= 3; num++){
        buttonName = "btn" + Integer.toString(num);
        JButton.parseJButton(buttonName).setText(num);
    }

我想用一个字符串来做一个循环

I want to make a loop using a String with

  • 固定的字符串值(btn)和
  • 其后的递增数字(1、2、3 ...)和
  • 用于调用JButton.

我可以简单地做到这一点,但是如果我的年满25岁或更多,该怎么办?所以我想要一个循环...

I can just simply do this, but what if I got a 25 or more? So a loop what I wanted...

btn1.setText("1");
btn2.setText("2");
btn3.setText("3");

请注意,出于某些目的,这些JButton的值不一定是递增的.

Note that the value of these JButtons are not necessarily incremental in some purpose.

输出:

我的真实发展:

P.S.我曾经在NetBeans Design中制作JFrame(只需单击并拖动调色板窗口中的对象(如JPanel,JButton等),所以除了创建自己的逻辑方法外,我不会手动键入代码;而且我无法编辑代码在源视图"中以灰色背景显示,该视图由设计视图"自动创建,但在设计视图"本身中.如果您有提示和指导,我将很高兴).

P.S. I use to make JFrame in NetBeans Design (just click and drag the objects in palette window like JPanel, JButton, etc., so I don't type the code manually except making my own logical Method; and I can't edit the code in grey background in Source View that was made automatically by Design View but in Design View itself. If you have tips and guide, I'll be happy to).

推荐答案

使用地图:

private Map<String,JButton> buttonMap = new HashMap<String,JButton>();

在构造函数中添加按钮:

In your constructor add your buttons:

buttonMap.add("btn1", btn1);
buttonMap.add("btn2", btn2);
buttonMap.add("btn3", btn3);
buttonMap.add("btn4", btn4);

在您的动作监听器/循环中,制作任何内容:

And in your action Listener / Loop whatever make:

String buttonName = "btn1"; //should be parameter or whatever
JButton button = buttonMap.get(buttonName);

作为替代方案,您也可以设置一个JButton数组:

As an alternative you could just as well set up an array of JButton:

JButton[] buttons = new JButton[4];

button[0] = new JButton(); //btn1
button[1] = new JButton(); //btn2
button[2] = new JButton(); //btn3
button[3] = new JButton(); //btn4

并访问它

JButton but = button[Integer.parseInt(buttonString)-1];

或者通过利用向UI元素添加自定义属性的可能性(为此,您将需要一个JComponent)

Or by utilizing the possibility of adding custom properties to UI elements (you'll need a JComponent for that)

getContentPane().putClientProperty("btn1", btn1);

,然后检索蒙山

JButton but = (JButton)getContentPane().getClientProperty("btn1");

这篇关于如何使String值调用Java中特定的现有JButton变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆