简体   繁体   English

如何将数据从1个JFrame形式的文本字段传递到另一个JFrame形式的JList?

[英]How can I pass data from text-fields in 1 JFrame Form to a JList in another JFrame Form?

Working on a GUI that is for allowing Customers to register their information on 1 GUI (Button click to add new customer) whereby after clicking the add button to register their information it will collect up all their data (forename, surname, address Line 1/2, city & postcode) and then upload it onto a JList in the previous GUI in which it displays the customer's first & last name. 在一个允许客户在1个GUI上注册其信息的GUI(单击按钮以添加新客户)上进行操作,从而在单击“添加”按钮以注册其信息之后,它将收集其所有数据(姓,名,地址第1 / 2,城市和邮政编码),然后将其上载到上一个GUI的JList中,其中显示客户的名字和姓氏。 From which a user can then select that particular customer and click to update the customer's details in an new GUI. 然后,用户可以从中选择该特定客户,然后单击以在新的GUI中更新该客户的详细信息。

The problem I'm having is trying to figure out how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI with only the first & last name being displayed. 我遇到的问题是试图弄清楚如何从第二个GUI中的文本字段传递所有客户的数据,然后将它们传输到第一个GUI中的JList中,并且只显示名字和姓氏。 The 2nd problem I'm also facing is how to then open up another GUI which will then reverse the process when that particular customer has been selected so that changes can be made to their details. 我还面临的第二个问题是如何打开另一个GUI,当选择了该特定客户时,它将逆转该过程,以便可以更改其详细信息。

In my Person class in the data-model package I have got the method getFullName which goes as follows: 在数据模型包的Person类中,我有方法getFullName,其方法如下:

public String getFullName()
{
    String result = this.forename + " " + this.surname;
    return result;
}

Is does anyone know how or could show me how I would be able to apply a solution to these problems? 有谁知道如何或者可以向我展示如何解决这些问题?

I have tried experiementing with the getFullName method within the 2nd GUI upon pressing btnAdd, as shown below: 我尝试在按btnAdd时在第二个GUI中使用getFullName方法进行实验,如下所示:

public void addItem()
{
    String result = this.forename + " " + this.surname;
    return result;
}

But alas I have had no luck. 但是a,我没有运气。

Regarding your questions: 关于您的问题:

The problem I'm having is trying to figure out how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI... 我遇到的问题是试图弄清楚如何从第二个GUI的文本字段传递所有客户的数据,然后将它们传输到第一个GUI的JList中。

There are usually two problems in this situation -- 在这种情况下通常有两个问题-

  1. How to get the information held by the fields of add new customer dialog window (and yes, this should be a dialog window of some form, either a JDialog or a JOptionPane) into the parent window object, and 如何获取由添加新客户对话框窗口(是的,这应该是某种形式的对话框窗口,JDialog或JOptionPane)的字段保存的信息到父窗口对象中,以及
  2. How to be notified exactly when this transfer of information should take place, and this is usually the more problematic of the two. 如何准确地通知何时应该进行信息传递,而这通常是两者中比较棘手的问题。

Again the first issue is usually easy to solve, since it is nothing more than a specific version of the more general problem of how to pass information between two classes. 同样,第一个问题通常很容易解决,因为它只不过是如何在两个类之间传递信息这一更普遍问题的特定版本。 The most simple way to solve it is to have the parent window class call getter methods in the dialog window class to extract its state. 解决它的最简单方法是在对话框窗口类中使父窗口类调用getter方法以提取其状态。 For instance, the dialog window could have public getter methods for the data held in each JTextField, which the calling parent window could call, or the dialog class could have a getCustomer() method within which it gathers all the information held by its input fields, creates a Customer object, and then returns that object. 例如,对话框窗口可以具有每个JTextField中保存的数据的公共getter方法,调用父窗口可以调用该方法,或者对话框类可以具有getCustomer()方法,在其中收集其输入字段保留的所有信息。 ,创建一个Customer对象,然后返回该对象。

Perhaps a better and more robust way of transferring your data is to structure your program as a Model-View-Control or MVC type program, have your add new customer dialog window update the model when its submit button is pressed, and have the parent main window be notified by the model that its data has been changed, and so it knows that it must update its view of the data. 传输数据的一种更好,更健壮的方法可能是将程序构造为Model-View-Control或MVC类型的程序,让添加新的客户对话框窗口在按下其提交按钮时更新模型,并让其父主由模型通知窗口其数据已更改,因此它知道必须更新其数据视图。 This may be overkill for your program, and so I am not going to push this recommendation at this time , but in the future, when you are creating larger more complex programs, this is really what you will want to be doing since it will more easily allow your program's classes to have high cohesion (code that deals with the same things are close together) and low coupling (code that deals with unlike things are not tightly bound which decreases code complexity), making it much easier to debug and improve your program. 这可能是矫枉过正为您的程序,所以我不打算在这个时候推这一建议,但在未来,当你创建更大更复杂的程序,这是真的,你会想要做什么,因为它会更轻松地使程序的类具有高内聚性(处理相同事物的代码紧密结合在一起)和低耦合(处理不同事物的代码没有紧密结合在一起,从而降低了代码复杂性),从而使调试和改善您的代码变得更加容易程序。

Again, the 2nd issue is more difficult to solve. 同样,第二个问题更难解决。 If the dialog window is to close after obtaining and submitting the information that constitutes one customer, than the easiest solution to this is to make the new customer dialog window a modal JDialog or a JOptionPane (which in really is nothing more than a specialized modal JDialog). 如果对话框窗口是获得并提交构成一个客户的信息后关闭,比最简单的解决方案,这是为了让新客户对话窗口模态的JDialog或JOptionPane的(这真的没有什么比一个专门的模态的JDialog更多)。 Why this helps is that Swing has a special mechanism for modal dialogs where it freezes code flow in the calling window starting immediately after setting the dialog visible. 为什么这样做有帮助,是因为Swing具有一种用于模式对话框的特殊机制,该机制在将对话框设置为可见后立即冻结在调用窗口中的代码流。 And so the calling code will always know when the dialog is no longer visible since its code's program flow will resume only when the dialog isn't visible. 因此,调用代码将始终知道对话框何时不再可见,因为仅当对话框不可见时,其代码的程序流才会恢复。 So you would want to extract the dialog window's data in the lines immediately following the one that sets the dialog or JOptionPane visible. 因此,您需要在将对话框或JOptionPane设置为可见的那一行之后的行中提取对话框窗口的数据。 And before you discard JOptionPanes as being too simplistic, understand that their second parameter, the one of type Object can take any Swing GUI component as a parameter, including a JPanel that holds a very large and complex GUI, making these tools extremely useful. 并且在您放弃JOptionPanes过于简单之前,请了解它们的第二个参数, Object类型的Object可以将任何 Swing GUI组件作为参数,包括一个包含非常大而复杂的GUI的JPanel,这使这些工具非常有用。

If the second dialog window will not be closed on the submission of a Customer and is to be left open, for instance if you want to leave it open so that the user can enter multiple Customers, then you'll likely want it to be a non-modal JDialog and will need to set up some kind of notification scheme so that the calling code will be notified when the user has submitted a Customer. 如果第二个对话框窗口在提交客户时不会关闭,而是保持打开状态,例如,如果您希望将其保持打开状态,以便用户可以输入多个客户,那么您很可能希望它是一个非模态 JDialog,将需要设置某种通知方案,以便在用户提交客户时通知调用代码。 One possible way is to allow the calling class to add an ActionListener to the dialog's submit button by giving the dialog class a public addActionListener(....) method. 一种可能的方法是通过给对话框类提供公共的addActionListener(....)方法,从而允许调用类向对话框的提交按钮添加ActionListener。 Another is to use Swing's PropertyChangeSupport and have the calling class add a PropertyChangeListener to the add customer dialog, and thus be notified of significant changes to its properties (ie, that the submit button has been changed, or that a new Customer has been created). 另一个方法是使用Swing的PropertyChangeSupport并让调用类将PropertyChangeListener添加到添加客户对话框中,从而通知其属性发生重大更改(即,提交按钮已更改,或者已创建新客户)。 。 If you use an MVC design for your program, then the notification will be from the model to the view. 如果您对程序使用MVC设计,则通知将从模型到视图。 In other words, the dialog will create the new Customer, the submit button will have the Control add the Customer to the Model, and the main GUI's Model listeners will be notified of the newly created object and will display it. 换句话说,该对话框将创建新的Customer,提交按钮将使Control将Customer添加到模型,并且将向主GUI的Model侦听器通知新创建的对象并将其显示。


... how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI with only the first & last name being displayed ...如何从第二个GUI中的文本字段传递所有客户的数据,然后将其传输到第一个GUI中的JList中, 并且只显示名字和姓氏

Your JList should be a JList<Customer> so that it holds a collection of complete Customer objects. 您的JList应该是JList<Customer>以便它包含完整的 Customer对象的集合。 You can easily change how the JList displays its data by overriding the Customer toString() method (not recommended since this method is more for debugging than for end user display), or better by assigning the JList a custom ListCellRenderer (recommended). 您可以通过覆盖Customer toString()方法(不推荐使用此方法,因为它比用于最终用户显示的调试更多,而不推荐这样做),或者通过为JList分配自定义ListCellRenderer (推荐)更好地更改JList显示数据的方式。 The Swing JList tutorial will explain how to do this, and you can come back here with your code attempt if you're stuck at this step. Swing JList教程将说明如何执行此操作,如果您在此步骤中遇到困难 ,可以尝试使用代码返回此处。


The 2nd problem I'm also facing is how to then open up another GUI which will then reverse the process when that particular customer has been selected so that changes can be made to their details. 我还面临的第二个问题是如何打开另一个GUI,当选择了该特定客户时,它将逆转该过程,以便可以更改其详细信息。

This would be solved in largely the same way as above, except rather than creating a new Customer and adding it to the collection displayed by the JList, you would replace one Customer in the JList with the edited one. 这将以与上述基本相同的方式解决,除了要创建一个新的Customer并将其添加到JList显示的集合中,您可以将JList中的一个Customer替换为已编辑的Customer。

For more detailed help, consider creating and posting a Minimal, Complete, and Verifiable Example Program . 要获得更详细的帮助,请考虑创建并发布最小,完整和可验证的示例程序 We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem. 我们不想看到您的整个程序,但是您应该将代码压缩为仍可编译的最小位,没有与您的问题无关的额外代码,但仍然可以演示您的问题。

you can pass firstname and last name or other data in main(args[]); 您可以在main(args [])中传递名字和姓氏或其他数据;

call you Jframe like this Yourclassname.main(new string[]{"firstname","lastname"}); 这样称呼您Jframe Yourclassname.main(new string [] {“ firstname”,“ lastname”});

let's know how would you like to set a data in JList. 让我们知道您想如何在JList中设置数据。

each item should contains one entity or each item contains one entity property.? 每个项目应包含一个实体或每个项目包含一个实体属性。

to display you data nicely you have to use jtable instead. 为了更好地显示数据,您必须使用jtable。

About your two jframes one contains textfield for input other one display the data. 关于您的两个jframe,一个包含用于输入的文本字段,另一个则显示数据。 I wanna to idvice you to use a jpanel inside a joptionPane, like this code snippet : 我想指导您在joptionPane中使用jpanel,例如以下代码片段:

private Customer customer; 私人客户客户;

private void newCustomer() {

    JLabel labelCode = new JLabel("Customer Code *");
    JTextField jTextFieldCode = new JTextField();
    JLabel labelName = new JLabel("Customer Name *");
    JTextField jTextFieldName = new JTextField();
    JLabel labelPrincipalAdress = new JLabel("Principal Adress *");
    JTextField jTextFieldPrincipalAdress = new JTextField();
    JLabel labelSaleAdress = new JLabel("Sale Adress *");
    JTextField jTextFieldSaleAdress = new JTextField();
    JLabel labelActivity = new JLabel("Activity ");
    JTextField jTextFieldActivity = new JTextField();

    JLabel labelDiscountC = new JLabel("Discount Code *");

    JLabel labelCountryC = new JLabel("Country Code *");
    JComboBox<String> jComboBoxCountryC = new JComboBox<>(Locale.getISOCountries());
    jComboBoxCountryC.setSelectedIndex(-1);

    JPanel panel = new JPanel();

    panel.add(labelCode);
    panel.add(jTextFieldCode);
    panel.add(labelName);
    panel.add(jTextFieldName);
    panel.add(labelPrincipalAdress);
    panel.add(jTextFieldPrincipalAdress);
    panel.add(labelSaleAdress);
    panel.add(jTextFieldSaleAdress);
    panel.add(labelActivity);
    panel.add(jTextFieldActivity);

    panel.add(labelCountryC);
    panel.add(jComboBoxCountryC);
    panel.setLayout(new GridLayout(8, 2, 1, 3));

    for (Component c : panel.getComponents()) {
        c.setFont(new Font("", Font.PLAIN, 14));
        c.setPreferredSize(new Dimension(150, 25));
    }

    if (JOptionPane.showConfirmDialog(this, panel, "New Customer",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {


            Customer newCustomer = new Customer();
            newCustomer.setCode(jTextFieldCode.getText());
            newCustomer.setName(jTextFieldName.getText());
            newCustomer.setPrincipalAdresse(labelPrincipalAdress.getText());
            newCustomer.setShippingAdresse(jTextFieldSaleAdress.getText());
            newCustomer.setActivity(jTextFieldActivity.getText());
             newCustomer.setCountry_code((String) jComboBoxCountryC.getSelectedItem());

            MainFrame.customerSBRemote.addCustomer(newCustomer);
            customersList.add(newCustomer);
            this.customer = newCustomer;
            jTableCustomer.changeSelection(customersList.indexOf(newCustomer), 0, false, false);
            this.hashcode = customer.hashCode();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM