2010-10-25 110 views
0

下面的代码行显示一个带有两个按钮的对话框:是和否我希望这两个按钮至少为实际默认大小的3倍。我知道我可以创建一个定制的JFrame并添加按钮并设置大小,但我有几十个对话框;似乎不实际。如何放大JOptionPane对话框上的按钮?

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

为什么我想使按钮大的原因是因为我增加的对话框(一行代码影响的所有对话框在我的代码)的字体大小。与虚拟机相比,按钮的小(默认)大小看起来很尴尬。

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

回答

2

在对话框前加上这个,会改变按钮文字的字体;从而增加按钮的尺寸。确保导入此:

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




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

如果希望有不同的字体为特定的对话框,然后回到你用的人,你所要做的就是把该行的代码,并更改字体大小。然后在对话框后,把原来的一个放回去。每次你这样做,它都会覆盖它。

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. 

}

相关问题