【问题标题】:JOptionPane component placementJOptionPane 组件放置
【发布时间】:2020-11-03 09:48:47
【问题描述】:

我有一个像这样的 JOptionPane 对话框。

我希望复选框就在这样的图标下方。

是否可以使用 JOptionPane 或者我必须创建自己的对话框?

这是第一张图片的代码。

public static void main(String[] args) {

    Box box = Box.createVerticalBox();
    box.add(new JLabel("TEXT"));
    box.add(new JCheckBox("Check box text"));

    int result = JOptionPane.showConfirmDialog(null,
            box,
            "TITLE",
            JOptionPane.YES_NO_OPTION);
}

【问题讨论】:

  • 您必须创建自己的对话框。您可以通过 UIManager.getIcon("OptionPane.questionIcon") 检索 JOptionPane 的图标来使用它。

标签: java swing joptionpane


【解决方案1】:

使用 PLAIN_MESSAGE,然后在面板中显示图标:

public static void main(String[] args) throws Exception
{
    JPanel first = new JPanel( new FlowLayout( FlowLayout.LEFT) );
    first.add( new JLabel( UIManager.getIcon("OptionPane.questionIcon" ) ) );
    first.add(new JLabel("TEXT"));
    JPanel box = new JPanel( new GridLayout(0, 1) );
    box.add(first);
    box.add(new JCheckBox("Check box text"));

    int result = JOptionPane.showConfirmDialog(null,
            box,
            "TITLE",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.PLAIN_MESSAGE
            );

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    相关资源
    最近更新 更多