使用多个框架显示文本字段数据

时间:2016-03-09 06:03:19

标签: java frames

我是java的菜鸟,我遇到了一个问题...我需要做的是运行我的shipmentApp程序,它显示我的EnterShipInfo框架,其中有几个文本字段用于输入数据和一个输入按钮,当clicked将输入的数据传递给我的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);


}

1 个答案:

答案 0 :(得分:0)

修改

以下是在JFrame中显示Component的一般过程:

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

JFrame frame = new JFrame("Frame Title");
JPanel contentPane = ((JPanel) frame.getContentPane());

contentPane.setLayout(new GridLayout(4,1)); //use whatever kind of layout you need
contentPane.setBorder(new EmptyBorder(10,10,10,10)); //use whatever kind of border you need

2 - 初始化你的组件(在这种情况下,两个JTextFields和一个JButton):

JTextField empNumTF = new JTextField("Initial text in field");
JTextField shipNumTF = new JTextField("Initial text in field");
JButton displayButton = new JButton("Display");

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

contentPane.add(empNumTF);
contentPane.add(shipNumTF);
contentPane.add(displayButton);

4 - 打包并显示:

frame.pack();
frame.setVisible(true);

然后,您的JFrame应与这些组件一起显示。

<强> ADDED

现在,如果我们想要操纵用户输入的文本字段,我们需要为按钮添加一个动作监听器。要做到这一点:

1 - 将一个类设为动作侦听器(如果需要,可以使用与创建JButton相同的类),声明它为implements ActionListener

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()方法(因为您似乎已经正确完成):

public void actionPerformed(ActionEvent e){
    //insert code to execute whenever your button is clicked.
}

所以现在,特别是你,在你的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);
    }
}

您可能需要查看一些关键文档: