【问题标题】:JOptionPane button size (Nimbus LAF)JOptionPane 按钮大小 (Nimbus LAF)
【发布时间】:2015-05-09 10:47:02
【问题描述】:

最近我一直在使用 Nimbus Look and Feel 进行 Swing 项目。我想将 JOptionPane 中的所有按钮设置为相同的大小,但徒劳无功。

import javax.swing.*;

public class NimbusTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {    
                try {
                    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if (("Nimbus").equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                UIManager.put("OptionPane.sameSizeButtons", true);    
                String[] options = new String[]{"--------------------","short","1"};
                int option = JOptionPane.showOptionDialog(null, "Nimbus problem", "JOptionPane", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);    
            }
        });
    }
}

我想要的是一个具有三个大小相同的按钮的 JOptionPane。但是,我得到了以下结果:

我的代码UIManager.put("OptionPane.sameSizeButtons", true); 似乎被忽略了。如果我不想重新创建类似 JOptionPane 的对话框,我应该如何创建具有相同大小按钮的 JOptionPane?

【问题讨论】:

    标签: java swing joptionpane nimbus uimanager


    【解决方案1】:

    替换

     UIManager.put("OptionPane.sameSizeButtons", true);
    

     UIManager.getLookAndFeelDefaults().put("OptionPane.sameSizeButtons", true);
    

    它就像一个魅力

    【讨论】:

    • 如何设置像素大小?
    • @EthanJCRubik 如果需要,您可以扩展 jbutton 并覆盖 getpreferedsize
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多