在java swing 中,怎样获取按钮上的内容,并显示在另一个窗体上

如题所述

在A窗口调用 B.setcontent(button.getlabel()); 在B窗口里面的setcontent(string)方法里面设置button.setlabel(string)的方法。追问

能不能具体点代码?谢谢

追答

public class FrameWork extends JFrame{

FrameWork1 fw = new FrameWork1();
public FrameWork() {
this.setName("Log Tools");
this.setSize(295, 260);
final Container con = getContentPane();
con.setLayout(null);
final JButton addstylebut = new JButton("11");
addstylebut.setBounds(0, 15, 100, 20);
addstylebut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fw.setLabel(addstylebut.getLabel());
}
});
this.add(addstylebut);
this.setVisible(true);
this.repaint();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String args[]) {
FrameWork fw = new FrameWork();
}
}

public class FrameWork1 extends JFrame{
JButton addstylebut = new JButton("Fr");
public FrameWork1() {
this.setName("Log Tools");
this.setSize(295, 260);
final Container con = getContentPane();
con.setLayout(null);
//final JButton addstylebut = new JButton("22");
addstylebut.setBounds(0, 15, 100, 20);
this.add(addstylebut);
this.setVisible(true);
this.repaint();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setLabel(String label){
addstylebut.setLabel(label);

this.repaint();
}

}

温馨提示:答案为网友推荐,仅供参考
相似回答