繁体   English   中英

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

[英]How to enlarge buttons on JOptionPane dialog boxes?

下面的代码行显示一个带有两个按钮的对话框:是和否。我希望这两个按钮至少是实际默认大小的 3 倍。 我知道我可以创建一个自定义的 JFrame 并添加按钮并设置大小,但我有很多对话框; 似乎不实用。

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

我想让按钮变大的原因是因为我增加了对话框的字体大小(一行代码影响了我代码中的所有对话框)。 与文字相比,按钮的小(默认)尺寸现在看起来很尴尬。

那么如何操作 JOptionPane 对话框的按钮大小呢?

您必须创建一个 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.
}

在对话框之前添加此项,将更改按钮文本的字体; 因此,增加了按钮的尺寸。 确保导入这个:

import java.awt.Font; 
import javax.swing.plaf.FontUIResource; 




UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("ARIAL",Font.PLAIN,35))); 

如果想要为特定对话框使用不同的字体,然后返回到您使用的那个,您所要做的就是输入该行代码并更改字体大小。 然后弹出对话框后,把原来的放回去。 每次你这样做时,它都会覆盖它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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