我希望我说的是我的话。我有这样的课:
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方法的类。