日期:2014-05-20 浏览次数:20735 次
import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; public class Test extends JFrame{ JPanel panel_1, panel_2; JButton button1, button2; JLabel label; JTextArea te; String a = "in button_1_1_1"; String b = "in button_2_2_2"; static String returnText; //定义接收子窗口返回的数据为静态类型 public Test(){ setTitle("test"); setBounds(200, 100, 500, 500); Container c = getContentPane(); c.setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); button1 = new JButton("显示子窗口返回的数据"); button2 = new JButton("打开子窗口"); te = new JTextArea(20,20); label = new JLabel(); panel_1 = new JPanel(); panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 10)); //设置panel_1的网格 panel_2 = new JPanel(); panel_1.add(button1); panel_1.add(button2); panel_2.add(label); c.add(panel_1, BorderLayout.NORTH); c.add(panel_2, BorderLayout.CENTER); //------------------------------------- button1.addActionListener(new ActionListener(){ //加载button1按钮的事件 public void actionPerformed(ActionEvent e){ label.setText(getRt()); //--在窗体上显示子窗口返回的数据 } }); button2.addActionListener(new ActionListener(){ //加载button2按钮的事件 public void actionPerformed(ActionEvent e){ T frame2 = new T(); frame2.setVisible(true); //打开子窗口 } }); } public String getRt() { //get方法 return returnText; } public void setRt(String rt) { //set方法 this.returnText = rt; } public static void main(String []args){ Test frame = new Test(); frame.setVisible(true); } } 子窗口: