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

时间:2010-10-25 17:07:18

标签: java user-interface joptionpane

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

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

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

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

2 个答案:

答案 0 :(得分:2)

在对话框之前添加此项,将更改按钮文本的字体;从而,增加了按钮的尺寸。请务必导入:

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




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

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

答案 1 :(得分:2)

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

}