代码之家  ›  专栏  ›  技术社区  ›  razshan

如何放大JOptionPane对话框上的按钮?

  •  0
  • razshan  · 技术社区  · 15 年前

    JOptionPane.showConfirmDialog(null, "Did you eat", "Confirmation", JOptionPane.YES_NO_OPTION);
    

    我想让按钮变大的原因是我增加了对话框的字体大小(一行代码影响了我代码中的所有对话框)。与冗长的内容相比,小的(默认)按钮看起来更像是一个小问题。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Alex    6 年前

    必须创建JOptionPane的实例并设置新的PreferredSize()。 使用setSize()无法按预期工作。 例如:

    public static void showMessageBox(final String strTitle, final String strMessage)
    {
            //Redone for larger OK button
             JOptionPane theOptionPane = new JOptionPane(strMessage,JOptionPane.INFORMATION_MESSAGE);
             JPanel buttonPanel = (JPanel)theOptionPane.getComponent(1);
            // get the handle to the ok button
            JButton buttonOk = (JButton)buttonPanel.getComponent(0);
            // set the text
            buttonOk.setText(" OK ");
            buttonOk.setPreferredSize(new Dimension(100,50));  //Set Button size here
            buttonOk.validate();
            JDialog theDialog = theOptionPane.createDialog(null,strTitle);
            theDialog.setVisible(true);  //present your new optionpane to the world.
    }
    
        2
  •  2
  •   razshan    15 年前

    将其添加到对话框之前,将更改按钮文本的字体;从而减小按钮的尺寸。请确保导入此:

    import java.awt.Font; 
    import javax.swing.plaf.FontUIResource; 
    
    
    
    
    UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("ARIAL",Font.PLAIN,35))); 
    

    如果要为特定的对话框使用不同的字体,然后返回到您正在使用的对话框,您只需放置该行代码并更改字体大小。然后在对话框之后,放回原来的那个。每次你这样做的时候它都会覆盖它。