[英]How would I use an ActionListener on a Button to change JLabels in a different class?
I am currently doing a group assignment.One of my tasks is to use an ActionListener to add "data" to JLabels in a different class. 我当前正在进行组分配。我的任务之一是使用ActionListener将“数据”添加到其他类的JLabel中。
Another person in my group was responsible for coding the GUI, however he did a very bad job with it making my task a bit more difficult. 我们小组中的另一个人负责GUI的编码,但是他做得很糟糕,这使我的工作更加困难。 We were taught to practice MVC standards. 我们被教导实践MVC标准。 However I don't think I could do that properly now. 但是我认为我现在不能正确地做到这一点。 The code for the GUI is a single huge chunk of code over a thousand lines long all contained in 1 method making it hard to tidy up without wasting too much time. GUI的代码是一千多行代码中的一大块代码,所有这些都包含在一种方法中,因此很难在不浪费太多时间的情况下进行整理。
I have used ActionListeners before, but only in practice and when they are all in one class. 我以前使用过ActionListeners,但仅在实践中以及它们都在一个类中时才使用。
What I am trying to achieve is when a button is clicked (from the GUI class), the actionPerformed method will call methods from an external JAR file and change the JLabels from showing the text Default to the "data" obtained from the called methods. 我想要实现的是,当单击一个按钮(从GUI类)时,actionPerformed方法将从外部JAR文件中调用方法,并将JLabel从显示文本Default更改为从被调用方法获得的“数据”。
At the moment I have 6 JLabels created with a for loop, 目前,我通过for循环创建了6个JLabel,
int totalLbls = 6;
JLabel lbl[] = new JLabel[totalLbls];
for (int j = 0; j <totalLbls;j++){
lbl[j] = new JLabel("default");
pa2.add(lbl[j]);
Firstly, how would I change each label? 首先,我将如何更改每个标签? Initially I thought about doing it manually and mapping it individually 6 times like so, 最初,我考虑过手动进行并分别映射6次,就像这样,
JLabel lb1 = new JLabel(jarname.getData());
JLabel lb2 = new JLabel(jarname.getMoreData());
.
.
.
JLabel lb6 = new JLabel(jarname.getOtherData());
The methods that are called return String values. 称为方法的方法返回String值。 The methods and jar file name used are just placeholder names. 使用的方法和jar文件名仅是占位符名称。
Secondly, how would I go about implementing the ActionListener? 其次,我将如何实现ActionListener? I've seen examples but they often ask that we apply the MVC standards. 我看过示例,但他们经常要求我们应用MVC标准。 Which means the the Controller calling both the View and Model. 这意味着控制器同时调用视图和模型。 And in my case there is only a really messy View. 就我而言,只有一个非常混乱的视图。
One way to go about doing it if you are not implementing ActionListener to the main class is to make one specific for that button. 如果您没有在主类中实现ActionListener,那么执行此操作的一种方法是为该按钮创建一个特定的按钮。
yourButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
/* Set your labels here.
OtherClass.lb1.setText(jarname.getData());
OtherClass.lb2.setText(jarname.getMoreData());
.
.
.
OtherClass.lb6.setText(jarname.getOtherData());
*/
}
});
Pass the instance of the class that knows about the JLabels into the class that has the action listener. 将知道JLabel的类的实例传递到具有操作侦听器的类中。 For example (simplified access for example): 例如(例如简化访问):
public class LabelContainer { // May well extend JPanel or JFrame
public JLabel label1 = new JLabel("First");
// Plus five more..
}
public class LabelChanger implements ActionListener { // May well extend JButton
private LabelContainer labels;
public LabelChanger(LabelContainer labels) {
this.labels = labels;
someWidget.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
labels.label1.setText("Changed");
// five more...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.