Reputation: 71
I have been trying to change the font size of a JMenu in a Java program of mine because it automatically is displayed incredibly tiny (I am running Windows 10).
Here is my code:
import javax.swing.*;
import java.awt.*;
...
public static void runGUI()
{
//setup
JFrame frame = new JFrame("Resistor Identifier");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,1000);
//define content
Font font1 = new Font("SansSerif", Font.BOLD, 30);
JMenuBar menu = new JMenuBar();
JMenu menu1 = new JMenu("Settings");
menu.setFont(font1);
menu.add(menu1);
JButton button1 = new JButton("Button 1");
button1.setFont(font1);
JTextArea text = new JTextArea();
UIManager.put("Menu.font", font1);
//show content
frame.getContentPane().add(BorderLayout.CENTER, button1);
frame.getContentPane().add(BorderLayout.NORTH, menu);
frame.setVisible(true);
}
I have tried using both of the following statements to change the font size, but neither has done anything to the font size (the bold option did not work either in the statements I tried).
Font font1 = new Font("SansSerif", Font.BOLD, 30);
menu.setFont(font1);
and
UIManager.put("Menu.font", font1);
Please let me know if you have a solution to this problem. Thank you.
Upvotes: 1
Views: 926
Reputation: 71
After typing this out, I realized my mistake. I had defined the method I used to display the GUI as a static method. After redefining the method simply as
public void runGUI() {}
and running the program, the font of the JMenu became both bolded and 30 pt. I tested both of the methods I used as stated above with the method redefined as non-static, and both statements worked for me to adjust the font size and boldness of the JMenu.
I decided to answer my own question in case anyone else had a similar problem. I just have one more question for anyone who would like to answer: why does this work only when the method is not static?
Upvotes: 1