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

Java 如何放大JOptionPane对话框上的按钮?,java,user-interface,joptionpane,Java,User Interface,Joptionpane,下面的代码行显示一个带有两个按钮的对话框:是和否。我希望这两个按钮至少是实际默认大小的3倍。我知道我可以创建一个定制的JFrame和add按钮并设置大小,但我有很多对话框;这似乎不切实际 JOptionPane.showConfirmDialog(null, "Did you eat", "Confirmation", JOptionPane.YES_NO_OPTION); 我之所以想让按钮变大,是因为我增加了对话框的字体大小(一行代码影响了代码中的所有对话框)。与冗余相比,按钮的小(默认)大

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

JOptionPane.showConfirmDialog(null, "Did you eat", "Confirmation", JOptionPane.YES_NO_OPTION);
我之所以想让按钮变大,是因为我增加了对话框的字体大小(一行代码影响了代码中的所有对话框)。与冗余相比,按钮的小(默认)大小现在看起来更大了


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

在对话框之前添加此项,将更改按钮文本的字体;因此,减少了按钮的尺寸。请确保导入以下内容:

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




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

如果你想为一个特定的对话框使用不同的字体,然后回到你使用的那个对话框,你所要做的就是把那行代码放进去,然后改变字体大小。然后在对话框之后,将原始对话框放回原处。每次你这么做的时候,它都会覆盖它

在对话框之前添加此选项将更改按钮文本的字体;因此,减少了按钮的尺寸。请确保导入以下内容:

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




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

如果你想为一个特定的对话框使用不同的字体,然后回到你使用的那个对话框,你所要做的就是把那行代码放进去,然后改变字体大小。然后在对话框之后,将原始对话框放回原处。每次你这么做的时候,它都会覆盖它

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

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