簡體   English   中英

如何將對象傳遞給新表單?

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

為什么我的汽車對象沒有進入我的ViewCarForm?

汽車被傳遞到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);
}                                         
}

然后單擊該按鈕應該將同一輛車傳遞給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());
}
}

但是,ViewCarForm中的標簽不會被汽車對象更新,所以我假設它沒有通過?

讓我們看一下這個構造函數在做什么:

public ViewCarForm(Car car){
   new ViewCarForm().setVisible(true); // (A)
   jLabel.setText(car.getMake());      // (B)
}
  • 在行(A)上你創建一個新的ViewCarForm對象 - 你在ViewCarForm構造函數中這樣做,而不是我建議你做的事情,因為現在你有兩個 ViewCarForm實例,原始實例和你顯示的新實例。
  • 在行(B)上設置JLabel的文本,這是第一個和未顯示的ViewCarForm實例的變量(我猜它是這個類的一個變量 - 你從來沒有向我們展示變量聲明或實例化。確定這個將設置為非顯示 GUI的JLabel的文本,同時第二ViewCarForm情況下,你做的顯示器之一,沒有改變它的JLabel的文本。
  • 不要在第二個構造initComponents()調用this()initComponents() ,因此來自第一個默認構造函數的代碼,包括initComponents(); call,從不被調用,因此在調用此構造函數時,組件永遠不會正確布局。

解決方案: 不要這樣做 ,不要創建兩個ViewCarForm實例,尤其是在同一個類的構造函數中。 你沒有stackoverflow錯誤的唯一原因是因為你的類有兩個構造函數,但即使沒有stackoverflow,它也是瘋狂的。 僅創建一個實例並設置其JLabel文本。 擺脫線(A)

此外,如果ViewCarForm是一個輔助窗口,它甚至不應該是一個JFrame,而應該是一個JDialog,根據您的需要,模態或非模態。

此外,您只在一個ViewCarForm構造函數中初始化組件而不在另一個構建函數中。 所以JLabel不會出現在第二個構造函數/實例中。

例如:

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