【发布时间】:2016-03-08 17:59:34
【问题描述】:
我希望我的意思是我的话。 我有这样的课:
public class MainClass extends JFrame{
private JLabel mainlabel;
private SampleClass sample=new SampleCalss();
public void intital(){
mainlabel=new JLabel("Main");
sample.setMethod(getLabel());
//
//some code
//
add(mainlabel);
}
public static void main(){
intital();
}
public JLabel getLabel(){
return mainlabel;
}
}
和其他类似的类:
public class SampleClass extends JFrame{
private JButton button=new JButton("Change");
private JLabel sLabel;
public SampleClass(){
//somecode
//
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sLabel.setText("Sample text set");
}
});
add(jButton);
}
public void setMethod(JLabbel l){
sLabel=l;
}
}
这是访问mainlabel 并从其他类更改其值的真正方法(在此类SampleClass 的示例代码中)是否有更好或更正确的解决方案?
请注意,MainClass 是具有 main 方法的类。
【问题讨论】:
-
反射可以做到,但它会是一个黑客