我不熟悉渲染GUI并使用JScrollPane不会显示任何内容:
private JFormattedTextField IDEnterer;
private Cart cart;
private JFrame frame;
private JScrollPane jScrollPane;
public MainUserGUI(UUID uuid){
frame = new JFrame("Current user: " + uuid.toString());
cart = new Cart();
jScrollPane = new JScrollPane(/*ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED*/);
jScrollPane.setLayout(new ScrollPaneLayout());
openUserGUI();
}
public void openUserGUI(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
JPanel iDPanel = new JPanel();
iDPanel.add(Box.createHorizontalStrut(10));
iDPanel.add(Box.createVerticalStrut(50));
iDPanel.add(new JLabel("Enter an ID:"));
iDPanel.add(getIntegerField(200, 20));
iDPanel.add(getIntegerSubmitButton("Submit ID"));
jScrollPane.add(new JLabel("Shopping cart:"));
frame.add(iDPanel, BorderLayout.WEST);
frame.add(jScrollPane, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
private void renderList(){
JPanel p = new JPanel();
jScrollPane.removeAll();
jScrollPane.add(new JLabel("Shopping cart:"));
int i = 0;
for(Item item : getCartItems()){
int j = i;
JPanel panel = new JPanel();
JButton removeOne = new JButton("Remove one");
Action removeOneAction = new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
new InformationEnterer().removeOne(cart, getCartItems()[j].getID());
renderList();
}
};
removeOne.addActionListener(removeOneAction);
JButton remove = new JButton("Remove all");
Action removeAllAction = new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
new InformationEnterer().removeAll(cart, getCartItems()[j].getID());
renderList();
}
};
remove.addActionListener(removeAllAction);
JLabel label = new JLabel(i + ": " + item.getName() + "(" + item.getID() + "), "
+ "Amount: " + cart.getCart().get(item.getID()) + ", "
+ "Price: " + item.getPrice() * cart.getCart().get(item.getID()));
panel.add(label);
panel.add(removeOne);
panel.add(remove);
p.add(panel);
i++;
}
jScrollPane.setViewportView(p);
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
private Item[] getCartItems(){
Item[] r = new Item[cart.getCart().size()];
int i = 0;
for(Integer integ : cart.getCart().keySet()){
Item item = new Item(integ);
r[i] = item;
i++;
}
return r;
}
private JFormattedTextField getIntegerField(int width, int height){
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(-1);
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setCommitsOnValidEdit(true);
formatter.setAllowsInvalid(false);
IDEnterer = new JFormattedTextField(formatter);
Dimension dimension = new Dimension(width, height);
IDEnterer.setPreferredSize(dimension);
return IDEnterer;
}
private JButton getIntegerSubmitButton(String text){
JButton button = new JButton(text);
Action action = new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
if(IDEnterer != null){
if(IDEnterer.getText().length() <= 0){
JOptionPane.showMessageDialog(null, "The ID can't be null", "Warning", JOptionPane.ERROR_MESSAGE);
}else{
new InformationEnterer().addItem(cart, Integer.parseInt(IDEnterer.getText().replaceAll(",", "")));
renderList();
IDEnterer.setValue(null);
}
}
}
};
button.addActionListener(action);
return button;
}
我不知道为什么它没有显示任何东西,屏幕更新肯定是。提前谢谢你,
Whereisthemonkey
@VGR这是我的更新代码,它仍然无法正常工作