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