我们的GUI代码存在问题。当我们运行它时会发生几个错误,我们不知道它们的含义。我们正在从模型创建一个订单系统,然后我们必须包含一个我们在Window构建器中构建的接口。对java编程很新,所以我们需要得到所有帮助:D谢谢。
GUI代码:
package ISprojekt3;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class OrderApplication {
Controller1 controller = new Controller1();
private JFrame frame;
private JTextField name;
private JTextField customerNumber;
private JTextField address;
private JTextField deliveryDate;
private JTextField orderID;
private JTextField number;
private JTextField quantity;
private JTextField nameProduct;
private JTextField category;
private JTextField price;
private JTextField serialNumber;
private JTextArea message;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OrderApplication window = new OrderApplication();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public OrderApplication() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 672, 482);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblName = new JLabel("Namn:");
lblName.setBounds(23, 45, 61, 16);
frame.getContentPane().add(lblName);
JLabel lblCustomerNumber = new JLabel("Kundnummer:");
lblCustomerNumber.setBounds(23, 73, 106, 16);
frame.getContentPane().add(lblCustomerNumber);
JLabel lblAddress = new JLabel("Adress:");
lblAddress.setBounds(23, 103, 61, 16);
frame.getContentPane().add(lblAddress);
JLabel lblCustomer = new JLabel("Kund");
lblCustomer.setBounds(23, 17, 61, 16);
frame.getContentPane().add(lblCustomer);
name = new JTextField();
name.setBounds(125, 40, 130, 26);
frame.getContentPane().add(name);
name.setColumns(10);
customerNumber = new JTextField();
customerNumber.setBounds(125, 68, 130, 26);
frame.getContentPane().add(customerNumber);
customerNumber.setColumns(10);
address = new JTextField();
address.setBounds(125, 98, 130, 26);
frame.getContentPane().add(address);
address.setColumns(10);
JLabel lblOrder = new JLabel("Order");
lblOrder.setBounds(313, 17, 61, 16);
frame.getContentPane().add(lblOrder);
JLabel lblOrderID = new JLabel("OrderID:");
lblOrderID.setBounds(313, 45, 61, 16);
frame.getContentPane().add(lblOrderID);
JLabel lblDeliveryDate = new JLabel("Leveransdatum:");
lblDeliveryDate.setBounds(313, 73, 106, 16);
frame.getContentPane().add(lblDeliveryDate);
deliveryDate = new JTextField();
deliveryDate.setBounds(412, 68, 130, 26);
frame.getContentPane().add(deliveryDate);
deliveryDate.setColumns(10);
orderID = new JTextField();
orderID.setBounds(412, 40, 130, 26);
frame.getContentPane().add(orderID);
orderID.setColumns(10);
JLabel lblOrderLine = new JLabel("Orderrad");
lblOrderLine.setBounds(313, 159, 61, 16);
frame.getContentPane().add(lblOrderLine);
JLabel lblNumber = new JLabel("Nummer:");
lblNumber.setBounds(313, 187, 61, 16);
frame.getContentPane().add(lblNumber);
JLabel lblQuantity = new JLabel("Antal produkter:");
lblQuantity.setBounds(313, 215, 106, 16);
frame.getContentPane().add(lblQuantity);
number = new JTextField();
number.setBounds(412, 182, 130, 26);
frame.getContentPane().add(number);
number.setColumns(10);
quantity = new JTextField();
quantity.setBounds(412, 210, 130, 26);
frame.getContentPane().add(quantity);
quantity.setColumns(10);
JLabel lblProduct = new JLabel("Produkt");
lblProduct.setBounds(23, 159, 61, 16);
frame.getContentPane().add(lblProduct);
JLabel lblName_2 = new JLabel("Namn:");
lblName_2.setBounds(23, 187, 61, 16);
frame.getContentPane().add(lblName_2);
JLabel lblCategory = new JLabel("Kategori:");
lblCategory.setBounds(23, 215, 61, 16);
frame.getContentPane().add(lblCategory);
nameProduct = new JTextField();
nameProduct.setBounds(125, 187, 130, 26);
frame.getContentPane().add(nameProduct);
nameProduct.setColumns(10);
category = new JTextField();
category.setBounds(125, 215, 130, 26);
frame.getContentPane().add(category);
category.setColumns(10);
price = new JTextField();
price.setBounds(125, 238, 130, 26);
frame.getContentPane().add(price);
price.setColumns(10);
JLabel lblPrice = new JLabel("Pris:");
lblPrice.setBounds(23, 243, 61, 16);
frame.getContentPane().add(lblPrice);
JLabel lblCopy = new JLabel("Exemplar");
lblCopy.setBounds(23, 297, 69, 16);
frame.getContentPane().add(lblCopy);
JLabel lblSerialNumber = new JLabel("Serienummer:");
lblSerialNumber.setBounds(23, 325, 106, 16);
frame.getContentPane().add(lblSerialNumber);
serialNumber = new JTextField();
serialNumber.setBounds(125, 320, 130, 26);
frame.getContentPane().add(serialNumber);
serialNumber.setColumns(10);
JButton btnSearch = new JButton("Sök");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSearch.setBounds(409, 263, 117, 29);
frame.getContentPane().add(btnSearch);
JButton btnAdd = new JButton("Lägg till");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String customerName = name.getText();
String customerNumber = number.getText();
String customerAddress = address.getText();
if((customerName.length() < 1) || (customerNumber.length() < 1) || (customerAddress.length() < 1)) {
message.setText("Tilläggning misslyckades");
}
else {
controller.addCustomer(customerNumber, customerName, customerAddress);
message.setText("Kunden är tillagd");
}
String order = orderID.getText();
String dDate = deliveryDate.getText();
if((order.length() < 1 ) || (dDate.length() < 1)) {
message.setText("Tilläggning misslyckades");
}
else {
controller.addOrder(order, dDate, customerNumber);
message.setText("Ordern är tillagd till kund " + controller.findCustomer(customerNumber).getCustomerNumber());
}
String productName = nameProduct.getText();
String productCategory = category.getText();
String productPrice = price.getText();
if((productName.length() < 1 ) || (productCategory.length() < 1 ) || (productPrice.length() < 1 )) {
message.setText("Tilläggning misslyckades");
}
else {
controller.addProduct(productName, productCategory, productPrice);
message.setText("Produkten är tillagd");
}
String orderLineNumber = number.getText();
String orderLineQuantity = quantity.getText();
if((orderLineNumber.length() < 1 ) || (orderLineQuantity.length() < 1)) {
message.setText("Tilläggning misslyckades");
}
else {
controller.addOrderLine(orderLineNumber, orderLineQuantity, order);
message.setText("Orderraden är tillagd i order" + controller.findOrder(order).getOrderID());
}
String serialNumberCopy = serialNumber.getText();
if((serialNumberCopy.length() < 1)) {
message.setText("Tilläggningen misslyckades");
}
else {
controller.addCopy(serialNumberCopy, productName);
message.setText("Exemplaret är tillagd i produkt " + controller.findProduct(productName).getName());
}
}
});
btnAdd.setBounds(409, 292, 117, 29);
frame.getContentPane().add(btnAdd);
JButton btnRemove = new JButton("Ta bort");
btnRemove.setBounds(409, 370, 117, 29);
frame.getContentPane().add(btnRemove);
JButton btnChange = new JButton("Ändra");
btnChange.setBounds(409, 345, 117, 29);
frame.getContentPane().add(btnChange);
JTextArea message = new JTextArea();
message.setBounds(23, 375, 231, 50);
frame.getContentPane().add(message);
}
}
以下是错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ISprojekt3.OrderApplication$3.actionPerformed(OrderApplication.java:198)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:0)
使用变量范围搞乱,使用的字段private JTextArea message
未初始化。
创建的本地作用域JTextArea message = new JTextArea()
从未分配到字段 message
。