您可以使用多种方法来完成此操作...
你可以...
使类,实例变量可用于numGenerator
直接访问...
public class Main{
public static void main(String[] args){
new Main();
}
// This variable will be visible to the inner class numGenerator
private JLabel output;
public Main() {
JFrame mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);
Panel panel = new Panel();
mainFrame.getContentPane().add(panel);
output = new JLabel("This is where the result from the num variable in the numGenerator class would go");
panel.add(output);
JButton numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new numGenerator());
}
public class numGenerator implements ActionListener{
public void actionPerformed(ActionEvent e){
Random dice = new Random();
int num = dice.nextInt(3);
output.setText(Integer.toString(num));
}
}
}
这将您的操作与您的标签紧密结合在一起,使代码的可重用性降低......
你可以...
将要更改的标签的引用传递给numGenerator
...
public class Main{
public static void main(String[] args){
new Main();
}
public Main() {
JFrame mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);
Panel panel = new Panel();
mainFrame.getContentPane().add(panel);
JLabel output = new JLabel("This is where the result from the num variable in the numGenerator class would go");
panel.add(output);
JButton numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new numGenerator(output));
}
public class numGenerator implements ActionListener{
private JLabel label;
public numGenerator(JLabel label) {
this.label = label;
}
public void actionPerformed(ActionEvent e){
Random dice = new Random();
int num = dice.nextInt(3);
if (label != null) {
label.setText(Integer.toString(num));
}
}
}
}
这使得numGenerator
更可重用,因为它不依赖于单个实例JLabel
你可以...
使用观察者风格的模式,可以告诉一些感兴趣的人正在生成一个新号码......
public class Main{
public static void main(String[] args){
new Main();
}
public Main() {
JFrame mainFrame = new JFrame("Experiment");
mainFrame.setSize(500,500);
mainFrame.setVisible(true);
Panel panel = new Panel();
mainFrame.getContentPane().add(panel);
final JLabel output = new JLabel("This is where the result from the num variable in the numGenerator class would go");
panel.add(output);
JButton numGenerator = new JButton("Generate Number");
panel.add(numGenerator);
numGenerator.addActionListener(new numGenerator(new NumberGeneratorListener() {
public void numberGenerated(int number) {
output.setText(Integer.toString(number));
}
}));
}
public interface NumberGeneratorListener {
public void numberGenerated(int number);
}
public class numGenerator implements ActionListener{
private NumberGeneratorListener listener;
public numGenerator(NumberGeneratorListener listener) {
this.listener = listener;
}
public void actionPerformed(ActionEvent e){
Random dice = new Random();
int num = dice.nextInt(3);
if (listener != null) {
listener.numberGenerated(num);
}
}
}
}
这不仅将numGenerator
代码与其余代码分离,因为它不依赖于代码的任何其他部分,它使得它非常可重用,因为它不关心数字的去向或如何使用,这取决于观察者/听众来决定...
旁注...
您可能还想通读...