只有一个JTextField将输出或显示在框架上

时间:2014-10-01 05:16:56

标签: java swing jtextfield jtextarea

所以当我点击添加菜单时,我正在尝试填写表格。因此,一旦我点击添加,它将显示一个填充表格,我必须填写它,以便我可以使用我制作的jtextarea在另一个窗口或框架上查看它。我的问题是,一旦我在填充表单上输入数据,输出将只显示jtextarea中的一个文本字段。我必须输出我放置在jtextfield中的所有输入,并将其输出为另一个jtextarea中的输入。另外,我必须将JLabel放在jtextarea输出上。例如,我有FirstName标签,我将输入Jtext“我的名字”然后它应该在另一个窗口输出作为名字:MyName以及要输入的其他要求。希望我能得到一些帮助。谢谢。到目前为止,这是我的工作:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Sample extends JFrame implements ActionListener {
    JTextField r = new JTextField();
    JDesktopPane desk = new JDesktopPane();
    JMenuBar bar = new JMenuBar();
    JMenu mAdd = new JMenu("Add");
    JMenu mView = new JMenu("View");
    JMenuItem miNew =new JMenuItem("New");
    JMenuItem miExit = new JMenuItem("Exit");
    JMenuItem miStudInfo = new JMenuItem("Student Info");
    JMenuItem miEducInfo = new JMenuItem("Educational Info");

    JLabel personalInfo = new JLabel("Personal Information");
    JLabel space1 = new JLabel("");
    JLabel space2 = new JLabel("");
    JLabel studnum = new JLabel("Student #: ");
    JLabel space3 = new JLabel("");
    JTextField jtxstudnum = new JTextField();
    JLabel fname = new JLabel("First Name: ");
    JLabel space4 = new JLabel("");
    JTextField jtxfname = new JTextField();
    JLabel midname = new JLabel("Middle Name: ");
    JLabel space5 = new JLabel("");
    JTextField jtxmidname = new JTextField();
    JLabel lname = new JLabel("Last Name: ");
    JLabel space6 = new JLabel("");
    JTextField jtxlname = new JTextField();
    JLabel address = new JLabel("Address: ");
    JLabel space7 = new JLabel("");
    JTextField jtxaddress = new JTextField();

    JLabel educQualify = new JLabel("Educational Qualification");
    JLabel space8 = new JLabel("");
    JLabel space9 = new JLabel("");
    JLabel preschool = new JLabel("Preschool: ");
    JTextField jtxpre= new JTextField();
    JTextField jtxpreyear = new JTextField();
    JLabel elementary = new JLabel("Elementary: ");
    JTextField jtxelem = new JTextField();
    JTextField jtxelemyear = new JTextField();
    JLabel highschool = new JLabel("HighSchool: ");
    JTextField jtxhigh = new JTextField();
    JTextField jtxhighyear = new JTextField();
    JLabel college = new JLabel("College: ");
    JTextField jtxcol = new JTextField();
    JTextField jtxcolyear = new JTextField();
    JLabel postgrad = new JLabel("PostGrad: ");
    JTextField jtxpost = new JTextField();
    JTextField jtxpostyear = new JTextField();

    JLabel space10 = new JLabel("");
    JButton jbtnView = new JButton("View");
    JButton jbtnReset = new JButton("Reset");

JPopupMenu jpExit = new JPopupMenu("Select an option");
JButton yes = new JButton("Yes");
JButton no = new JButton("No");
JButton cancel = new JButton("Cancel");
JLabel message;

JTabbedPane tabbedPane;
//JTabbedPane tabStudInfo = new JTabbePane("Student Info");
//JTabbedPane tabEducInfo = new JTabbePane("Educational Info");
JPanel p1;
JPanel p2;
JPanel p3;

Container con = getContentPane();


public Sample() {
    setTitle("Student Information");
    setSize(900,700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    con.add(desk);
    setJMenuBar(bar);

    bar.add(mAdd);
    bar.add(mView);
    mAdd.add(miNew);
    mAdd.add(miExit);
    mView.add(miStudInfo);
    mView.add(miEducInfo);

    miNew.addActionListener(this);
    mAdd.add(miExit);
    miExit.addActionListener(this);
    miStudInfo.addActionListener(this);
    miEducInfo.addActionListener(this);


    setVisible(true);
}

public void actionPerformed(ActionEvent e){
        if(e.getSource()==miNew){
        JInternalFrame nf = new JInternalFrame("Add -> New", true, true, true, true );
        nf.setSize(450,500);
        nf.setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);

        Container con = nf.getContentPane();
        con.setLayout(new GridLayout(13,2));
        //con.setLayout(new GridLayout(6,2));
        con.add(personalInfo);
        con.add(space1);
        con.add(space2);
        //personalInfo.setBounds(2,2,200,30);
        //x=100, y =50, width=300, height=30
        con.add(studnum);
        con.add(space3);
        con.add(jtxstudnum);
        con.add(fname);
        con.add(space4);
        con.add(jtxfname);
        con.add(midname);
        con.add(space5);
        con.add(jtxmidname);
        con.add(lname);
        con.add(space6);
        con.add(jtxlname);
        con.add(address);
        con.add(space7);
        con.add(jtxaddress);

    //  con.setLayout(new GridLayout(6,3));
        con.add(educQualify);
        con.add(space8);
        con.add(space9);
        con.add(preschool);
        con.add(jtxpre);
        con.add(jtxpreyear);
        con.add(elementary);
        con.add(jtxelem);
        con.add(jtxelemyear);
        con.add(highschool);
        con.add(jtxhigh);
        con.add(jtxhighyear);
        con.add(college);
        con.add(jtxcol);
        con.add(jtxcolyear);
        con.add(postgrad);
        con.add(jtxpost);
        con.add(jtxpostyear);

    //  con.setLayout(new GridLayout(1,2));
        con.add(space10);
        con.add(jbtnView,BorderLayout.SOUTH);
        con.add(jbtnReset,BorderLayout.SOUTH);

        //r.setEditable(false);
        jbtnView.addActionListener(this);       
        jbtnReset.addActionListener(this);

        nf.setVisible(true);

        desk.add(nf);

    }

    else if(e.getSource()==jbtnView){
        r.setText(jtxstudnum.getText());

        JInternalFrame showNF =  new JInternalFrame("Show Result", true, true, true, true);
        showNF.setSize(400,500);
        showNF.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
        showNF.setLocation(300,0);

        Container showNFcon = showNF.getContentPane();
        showNFcon.setLayout(new FlowLayout());
        JTabbedPane pane = new JTabbedPane();

        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        JTextArea display = new JTextArea(20,30);
        p1.add(display);
        display.setText(jtxstudnum.getText()); display.setText(jtxfname.getText());


        display.setText(jtxfname.getText());
        display.setText(jtxmidname.getText());
        display.setText(jtxlname.getText());
        display.setText(jtxaddress.getText());

        pane.add("Student Info", p1);


        JPanel p3 = new JPanel();
        p3.setLayout(new FlowLayout());
        JTextArea display2 = new JTextArea(20,30);
        p3.add(display2);
        display2.setText(jtxpre.getText());
        display2.setText(jtxpreyear.getText());
        display2.setText(jtxelem.getText());
        display2.setText(jtxelemyear.getText());
        display2.setText(jtxhigh.getText());
        display2.setText(jtxhighyear.getText());
        display2.setText(jtxcol.getText());
        display2.setText(jtxcolyear.getText());
        display2.setText(jtxpost.getText());
        display2.setText(jtxpostyear.getText());
        pane.add("Educational Info", p3);


        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());

        p2.add(pane);
        showNFcon.add(p2);


        showNF.setVisible(true);
        desk.add(showNF);
    }
        //else if(e.getSource()==b){
    //  r.setText(t.getText());
//  }
 /*
        else if(e.getSource()==miExit)
        //  con.add(jpExit);

        int response = JOptionPane.showConfirmDialog(null,"Exit?","Select an option",JOptionPane.QUESTION_MESSAGE);
        switch(response){
            case JOptionPane.YES_OPTION:
                message.setText("Yes");
                System.exit(0);
                break;
            case JOptionPane.NO_OPTION:
                message.setText("No");
                break;
            case JOptionPane.CANCEL_OPTION:
                message.setText("Cancel");
                break;

        }*/
        //System.exit(0);

}
/*
public void createPage1(){
    panel1 = new JPanel();
//  panel1=setLayout(new BorderLayout());


}

public void createPage2(){
    panel2 = new JPanel();
//  panel2=setLayout(new BorderLayout());


}*/


public static void main(String[] args) {
    Sample frame = new Sample();
}

}

1 个答案:

答案 0 :(得分:3)

你的问题就在这里......

display.setText(jtxstudnum.getText());
display.setText(jtxfname.getText());

display.setText(jtxfname.getText());
display.setText(jtxmidname.getText());
display.setText(jtxlname.getText());
display.setText(jtxaddress.getText());

setText这样做,它说,"丢弃您当前设置文字的文字我将您作为您唯一的内容" ...

相反,您希望使用JTextArea#append代替

display.append(jtxstudnum.getText());
display.append(jtxfname.getText());

display.append(jtxfname.getText());
display.append(jtxmidname.getText());
display.append(jtxlname.getText());
display.append(jtxaddress.getText());

它说的是什么,它将提供的文本附加到现有内容

仔细查看How to Use Text Areas了解更多详情