日期:2014-05-20 浏览次数:20882 次
package com.zf.ui; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; /** * 主窗口 * @author zhoufeng * */ public class MainFrame { JFrame frame = null ; JTextArea text = null; JButton button = null; public void init(){ frame = new JFrame("主窗口"); frame.setBounds(500, 250, 500, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); text = new JTextArea(); text.setSize(100, 50 ); text.setText("这里将会显示时间!"); frame.add(text , BorderLayout.NORTH); button = new JButton("打开子窗口"); frame.add(button , BorderLayout.SOUTH); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("点击了。"); new ChildFrame(MainFrame.this); } }); } public static void main(String[] args) { new MainFrame().init(); } } /** * 子窗口 * @author zhoufeng * */ class ChildFrame{ MainFrame mainFrame = null; JFrame frame = null; JButton button = null; public ChildFrame(final MainFrame mainFrame){ frame = new JFrame("子窗口"); frame.setBounds(500, 250, 300, 150); frame.setVisible(true); button = new JButton("为主窗口中的文本域赋值"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { mainFrame.text.setText(new Date().toLocaleString()); frame.setVisible(false); } }); frame.add(button , BorderLayout.NORTH); } }
------解决方案--------------------