如何制作像 JOptionPane 一样工作的 JPanel?

How to make a JPanel that work like JOptionPane?

我正在 NetBeans 上制作一个简单的 POS 系统,它会在单击产品照片时弹出 JPanel(数量)询问数量。我正在使用卡片布局并将面板放在卡片布局内似乎不起作用,因为它的大小不同。它也很难定位,因为移动它会使更大的面板(购买)吸收它并成为面板的一部分,从而弄乱该面板的布局。我想让面板最初不可见,只用这段代码弹出:

public void mouseClicked(MouseEvent e) {
    if (e.getSource() == bpie )
    {
        String name = "Banoffee Pie";
        int price = 8;

        quantity.setVisible(true);
    }
}

我目前是初学者,很难自定义 JOptionPane 对话框,如果可能的话我更喜欢使用面板。该问题可以通过使用另一个 JFrame 来解决,但是,根据专家的说法,使用多个框架是不好的做法。

这是我希望选项窗格的外观:

I'm currently a beginner and have a hard time customizing JOptionPanes

JOptionPane 是为实用而不是定制而设计的。一旦您开始考虑 'How can I change a JOptionPane to..?' 就放弃选项窗格,而是使用模态 JDialog.

这里是一个使用对话框的例子。我沿着这些方向调整了布局:

  • 标题栏下方居中的食物图标。
  • 放弃了更简单的按钮名称,取而代之的是更具描述性的名称。
  • 在同一行添加问题和答案,并使用旋转器选择数字。

当然,需要调整颜色以适应上面看到的样式,可能(可能不会 - 取决于进一步因素不是很明显)最好通过使用自定义可插拔外观来解决。

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;

public class SweetShop {

    private JComponent ui = null;
    private static JFrame frame = new JFrame("Sweet Shop");
    private final JDialog dialog = new JDialog(frame, "Choose Sweets", true);
    Random random = new Random();
    SpinnerNumberModel quantityModel = new SpinnerNumberModel(1, 1, 144, 1);

    SweetShop() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(40,100,40,100));

        JButton button = new JButton("Buy Sweets");
        ui.add(button);
        ActionListener openChooserListener = (ActionEvent e) -> {
            dialog.setLocationRelativeTo(button);
            dialog.setVisible(true);
        };
        button.addActionListener(openChooserListener);

        dialog.add(getSweetSelectionPanel());
        dialog.pack();
    }

    private JPanel getSweetSelectionPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        int pad = 10;
        panel.setBorder(new EmptyBorder(pad, pad, pad, pad));

        JPanel iconPanel = new JPanel();
        for (int ii=0; ii<12; ii++) {
            iconPanel.add(new JLabel(new ImageIcon(getSize16Image())));
        }
        panel.add(iconPanel, BorderLayout.PAGE_START);

        JPanel buttonPanel = new JPanel();
        JButton okButton = new JButton("Buy Delicious");
        buttonPanel.add(okButton);
        ActionListener okListener = (ActionEvent e) -> {
            System.out.println("Yuuuummmmm.. x " + 
                quantityModel.getNumber().intValue());
            dialog.setVisible(false);
        };
        okButton.addActionListener(okListener);

        JButton cancelButton = new JButton("No Thanks");
        buttonPanel.add(cancelButton);
        ActionListener cancelListener = (ActionEvent e) -> {
            System.out.println("I just like licking them.");
            dialog.setVisible(false);
        };
        cancelButton.addActionListener(cancelListener);
        panel.add(buttonPanel, BorderLayout.PAGE_END);

        JPanel questionPanel = new JPanel();
        questionPanel.setBorder(new EmptyBorder(20, 50, 20, 50));
        panel.add(questionPanel); // automatically uses CENTER constraint
        JLabel label = new JLabel("How many do you wish to buy?");
        Font font = label.getFont();
        label.setFont(font.deriveFont(Font.ITALIC));
        label.setText("How many do you wish to buy?");
        label.setBorder(new EmptyBorder(5, 5, 5, 5));
        questionPanel.add(label);
        JSpinner spinner = new JSpinner(quantityModel);
        questionPanel.add(spinner);

        return panel;
    }

    private Image getSize16Image() {
        int w = 16;
        int h = 16;
        if (random.nextBoolean()) {
            w = random.nextInt(12) + 4;
        } else {
            h = random.nextInt(12) + 4;
        }
        BufferedImage bi = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        return bi;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            SweetShop o = new SweetShop();

            frame = new JFrame(o.getClass().getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);

            frame.setContentPane(o.getUI());
            frame.pack();
            frame.setMinimumSize(frame.getSize());

            frame.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}