【问题标题】:Display Text field data using multiple frames使用多帧显示文本字段数据
【发布时间】:2016-03-09 06:03:19
【问题描述】:

我是 java 的菜鸟,我在构建我的 gui 时遇到了障碍......我需要做的是运行我的 shippingApp 程序,该程序显示我的 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);


}

【问题讨论】:

  • 您是否有特定的问题或遇到的特定问题?
  • 事实上有一个特定的问题,直到点击第二帧(shipmentFrame)上的显示按钮......当我点击显示按钮时,我当然得到错误,所以我的想法是我犯了两个可能的错误之一,要么我没有将文本字段中的信息传递给对象,要么我在单击显示按钮时未能调用它们
  • 将错误复制并粘贴到您问题中的另一个代码块中,以便我们查看到底发生了什么。

标签: java frames


【解决方案1】:

编辑

以下是在 JFrame 中显示组件的一般流程:

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 - 初始化您的组件(在这种情况下,两个 JTextField 和一个 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 应该与这些组件一起显示。


添加

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

1 - 通过声明 implements ActionListener:

将您的一个类设为 动作侦听器(如果需要,您可以使用创建 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 - 将 actionPerformed() 方法添加到您的 ActionListener(您似乎已经正确完成了):

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);
    }
}

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

【讨论】:

  • 好吧,我可能错过了你的意思,但我很确定我已经完成了你在回答中提到的所有事情......我的问题是获取输入到文本字段中的信息并传递它们显示在我的另一个框架中。
  • @philjd11 好的,你的问题有点模糊,所以我只介绍了基础知识,但我现在已经编辑了我的答案,希望它能涵盖你的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 2014-10-26
  • 2012-01-02
相关资源
最近更新 更多