AXISHK
asked on
Set the default size of Java Application
How to set the default font size in Java application ? The size shown in JOption frame is small.
Thx
Thx
ASKER
Seem like it doesn't make any difference... Any idea ?
Are you trying to change the frame title font size?
ASKER
Message inside JOptionPane...
Try this and let me know
———
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities ;
public class DialogueDemo
{
public static void main(String... args)
{
SwingUtilities.invokeLater (() -> {new DialogueDemo().go(args);}) ;
}
private DialogueDemo(){}
private void go(String[] args)
{
JDialog d = new JDialog((java.awt.Frame)nu ll, args[0], true);
d.setDefaultCloseOperation (JDialog.D ISPOSE_ON_ CLOSE);
d.setBounds(50, 50, 575, 150);
d.setLayout(new BorderLayout());
// label with original font
JLabel label = new JLabel(args[1]);
label.setFont(new Font("Serif", Font.ITALIC, 32));
d.add(label, BorderLayout.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(java.a wt.Color.R ED);
// first button
JButton larger = new JButton("Larger");
larger.addActionListener(( ActionEven t evt) -> {label.setFont(new Font(
label.getFont().getName(),
label.getFont().getStyle() ,
label.getFont().getSize() + 1
));});
panel.add(larger, BorderLayout.LINE_START);
// second button
JButton smaller = new JButton("smaller");
smaller.addActionListener( (ActionEve nt evt) -> {label.setFont(new Font(
label.getFont().getName(),
label.getFont().getStyle() ,
label.getFont().getSize() - 1
));});
panel.add(smaller, BorderLayout.LINE_END);
d.add(panel, BorderLayout.PAGE_START);
d.setVisible(true);
}
}
———-
———
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities
public class DialogueDemo
{
public static void main(String... args)
{
SwingUtilities.invokeLater
}
private DialogueDemo(){}
private void go(String[] args)
{
JDialog d = new JDialog((java.awt.Frame)nu
d.setDefaultCloseOperation
d.setBounds(50, 50, 575, 150);
d.setLayout(new BorderLayout());
// label with original font
JLabel label = new JLabel(args[1]);
label.setFont(new Font("Serif", Font.ITALIC, 32));
d.add(label, BorderLayout.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(java.a
// first button
JButton larger = new JButton("Larger");
larger.addActionListener((
label.getFont().getName(),
label.getFont().getStyle()
label.getFont().getSize() + 1
));});
panel.add(larger, BorderLayout.LINE_START);
// second button
JButton smaller = new JButton("smaller");
smaller.addActionListener(
label.getFont().getName(),
label.getFont().getStyle()
label.getFont().getSize() - 1
));});
panel.add(smaller, BorderLayout.LINE_END);
d.add(panel, BorderLayout.PAGE_START);
d.setVisible(true);
}
}
———-
java DialogueDemo "Alfred is Brilliant" "This is how you show a Dialogue"
If this does not work I will chek it tomorrow
ASKER
Compiler successfully but it returns error when running the program...
C:\Temp>javac DialogueDemo.java
C:\Temp>java DialogueDemo
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB oundsExcep tion: 0
at DialogueDemo.go(DialogueDe mo.java:19 )
at DialogueDemo.lambda$main$0 (DialogueD emo.java:1 4)
at java.awt.event.InvocationE vent.dispa tch(Invoca tionEvent. java:311)
at java.awt.EventQueue.dispat chEventImp l(EventQue ue.java:75 8)
at java.awt.EventQueue.access $500(Event Queue.java :97)
at java.awt.EventQueue$3.run( EventQueue .java:709)
at java.awt.EventQueue$3.run( EventQueue .java:703)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.security.ProtectionDo main$JavaS ecurityAcc essImpl.do Intersecti onPrivileg e(Protecti onDomain.j ava:74)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:728)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Even tDispatchT hread.java :205)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(EventDi spatchThre ad.java:11 6)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hread.java :105)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 01)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:9 3)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:82)
C:\Temp>javac DialogueDemo.java
C:\Temp>java DialogueDemo
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB
at DialogueDemo.go(DialogueDe
at DialogueDemo.lambda$main$0
at java.awt.event.InvocationE
at java.awt.EventQueue.dispat
at java.awt.EventQueue.access
at java.awt.EventQueue$3.run(
at java.awt.EventQueue$3.run(
at java.security.AccessContro
at java.security.ProtectionDo
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
ASKER
Sorry, the above code works ! However, how to make the default button and the text inside JOptionPane larger and match with the label ?
Thx again.
Thx again.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
label.setFont(new Font("Arial", Font.BOLD, 18));
JOptionPane.showMessageDia
hope this helps