简体   繁体   中英

how to display information in panel after a button is clicked?

I'm working on a application which takes pizza orders. Once the user clicks the order summary button the program would display the order summary. The application looks like this: 在此处输入图片说明

I would like to print out the order summary(only not the error) in a new penal with a JTextArea like such:

在此处输入图片说明

But I don't know how. This is what my application displays right now: 在此处输入图片说明

Here is the Code related to the display:

        orderSummary = "Customer Name: " + name +
                       "\nPhone Number: " + phoneNumber +
                       "\nSize: " + size +
                       "\nToppings: " + toppings +
                       "\nTotal: $" + total;
        if(error) 
            JOptionPane.showMessageDialog(null, ErrorString);
        else
            JOptionPane.showMessageDialog(null, orderSummary);

Error display:

在此处输入图片说明

I'm not 100% sure of what you are asking, but if you are looking for a dialog to popup (not just a panel), then you could try something like:

    JDialog zMessageDialog = new JDialog((java.awt.Frame) null, true);
    zMessageDialog.setTitle("Order summary");
    zMessageDialog.setLayout(new BorderLayout());
    JTextArea zTextArea = new JTextArea("Blah blah\nblah blah\nblah blah");
    zTextArea.setEditable(false);
    zTextArea.setColumns(40);
    zTextArea.setRows(10);
    zTextArea.setBackground(null);
    JScrollPane zScrollPane = new JScrollPane(zTextArea);
    zMessageDialog.add(zScrollPane, BorderLayout.CENTER);
    zMessageDialog.revalidate();
    zMessageDialog.pack();
    zMessageDialog.setVisible(true);

This just puts a JTextArea in a JDialog. It makes the JTextArea non-editable, and sets its background color to null (which makes it look less editable).

Of course, this may not be the best way to go in terms of a user interface, but that is a different question. If you are using an IDE like Netbeans, you can easily create a separate class based on JDialog and add a panel at the bottom with an "OK" button, and whatever other customizations you desire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM