简体   繁体   English

如何将对象传递给新表单?

[英]How do you pass an object into a new form?

Why doesn't my car object pass into my ViewCarForm? 为什么我的汽车对象没有进入我的ViewCarForm?

A car gets passed into InventoryItemPanel. 汽车被传递到InventoryItemPanel。

public class InventoryItemPanel extends JPanel{
Car car;
Button button = new Button("View More Details");



public InventoryItemPanel(Car car){

    this.car = car;

    // executes ButtonActionPerformed when button is clicked.
    button.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            ButtonActionPerformed(evt);
        }
    });
    add(button);


}

public void ButtonActionPerformed(java.awt.event.ActionEvent evt) {
    new ViewCarForm(car).setVisible(true);
}                                         
}

The button when clicked is then supposed to pass the same car to ViewCarForm. 然后单击该按钮应该将同一辆车传递给ViewCarForm。

public class ViewCarForm extends javax.swing.JFrame {
Car car;


public ViewCarForm() {
    initComponents();
}

public ViewCarForm(Car car){
   new ViewCarForm().setVisible(true);
   jLabel.setText(car.getMake());
}
}

However, the label in ViewCarForm does not get updated by the car object, so I assume that it is not passing through? 但是,ViewCarForm中的标签不会被汽车对象更新,所以我假设它没有通过?

Let's look at what this constructor is doing: 让我们看一下这个构造函数在做什么:

public ViewCarForm(Car car){
   new ViewCarForm().setVisible(true); // (A)
   jLabel.setText(car.getMake());      // (B)
}
  • On line (A) you create a new ViewCarForm object -- and you do so within the ViewCarForm constructor, not something that I recommend that you do, since now you have two ViewCarForm instances, the original one, and a new one that you display. 在行(A)上你创建一个新的ViewCarForm对象 - 你在ViewCarForm构造函数中这样做,而不是我建议你做的事情,因为现在你有两个 ViewCarForm实例,原始实例和你显示的新实例。
  • On line (B) you set the text of a JLabel, a variable of the first and non-displayed ViewCarForm instance (I'm guessing that it's a variable of this class -- you never show us the variable declaration or instantiation. OK this will set the JLabel text of a non-displayed GUI, meanwhile the second ViewCarForm instance, the one that you do display, has no change to the text of its JLabel. 在行(B)上设置JLabel的文本,这是第一个和未显示的ViewCarForm实例的变量(我猜它是这个类的一个变量 - 你从来没有向我们展示变量声明或实例化。确定这个将设置为非显示 GUI的JLabel的文本,同时第二ViewCarForm情况下,你做的显示器之一,没有改变它的JLabel的文本。
  • You don't call this() or initComponents() within the 2nd constructor, and so the code from the first default constructor, including the initComponents(); 不要在第二个构造initComponents()调用this()initComponents() ,因此来自第一个默认构造函数的代码,包括initComponents(); call, is never called, and so components are never properly laid out when this constructor is called. call,从不被调用,因此在调用此构造函数时,组件永远不会正确布局。

Solution: don't do this , don't create two ViewCarForm instances, especially from within the same class's constructor. 解决方案: 不要这样做 ,不要创建两个ViewCarForm实例,尤其是在同一个类的构造函数中。 The only reason you don't have a stackoverflow error is because your class has two constructors, but even without the stackoverflow, it's insanity to do this. 你没有stackoverflow错误的唯一原因是因为你的类有两个构造函数,但即使没有stackoverflow,它也是疯狂的。 Create only one instance and set its JLabel text. 仅创建一个实例并设置其JLabel文本。 Get rid of line (A) 摆脱线(A)

Also, if the ViewCarForm is a secondary window, it shouldn't even be a JFrame but rather it should be a JDialog, either modal or non-modal depending on your need. 此外,如果ViewCarForm是一个辅助窗口,它甚至不应该是一个JFrame,而应该是一个JDialog,根据您的需要,模态或非模态。

Also, you only init components in one ViewCarForm constructor and not in the other. 此外,您只在一个ViewCarForm构造函数中初始化组件而不在另一个构建函数中。 So the JLabel will not show up in the second constructor/instance. 所以JLabel不会出现在第二个构造函数/实例中。

For example: 例如:

import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Window;
import javax.swing.*;

public class InventoryFoo extends JPanel {
    private static final Car FIRST_CAR = new Car("Honda");
    private InventoryItemPanel inventoryItemPanel = new InventoryItemPanel(FIRST_CAR);

    public InventoryFoo() {

        inventoryItemPanel.setBorder(BorderFactory.createTitledBorder("Inventory Item"));

        add(inventoryItemPanel);
    }

    private static void createAndShowGui() {
        InventoryFoo mainPanel = new InventoryFoo();

        JFrame frame = new JFrame("InventoryFoo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

class InventoryItemPanel extends JPanel {
    Car car;

    // Button button = new Button("View More Details"); // should be a JButton
    JButton button = new JButton("View More Details"); // should be a JButton

    public InventoryItemPanel(Car car) {

        this.car = car;

        // executes ButtonActionPerformed when button is clicked.
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonActionPerformed(evt);
            }
        });
        add(button);

    }

    public void ButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // new ViewCarPanel(car).setVisible(true);

        // get current JFrame
        Window thisJFrame = SwingUtilities.getWindowAncestor(this);
        // Create a non-modal JDialog
        JDialog dialog = new JDialog(thisJFrame, "Car Make", ModalityType.MODELESS);
        // create new viewCarPanel
        ViewCarPanel viewCarPanel = new ViewCarPanel(car);
        // add to dialog
        dialog.add(viewCarPanel);
        dialog.pack();
        dialog.setLocationRelativeTo(thisJFrame);
        dialog.setVisible(true);
    }
}

// better for this to be a JPanel
class ViewCarPanel extends JPanel {
    Car car;
    private JLabel jLabel = new JLabel();

    public ViewCarPanel() {
        add(new JLabel("Car Make:"));
        add(jLabel);
        setPreferredSize(new Dimension(300, 80));
    }

    public ViewCarPanel(Car car) {
        // so that we init components from the default constructor
        this(); 

        // new ViewCarPanel().setVisible(true);
        jLabel.setText(car.getMake());
    }
}

class Car {

    private String make;

    public Car(String make) {
        this.make = make;
    }

    public String getMake() {
        return this.make;
    }

}

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

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