我想做的程序是这样的,一个窗体JFrame,上面有一个按钮,点击这个按钮时,这个窗体消失,显示第二个窗体。当第二个窗体关闭(是点右上的X关闭,不是说用代码中的一行命令关闭)会返回第一个窗体。
有四个小问题1、在哪里写点击按钮需要执行的代码?2、如何让第一个窗体重新显示出来?我用的是System.exit(0);关闭的它。3、如何用一行代码去做关闭窗口的操作?是上面这句话吗?4、请问文本框应该如何创建?是继承JTextField吗?谢谢大家了。都是这个程序出的问题初学GUI,疑惑多多啊

解决方案 »

  1.   

    不给分啊!算了!给你弄个SWT的代码吧!
    第一个窗口的:
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.events.SelectionListener;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;public class Form_one { private Text text;

    final Shell shell = new Shell();

    public static void main(String[] args) {
    try {
    Form_one window = new Form_one();
    window.open();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Open the window
     */
    public void open() {
    final Display display = Display.getDefault();

    //设置窗口的坐标:
    shell.setLocation(200, 200);

    //设置窗口的大小
    shell.setSize(500, 375);

    //设置窗口的标题
    shell.setText("第一个窗口");

    //打开窗口
    shell.open();

    //放置一个文本框
    text = new Text(shell, SWT.BORDER);
    text.setBounds(120, 70, 285, 60); //放置一个按钮
    final Button button = new Button(shell, SWT.NONE);
    //为这个按钮添加单击事件监听。
    button.addSelectionListener(new SelectionListener(){ public void widgetSelected(SelectionEvent arg0) {
    // 这个是鼠标单击的方法。
    try {

    Form_two form_two = new Form_two();

    //把第一页中的Text里的内容,放到第二页中的Text里
    form_two.text.setText(text.getText());

    //关闭第一个窗口
    shell.close();

    //打开第二个窗口
    form_two.open();

    } catch (Exception e) {
    e.printStackTrace();
    }
    } public void widgetDefaultSelected(SelectionEvent arg0) {
    // 这个是在文本框中输入值以后,回车所激发的方法。

    }

    });
    button.setText("下一窗口");
    button.setBounds(190, 245, 160, 45);


    shell.layout();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    }}第二个窗口的:import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.events.SelectionListener;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;public class Form_two {

    public Text text;

    final Shell shell = new Shell();


    public Form_two(){
    text = new Text(shell, SWT.BORDER);
    }

    public static void main(String[] args) {
    try {
    Form_two window = new Form_two();
    window.open();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Open the window
     */
    public void open() {
    final Display display = Display.getDefault(); //设置窗口的坐标:
    shell.setLocation(200, 200);

    //设置窗口的大小
    shell.setSize(500, 375);

    //设置窗口的标题
    shell.setText("第二个窗口");

    //打开窗口
    shell.open(); //放置一个文本框
    text.setBounds(120, 70, 285, 60); //放置一个按钮
    final Button button = new Button(shell, SWT.NONE);
    //为这个按钮添加单击事件监听。
    button.addSelectionListener(new SelectionListener(){ public void widgetSelected(SelectionEvent arg0) {
    // 这个是鼠标单击的方法。
    try {

    Form_one form_one = new Form_one();

    //关闭第二个窗口
    shell.close();

    //打开第一个窗口
    form_one.open();

    } catch (Exception e) {
    e.printStackTrace();
    }
    } public void widgetDefaultSelected(SelectionEvent arg0) {
    // 这个是在文本框中输入值以后,回车所激发的方法。

    }

    });
    button.setText("上一窗口");
    button.setBounds(190, 245, 160, 45);
    shell.layout();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    }}
      

  2.   

    下面是swing实现:
    package org.anthrax;import java.awt.event.WindowAdapter;
    import java.awt.event.WindowListener;
    import javax.swing.JFrame;/**
     *
     * @author  Anthrax
     */
    public class Frame1 extends javax.swing.JFrame {
        
        /** Creates new form Frame1 */
        public Frame1() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">                          
        private void initComponents() {
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jButton1.setText("Frame2");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });        jButton2.setText("Exit");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(319, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(232, Short.MAX_VALUE)
                    .add(jButton2)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jButton1)
                    .addContainerGap())
            );
            pack();
        }// </editor-fold>                        
        
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            this.dispose();
            JFrame frame2 =new JFrame();
            frame2.setVisible(true);
            frame2.setSize(W_WIDTH,W_HEIGHT);
            frame2.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    formWindowClosing(evt);
                }
            });
            
            
        }                                        
        
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.exit(0);
        }                                        
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Frame1().setVisible(true);
                }
            });
        }
        
        void formWindowClosing(java.awt.event.WindowEvent evt){
            dispose();
            new Frame1().setVisible(true);
        }
        // 变量声明 - 不进行修改                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        // 变量声明结束                   
        
        private int W_HEIGHT = 350;
        private int W_WIDTH = 400;
        
        
        
    }