Java 使用多个帧显示文本字段数据

Java 使用多个帧显示文本字段数据,java,frames,Java,Frames,我是java的新手,在构建gui时遇到了一个障碍……我需要做的是运行shipmentApp程序,该程序显示我的EnterShipInfo框架,其中有几个文本字段可输入数据,还有一个enter按钮,单击该按钮可将输入的数据传递到我的ShipmentFrame。最初,当框架弹出时,应该有三个标签,其中只有一个“标签”和一个显示按钮,然后当您单击“显示”按钮时,文本字段中的数据会在三个标签中弹出,以及一些其他单词组成一个短语 //ShipmentFrame代码从这里开始 private

我是java的新手,在构建gui时遇到了一个障碍……我需要做的是运行shipmentApp程序,该程序显示我的EnterShipInfo框架,其中有几个文本字段可输入数据,还有一个enter按钮,单击该按钮可将输入的数据传递到我的ShipmentFrame。最初,当框架弹出时,应该有三个标签,其中只有一个“标签”和一个显示按钮,然后当您单击“显示”按钮时,文本字段中的数据会在三个标签中弹出,以及一些其他单词组成一个短语

//ShipmentFrame代码从这里开始

        private static final long serialVersionUID = 1L;
        private Label headerLbl = null;
        private Button displayButton = null;
        private Button Exit = null;
        private Label shipLbl = null;
        private Label empLbl = null;
        private Label dateTimeLbl = null;
        private Shipment s;
        private Shipment sf;



        public ShipmentFrame(Shipment ship){
            super();
            this.s = ship;
            initialize();
            s = ship;



        }


        public void actionPerformed(ActionEvent e) {


            shipLbl.setText("Shipment number " + s.getShipmentNum() + 
                    " was received from "  + s.getSupplierName());
            empLbl.setText("By employee number " + s.getEmployeeNum());
            dateTimeLbl.setText("On " + s.getRevDate() + " at " +      s.getRevTime());

        }
//这是我在EnterShipInfo中的代码

        private static final long serialVersionUID = 1L;
        private Label headerLbl = null;
        private Button displayButton = null;
        private Button Exit = null;
        private Label dateLbl = null;
        private Label timeLbl = null;
        private Label suplLbl = null;
        private Shipment s;
        private Label shipNumLbl = null;
        private Label empNumLbl = null;
        private TextField empNumTF = null;
        private TextField shipNumTF = null;
        private TextField dateTF = null;
        private TextField timeTF = null;
        private TextField supplTF = null;

        Shipment ship = new Shipment (" ", " ", " ", " ", " "); 

        //  @jve:decl-index=0:
        public EnterShipInfo(){
            super();

    //      this.s = ship;
            initialize();
        }
//这是错误的部分

        public void actionPerformed(ActionEvent e) {
            String employeeNum = empNumTF.getText();
            String shipmentNum = shipNumTF.getText();
            String revDate = dateTF.getText();
            String revTime = timeTF.getText();
            String supplierName = supplTF.getText();
            ShipmentFrame sf = new ShipmentFrame(null);


        }
//下面是我一直在寻找的…也许有人知道更有效的方法来实现这一点

public void actionPerformed(ActionEvent e) {
    ship.setEmployeeNum(empNumTF.getText());

    ship.setShipmentNum( shipNumTF.getText());
    ship.setRevDate( dateTF.getText());
    ship.setRevTime(timeTF.getText());
    ship.setSupplierName( supplTF.getText());
    ShipmentFrame sf = new ShipmentFrame(ship);


}
编辑

下面是在JFrame中显示组件的一般过程:

1-设置JFrame(布局、边框等):

2-初始化组件(在本例中,两个JTextFields和一个JButton):

3-将组件添加到JFrame的contentPane:

contentPane.add(empNumTF);
contentPane.add(shipNumTF);
contentPane.add(displayButton);
4-包装并使其可见:

frame.pack();
frame.setVisible(true);
然后,JFrame应该与这些组件一起显示


已添加

现在,如果我们想操纵用户在这些文本字段中输入的内容,我们需要为按钮添加一个操作侦听器。为此:

1-通过声明它
实现ActionListener
,使您的一个类成为action listener(如果需要,您可以使用在其中创建JButton的同一个类):

public class EnterShipInfo implements ActionListener{
2-向按钮添加动作侦听对象,如下所示:

displayButton.addActionListener(this);
//using "this" means that the object this JButton was created in is the action listener.
3-向ActionListener添加一个
actionPerformed()
方法(看起来您已经正确地执行了):


现在,特别是对您来说,在
actionPerformed()
方法中,您可能希望像这样处理它:

public void actionPerformed(ActionEvent e){
    if (e.getActionCommand().equals("Display")){ //"Display" is the text on the JButton
        String employeeNum = empNumTF.getText();
        String shipmentNum = shipNumTF.getText();
        String revDate = dateTF.getText();           //These text fields
        String revTime = timeTF.getText();           //not coded in
        String supplierName = supplTF.getText();     //my example
        ShipmentFrame sf = new ShipmentFrame(new Shipment(shipmentNum, supplierName, revDate, revTime, employeeNum)); //I'm just guessing at the order these come in
        sf.setVisible(true);
    }
}
您可能需要查看的一些关键文档:


您在这里遇到的是一个特定的问题还是一个特定的问题?事实上,这是一个特定的问题,直到单击第二帧(shipmentFrame)上的显示按钮为止。当我单击显示按钮时,我当然会出错,所以我认为我犯了两个可能的错误之一,我没有将文本字段中的信息传递给对象,或者在单击“显示”按钮时未能调用它们复制错误并将其粘贴到问题中的另一个代码块中,这样我们就可以看到到底发生了什么。好吧,我可能没有领会你的意思,但是我很确定我已经完成了你在回答中提到的所有事情……我的问题是将输入到文本字段中的信息传递到另一个框架中显示。@philjd11好的,你的问题有点模糊,所以我只介绍了基本知识,但我现在编辑了我的答案,希望它能涵盖你的问题。
displayButton.addActionListener(this);
//using "this" means that the object this JButton was created in is the action listener.
public void actionPerformed(ActionEvent e){
    //insert code to execute whenever your button is clicked.
}
public void actionPerformed(ActionEvent e){
    if (e.getActionCommand().equals("Display")){ //"Display" is the text on the JButton
        String employeeNum = empNumTF.getText();
        String shipmentNum = shipNumTF.getText();
        String revDate = dateTF.getText();           //These text fields
        String revTime = timeTF.getText();           //not coded in
        String supplierName = supplTF.getText();     //my example
        ShipmentFrame sf = new ShipmentFrame(new Shipment(shipmentNum, supplierName, revDate, revTime, employeeNum)); //I'm just guessing at the order these come in
        sf.setVisible(true);
    }
}