gpt4 book ai didi

Java GUI 使用从文本字段获取的参数创建对象

转载 作者:行者123 更新时间:2023-12-01 13:45:37 25 4
gpt4 key购买 nike

所以我有这个 Jframe,您可以在其中填写文本字段,当单击“添加”时,它应该创建一个 Car 对象并将其发送到数据库。但在监听器中,即使我在字段中输入某种文本,我仍然在 textfieldname.getText() 上收到 java.lang.NullPointerException

这是 Jframe。

public void newCar()
{

JLabel make = new JLabel("Brand:");
JTextField maket = new JTextField(10);

JLabel model = new JLabel("Model:");
JTextField modelt = new JTextField(10);

JLabel license = new JLabel("License Plate Numbers:");
JTextField licenset = new JTextField(10);

JLabel color = new JLabel("Color:");
JTextField colort = new JTextField(10);

JLabel year = new JLabel("Year:");
final JTextField yeart = new JTextField(10);
yeart.addKeyListener(new KeyAdapter()
{
@Override
public void keyTyped(KeyEvent e)
{
super.keyTyped(e);

e.getKeyCode();

if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
{
e.consume();
}
}

});
JLabel horse = new JLabel("Horse Power: ");
JTextField horset = new JTextField(10);
JLabel isAvailable = new JLabel("Car Status:");
JLabel isAvailablet = new JLabel("Available");
JLabel time = new JLabel("Time Until Service: ");
JTextField timet = new JTextField(10);
JLabel consumption = new JLabel("Consumption per 100km: ");
JTextField consumptiont = new JTextField(10);
JLabel seats = new JLabel("Number of Seats: ");
JTextField seatst = new JTextField(10);
seatst.addKeyListener(new KeyAdapter()
{
@Override
public void keyTyped(KeyEvent e)
{
super.keyTyped(e);

e.getKeyCode();

if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
{
e.consume();
}
}

});

JLabel transmission = new JLabel("Transmission ");
JComboBox transmissiont = new JComboBox<String>();
JLabel climate = new JLabel("Climate Control: ");
JComboBox climatet = new JComboBox<String>();

JFrame window = new JFrame("New Car");

JPanel newCarButtons = new JPanel();

newCarButtons.setLayout(new FlowLayout());

saveCar = new JButton("Save");
saveCar.addActionListener(buttonListener);
newCarButtons.add(saveCar);

cancelCar = new JButton("Cancel");
cancelCar.addActionListener(buttonListener);
newCarButtons.add(cancelCar);

JPanel newCarText = new JPanel();

GroupLayout group = new GroupLayout(newCarText);
newCarText.setLayout(group);

group.setAutoCreateGaps(true);
group.setAutoCreateContainerGaps(true);

newCarText.add(make);
newCarText.add(maket);

newCarText.add(model);
newCarText.add(modelt);

newCarText.add(license);
newCarText.add(licenset);

newCarText.add(color);
newCarText.add(colort);

newCarText.add(year);
newCarText.add(yeart);

newCarText.add(horse);
newCarText.add(horset);

newCarText.add(isAvailable);
newCarText.add(isAvailablet);

newCarText.add(time);
newCarText.add(timet);

newCarText.add(consumption);
newCarText.add(consumptiont);

newCarText.add(seats);
newCarText.add(seatst);

JLabel doors = new JLabel("Number of Doors: ");
JTextField doorst = new JTextField(10);

doorst.addKeyListener(new KeyAdapter()
{
@Override
public void keyTyped(KeyEvent e)
{
super.keyTyped(e);

e.getKeyCode();

if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
{
e.consume();
}
}

});

newCarText.add(doors);
newCarText.add(doorst);

transmissiont.addItem("Auto");
transmissiont.addItem("Manual");

transmissiont.addActionListener(buttonListener);

newCarText.add(transmission);
newCarText.add(transmissiont);

climatet.addItem("Yes");
climatet.addItem("No");

newCarText.add(climate);
newCarText.add(climatet);

GroupLayout.SequentialGroup hGroup = group.createSequentialGroup();

hGroup.addGroup(group.createParallelGroup().addComponent(make)
.addComponent(model).addComponent(license).addComponent(color)
.addComponent(time).addComponent(consumption).addComponent(year)
.addComponent(horse).addComponent(isAvailable).addComponent(seats)
.addComponent(doors).addComponent(transmission)
.addComponent(climate));

hGroup.addGroup(group.createParallelGroup().addComponent(maket)
.addComponent(modelt).addComponent(licenset).addComponent(colort)
.addComponent(timet).addComponent(consumptiont).addComponent(yeart)
.addComponent(isAvailablet).addComponent(horset)
.addComponent(seatst).addComponent(doorst)
.addComponent(transmissiont).addComponent(climatet));

group.setHorizontalGroup(hGroup);

GroupLayout.SequentialGroup vGroup = group.createSequentialGroup();

vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(make).addComponent(maket));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(model).addComponent(modelt));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(license).addComponent(licenset));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(color).addComponent(colort));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(year).addComponent(yeart));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(horse).addComponent(horset));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(time).addComponent(timet));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(consumption).addComponent(consumptiont));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(isAvailable).addComponent(isAvailablet));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(seats).addComponent(seatst));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(doors).addComponent(doorst));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(transmission).addComponent(transmissiont));
vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
.addComponent(climate).addComponent(climatet));

group.setVerticalGroup(vGroup);

JPanel newCar = new JPanel();
newCar.setLayout(new BorderLayout());

newCar.add(newCarText, BorderLayout.NORTH);
newCar.add(newCarButtons, BorderLayout.SOUTH);

newCar.setBorder(new TitledBorder(BorderFactory
.createLineBorder(Color.black), "[New Car]", 2, 0));


window.add(newCar);
window.setLocationRelativeTo(null);
window.setSize(400, 450);
window.setVisible(true);

}

按钮监听器:(忽略输入的值,我只是测试对象是否已发送,所以我必须输入一些值,因为我遇到了 .getText() 的问题)

 private class MyButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == addCars)
{
newCar();

}

if (e.getSource() == saveCar)
{
adapter = new DatabaseAdapter2();

// Car car1 = new Car("ss", "ss",
// "sd", "ss",
// 15, "ss","ss",
// "ss", "ss", 2, 2, "ss",
// "ss", 2);

Car car1 = new Car(maket.getText(), "test", "test", "test", 2011,
"test", "test", "test", "test", 2011, 2011, "test", "test", 2);
System.out.println(car1);

adapter.addCar(car1);
}
if (e.getSource() == cancelCar)
{

}

if (e.getSource() == getCars)
{

}
if (e.getSource() == removeCars)
{

}
if (e.getSource() == editCars)
{

}
if (e.getSource() == infoCars)
{

}
if (e.getSource() == addClients)
{
newClient();
}
if (e.getSource() == removeClients)
{

}
if (e.getSource() == editClients)
{

}
if (e.getSource() == infoClients)
{

}
if (e.getSource() == addAutoCampers)
{

}
if (e.getSource() == removeAutoCampers)
{

}
if (e.getSource() == editAutoCampers)
{

}
if (e.getSource() == infoAutoCampers)
{

}
if (e.getSource() == addBookings)
{
newBooking();
}
if (e.getSource() == removeBookings)
{

}
if (e.getSource() == editBookings)
{

}
if (e.getSource() == infoBookings)
{

}

// if (e.getSource() == transmissiont)
// {
// if (transmissiont.getSelectedItem())
// {
// Student temp = (Student)studentBox.getSelectedItem();
// firstNameField.setText(temp.getFirstName());
// lastNameField.setText(temp.getLastName());
// }
// }

}
}
}

最佳答案

您似乎遇到了上下文问题。

您已在 newCar 方法中声明了所有字段,这意味着您在屏幕上使用的字段与您在 newCar 中访问的字段不同>actionPerformed 方法。

尝试删除变量名称之前的 JTextField(在 newCar 方法中),并在需要时将缺少的字段声明为实例变量

您还应该避免在文本字段上使用 KeyListener,当您收到按键事件通知时,它已经应用于字段 Document

相反,您应该考虑使用DocumentFilter

查看

关于Java GUI 使用从文本字段获取的参数创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com