用swing做一个小表单。功能如下:
两个框
在一个框中输入内容
点击提交按钮
刚才输入的内容显示在另外一个框中,最好以前输入的内容也保留。
就这么简单,初学者,谢谢!!!

解决方案 »

  1.   


    package swing;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;import javax.swing.WindowConstants;
    import javax.swing.border.BevelBorder;
    import javax.swing.SwingUtilities;
    /**
    * This code was edited or generated using CloudGarden's Jigloo
    * SWT/Swing GUI Builder, which is free for non-commercial
    * use. If Jigloo is being used commercially (ie, by a corporation,
    * company or business for any purpose whatever) then you
    * should purchase a license for each developer using Jigloo.
    * Please visit www.cloudgarden.com for details.
    * Use of Jigloo implies acceptance of these licensing terms.
    * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
    * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
    * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
    */
    public class NewJFrame extends javax.swing.JFrame { {
    //Set Look & Feel
    try {
    javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
    } catch(Exception e) {
    e.printStackTrace();
    }
    } private JTextArea jTextArea1;
    private JButton jButton1;
    private JTextArea jTextArea2;
    private JPanel jPanel1; /**
    * Auto-generated main method to display this JFrame
    */
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    NewJFrame inst = new NewJFrame();
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
    }
    });
    }

    public NewJFrame() {
    super();
    initGUI();
    }

    private void initGUI() {
    try {
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    BorderLayout thisLayout = new BorderLayout();
    getContentPane().setLayout(thisLayout);
    {
    jPanel1 = new JPanel();
    getContentPane().add(jPanel1, BorderLayout.CENTER);
    jPanel1.setPreferredSize(new java.awt.Dimension(452, 96));
    jPanel1.setLayout(null);
    {
    jTextArea2 = new JTextArea();
    jPanel1.add(jTextArea2);
    jTextArea2.setBounds(278, 15, 164, 64);
    jTextArea2.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    }
    {
    jButton1 = new JButton();
    jPanel1.add(jButton1);
    jButton1.setText("\u6dfb\u52a0");
    jButton1.setBounds(188, 33, 59, 25);
    jButton1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });
    }
    {
    jTextArea1 = new JTextArea();
    jPanel1.add(jTextArea1);
    jTextArea1.setBounds(10, 15, 156, 64);
    jTextArea1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    }
    }
    pack();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
    System.out.println("jButton1.actionPerformed, event="+evt);
    //TODO add your code for jButton1.actionPerformed
    String text=jTextArea1.getText();
    if(text!=null){
    jTextArea2.append(text);
    }
    jTextArea1.setText("");
    }}
      

  2.   

    a simple sampleclass MyFrame extends JFrame {
        JTextField tf1 = new JTextField();
        JTextField tf2 = new JTextField();
        JButton btn = new JButton("submit");    public static void main(String[] args) {
            new MyFrame();
        }
        public MyFrame() {
            super("test");
            setSize(400, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container c = getContentPane();
            c.setLayout(null);
            ft1.setBounds(20, 20, 80, 20);
            ft2.setBounds(120, 20, 80, 20);
            btn.setBounds(50, 20, 80, 20);
            c.add(tf1);
            c.add(tf2);
            c.add(btn);
            btn.addActionListener(new ActionListener() {
                tf2.setText(tf1.getText());
            });
            setVisible(true);
        }
    }
      

  3.   

    手写太匆忙,方法都忘了添加了
            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tf2.setText(tf1.getText());
                }
            });
      

  4.   


    import java.awt.Button;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;public class Copy extends Frame { Button b;
    TextField L, R; public void display() {
    Frame f = new Frame("复制");
    f.setSize(300, 150);
    f.setBackground(Color.lightGray);
    f.setLayout(new FlowLayout());
    L = new TextField(10);
    R = new TextField(10);
    b = new Button("复制");
    f.add(L);
    f.add(R);
    f.add(b);
    b.addActionListener(new ActionListener() { @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    R.setText(L.getText());
    }
    });
    f.setVisible(true);
    } public static void main(String[] args) {
    (new Copy()).display();
    }
    }
      

  5.   

    谢谢大家在我的帖子中回复,阿宝的例子让我受益匪浅。我把阿宝的第二个texfield换乘了textArea, 以便我能看到以前的记录。但是有一个问题是我怎么才能给textarea加一个scrollbar呢?现在如果我输入的记录超过textarea的长度,新纪录将看不到。这是我修改过的代码。非常感谢,时间很紧,不是懒的研究,谢谢。import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;class MyFrame extends JFrame {
            JTextField tf1 = new JTextField();
            JTextArea ta = new JTextArea();
            JButton btn = new JButton("submit");        public static void main(String[] args) {
                    new MyFrame();
            }        public MyFrame() {
                    super("test");
                    setSize(400, 300);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    Container c = getContentPane();
                    c.setLayout(null);
                    tf1.setBounds(20, 20, 80, 20);
                    ta.setBounds(120, 20, 80, 200);
                    btn.setBounds(20, 200, 80, 20);
                    c.add(tf1);
                    c.add(ta);
                    c.add(btn);
                    btn.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                    // str = str + "\n" + tf1.getText();
                                    // ta.setText(str);
                                    ta.append(tf1.getText()+ "\n");
                            }
                    });
                    setVisible(true);
            }
    }
      

  6.   

    谢谢大家,自己解决了。import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;class MyFrame extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JTextField tf1 = new JTextField();
    JTextArea ta = new JTextArea();
    JButton btn = new JButton("submit");
    public static void main(String[] args) {
    new MyFrame();
    } public MyFrame() {
    super("test");
    setSize(400, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = getContentPane();
    c.setLayout(null);
    ta.setLineWrap(true);    //设置自动换行,自动换行则不会出现横向的滚动条
    ta.setEditable(true);    //设置可编辑
    JScrollPane jsp = new JScrollPane(ta);
    tf1.setBounds(20, 20, 80, 20);
    ta.setBounds(120, 20, 80, 200);
    jsp.setBounds(120, 15, 80, 200);
    btn.setBounds(10, 200, 80, 20);
    //jp.add(jsp);
    //c.add(jp);
    c.add(jsp);
    c.add(tf1);
    //c.add(ta);
    c.add(btn);
    btn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    // str = str + "\n" + tf1.getText();
    // ta.setText(str);
    ta.append(tf1.getText()+ "\n");
    }
    });
    setVisible(true);
    }
    }