java button名字,如何使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...

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

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

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

}

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.

See this:

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

a fixed String value (btn) and

increment number after it (1, 2, 3...) and

make use to call a JButton.

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

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

Output:

9f8ca05af9e7540b2bc4d83cb89c3ba5.png

My real development:

a3c0e30a3593947c1a70c8e6e76d227d.png

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).

解决方案

Use a Map:

private Map buttonMap = new HashMap();

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

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

And access it

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

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

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

and later retrieving whith

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值