문제

The text in my JOptionPanes are way to small I was wondering how I could change the font of the text inside the pane. Also, I would like to be set a space between two buttons.

Something like

   |Canecl|               |DoneSave| |Save|
도움이 되었습니까?

해결책

I guess you mean the fonts on your JButtons inside the JOptionPane are too small.

For that I suggest using Swing Utils by Darryl Burke. Now you can easily do e.g.

for (JButton button : SwingUtils.getDescendantsOfType(JButton.class, pane)) {
    button.setFont(new Font("Verdana", Font.BOLD, 32));
}

to set the fonts on all JButtons inside of JOptionPane pane.

If you want to set the font of the JOptionPane message itself, use e.g.

UIManager.put(
    "OptionPane.messageFont",
    new FontUIResource(new Font("Verdana", Font.BOLD, 32))
);

In regard to the "spacing of buttons" question: I don't know if that can be done without extending & make a customized JOptionPane yourself.

다른 팁

You cannot specify a spacing between just 2 buttons out of 3 (as per the OP's question) but you can increase the size of the spacing between ALL the buttons :

JPanel buttonPanel                     = (JPanel) myOptionPane.getComponent(1);   
BasicOptionPaneUI.ButtonAreaLayout lm2 = (BasicOptionPaneUI.ButtonAreaLayout) buttonPanel.getLayout();
lm2.setPadding(20);
lm2.setSyncAllWidths(false);     // buttons will vary in size as needed.

or perhaps something like :

lm2.setPadding(lm2.getPadding() * 2)  // double the spacing between ALL buttons.

If there were just 2 buttons, then using these LayoutManager calls you could achieve the desired outcome.

However for varying-sized spaces between the buttons you need to implement your own JDialog where you control the layout of its JPanel yourself.

A JOptionPane is made up of 2 JPanels... one for the message (and icon) itself, and one for the buttons, that's why we getComponent(1) above.

I know the question is 4 years old but I made this answer because I had a similar need today and couldn't find the answer ANYWHERE on Stack overflow or elsewhere.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top