Hi zusammen!
Bin momentan dabei, eine GUI nach MVC Muster zu erstellen. Hier mal der wichtige Code:
View:
Control:
Model:
Starte ich das Programm und mache Eingaben in meinen Textfeldern, ballert Eclipse mir ne NullPointerException um die Ohren:
Mache ich keine Eingaben, greift mein try-catch Block die NumberFormatException auf. Ich weiß, dass da irgendwas mit den Referenzen der Klassen untereinander nicht stimmt, aber ich sehe einfach nicht, wo genau der Wurm drin ist.
Der Code ist natürlich noch lang nicht fertig, also nicht über evtl. Redundanzen o.Ä. wundern.
Danke schon mal
Bin momentan dabei, eine GUI nach MVC Muster zu erstellen. Hier mal der wichtige Code:
View:
Java:
package gUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import net.miginfocom.swing.MigLayout;
public class ViewShop extends JFrame implements Observer {
private ModelShop model;
private JMenuBar menu;
private JPanel panelLeft, panelRight, panelDown;
private JMenu chooseONoff;
private JRadioButtonMenuItem workOn, workOff;
private JLabel name, quantity, price, orders, nameDown, quantityDown, priceDown;
private JList<String> orderList;
private JTextField enterName, enterQuantity, enterPrice;
private JButton showOnOff, addProduct;
private String[] data = {};
private JScrollPane scrollpane;
public ViewShop(ModelShop model) {
super("Hardware Shop");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(400, 300);
// this.setSize(600,400);
this.setLayout(new BorderLayout());
this.setResizable(false);
this.model = model;
model.addObserver(this);
// adding MenuBar and all of its components
chooseONoff = new JMenu("Offline");
workOn = new JRadioButtonMenuItem("Work Online");
workOff = new JRadioButtonMenuItem("Work Offline");
menu = new JMenuBar();
chooseONoff.add(workOff);
chooseONoff.add(workOn);
showOnOff = new JButton();
showOnOff.setEnabled(false);
showOnOff.setBackground(Color.red);
menu.add(showOnOff);
menu.add(chooseONoff);
this.add(menu, BorderLayout.NORTH);
// designing Panel on the left
panelLeft = new JPanel();
panelLeft.setLayout(new MigLayout());
name = new JLabel("Name");
quantity = new JLabel("Quantity");
price = new JLabel("Price");
name.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
quantity.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
price.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
panelLeft.add(name, "w 100!");
panelLeft.add(quantity, "w 100!");
panelLeft.add(price, "w 100!");
this.add(panelLeft, BorderLayout.WEST);
// designing Panel on the right
panelRight = new JPanel();
panelRight.setLayout(new BoxLayout(panelRight, BoxLayout.Y_AXIS));
orders = new JLabel("Orders");
orderList = new JList<String>(data);
orders.add(orderList);
panelRight.add(orders);
panelRight.add(orderList);
scrollpane = new JScrollPane(orderList);
panelRight.add(scrollpane);
this.add(panelRight, BorderLayout.EAST);
// designing Panel on the bottom
panelDown = new JPanel();
nameDown = new JLabel("Name:");
quantityDown = new JLabel("Quantity:");
priceDown = new JLabel("Price:");
enterName = new JTextField(15);
enterPrice = new JTextField(15);
enterQuantity = new JTextField(15);
addProduct = new JButton("Add Product");
panelDown.setLayout(new MigLayout());
panelDown.add(nameDown);
panelDown.add(quantityDown);
panelDown.add(priceDown, "wrap");
panelDown.add(enterName);
panelDown.add(enterQuantity);
panelDown.add(enterPrice);
panelDown.add(addProduct);
this.add(panelDown, BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}
@Override
public void update(Observable arg0, Object arg1) {
this.pack();
}
public JRadioButtonMenuItem getWorkOn() {
return workOn;
}
public JRadioButtonMenuItem getWorkOff() {
return workOff;
}
public JButton getAddProduct() {
return addProduct;
}
public JTextField getEnterName() {
return enterName;
}
public JTextField getEnterQuantity() {
return enterQuantity;
}
public JTextField getEnterPrice() {
return enterPrice;
}
public String returnName() {
return enterName.getText();
}
public int returnQuantity() {
return Integer.parseInt(enterQuantity.getText());
}
public double returnPrice() {
return Double.parseDouble(enterPrice.getText());
}
}
Control:
Java:
package gUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ControllerShop {
private final ModelShop model;
private final ViewShop view;
public ControllerShop(final ModelShop model, final ViewShop view) {
this.model = model;
this.view = view;
this.control();
}
public void control() {
// interaction with Add Product Button
view.getAddProduct().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
model.addProduct(view.returnName(), view.returnQuantity(), view.returnPrice());
} catch (NumberFormatException ex) {
System.err.println("Was soll das!");
}
}
});
// interaction with RadioMenuBar
view.getWorkOff().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.onlineOfflineMode();
}
});
view.getWorkOn().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.onlineOfflineMode();
}
});
}
}
Model:
Java:
package gUI;
import java.util.Observable;
public class ModelShop extends Observable {
private ViewShop view;
private Product p;
public ModelShop() {
super();
}
public void addProduct(String name, int quantity, double price) {
try {
if (view.getEnterName().getText() != null && view.getEnterQuantity().getText() != null &&
view.getEnterPrice().getText() != null) {
this.p = new Product(name, quantity, price);
}
} catch (NullPointerException e) {
e.printStackTrace();
// System.err.println("Die Eingaben müssen Zahlen sein!");
}
}
public void onlineOfflineMode() {
if (view.getWorkOff().isEnabled()) {
System.out.println("huhu");
view.getWorkOn().setEnabled(false);
}
super.setChanged();
super.notifyObservers();
}
}
Starte ich das Programm und mache Eingaben in meinen Textfeldern, ballert Eclipse mir ne NullPointerException um die Ohren:
Java:
java.lang.NullPointerException
at gUI.ModelShop.addProduct(ModelShop.java:16)
at gUI.ControllerShop$1.actionPerformed(ControllerShop.java:20)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Der Code ist natürlich noch lang nicht fertig, also nicht über evtl. Redundanzen o.Ä. wundern.
Danke schon mal