[英]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)
}
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.