Java 引用通过方法指定的按钮的名称
作为背景故事的一个暗示,我真的进入了java,并且一直在网上观看所有这些视频,我认为虽然学习它是好的,但如果我不能实际使用它,它是毫无意义的。所以我用了一台计算器。除了一件事,一切都很顺利 为了展示我的懒惰,我创建了一个方法,该方法接受参数并使用它们通过一个简单的方法调用构建一个JButton。该代码是:Java 引用通过方法指定的按钮的名称,java,swing,calculator,Java,Swing,Calculator,作为背景故事的一个暗示,我真的进入了java,并且一直在网上观看所有这些视频,我认为虽然学习它是好的,但如果我不能实际使用它,它是毫无意义的。所以我用了一台计算器。除了一件事,一切都很顺利 为了展示我的懒惰,我创建了一个方法,该方法接受参数并使用它们通过一个简单的方法调用构建一个JButton。该代码是: public class GUI_Element_Methods{ private JButton button1; private JButton numpad[]; public v
public class GUI_Element_Methods{
private JButton button1;
private JButton numpad[];
public void createButton(String buttonText, ActionListener eventMethod, boolean visible, String tooltipText){
button1 = new JButton(buttonText);
button1.addActionListener(eventMethod);
button1.setVisible(visible);
button1.setToolTipText(tooltipText);
}
public JButton getButton1(){
return(button1);
}
呼吁:
guiElement.createButton("+", asHandler, true, "Addition");
add(guiElement.getButton1());
我工作得很好,但是我很难检测某个操作是否是事件处理程序中的某个按钮,因为它们都通过这个“button1”JButton运行。例如,毕达哥拉斯定理,它有两个变体,基于您试图解决的问题,我只想使用一个事件处理程序来检测按下哪个
if(event.getSource().equals()){
}else{
}
这就是我想说的,我只是不知道如何引用方法创建的按钮。您总是可以通过获取按钮的actionCommand来区分按下的按钮,即
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
}
然后可以将该字符串与字符串常量进行比较,以查看是否有所需的按钮
例如:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class NumberPad extends JPanel {
public static final String[][] NUMBER_TEXTS = {
{"7", "8", "9"},
{"4", "5", "6"},
{"1", "2", "3"}
};
public NumberPad() {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionCommand: " + e.getActionCommand());
}
};
setLayout(new GridLayout(3, 3));
for (String[] row : NUMBER_TEXTS) {
for (String cell : row) {
JButton button = new JButton(cell);
button.addActionListener(actionListener);
add(button);
}
}
}
private static void createAndShowGui() {
NumberPad mainPanel = new NumberPad();
JFrame frame = new JFrame("Number Pad");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
您可以通过获取按钮的actionCommand来区分按下的按钮,即
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
}
然后可以将该字符串与字符串常量进行比较,以查看是否有所需的按钮
例如:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class NumberPad extends JPanel {
public static final String[][] NUMBER_TEXTS = {
{"7", "8", "9"},
{"4", "5", "6"},
{"1", "2", "3"}
};
public NumberPad() {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionCommand: " + e.getActionCommand());
}
};
setLayout(new GridLayout(3, 3));
for (String[] row : NUMBER_TEXTS) {
for (String cell : row) {
JButton button = new JButton(cell);
button.addActionListener(actionListener);
add(button);
}
}
}
private static void createAndShowGui() {
NumberPad mainPanel = new NumberPad();
JFrame frame = new JFrame("Number Pad");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
您可以创建一个
ArrayList
,其中将包含您将创建的所有JButton
对象
将按钮添加到其中,并在actionPerformed
方法上进行比较
List<JButton> btnList = new ArrayList<JButton>();
. . .
btnList.add(button1);
. . .
void actionPerformed(...)
for(JButton btn : btnList){
if(event.getSource().equals(btn)){
//do whatever you want with this button.
}
}
List btnList=new ArrayList();
. . .
btnList.add(按钮1);
. . .
已执行的无效操作(…)
用于(JButton btn:btnList){
if(event.getSource()等于(btn)){
//你想用这个按钮做什么都行。
}
}
您可以创建一个ArrayList
,其中将包含您将创建的所有JButton
对象
将按钮添加到其中,并在actionPerformed
方法上进行比较
List<JButton> btnList = new ArrayList<JButton>();
. . .
btnList.add(button1);
. . .
void actionPerformed(...)
for(JButton btn : btnList){
if(event.getSource().equals(btn)){
//do whatever you want with this button.
}
}
List btnList=new ArrayList();
. . .
btnList.add(按钮1);
. . .
已执行的无效操作(…)
用于(JButton btn:btnList){
if(event.getSource()等于(btn)){
//你想用这个按钮做什么都行。
}
}
您可能还希望从createButton
方法返回按钮,这可能会使被调用方更容易操作您可能还希望从createButton
方法返回按钮,这可能会使被调用方更容易获得帮助,使操作命令工作。看着你的代码,我想起了多维数组的存在,完全忘记了那些。我应该使用一个数字键盘。谢谢你的帮助,让动作命令正常工作。看着你的代码,我想起了多维数组的存在,完全忘记了那些。我应该使用数字键盘中的一个。1)使用逻辑和一致的形式缩进代码行和代码块。缩进的目的是使代码的流程更易于遵循!2) 请对代码和代码片段、结构化文档(如HTML/XML或输入/输出)使用代码格式。为此,选择文本并单击邮件发布/编辑表单顶部的{}
按钮。1)使用逻辑一致的代码行和代码块缩进形式。缩进的目的是使代码的流程更易于遵循!2) 请对代码和代码片段、结构化文档(如HTML/XML或输入/输出)使用代码格式。为此,请选择文本并单击邮件发布/编辑表单顶部的{}
按钮。