Set font size based on component size
Hi everybody,
This is kind of a duplicate, kind of new, because my original post is buried and I figured out a more helpful and general way to ask this question. Does anyone know if there is a way to set the size of a string as large as possible on a component, but still be able to have the whole string visible, if you're using a font other than default?
For example:
publicstaticvoid main( String args[] ){
try{
JFrame frame =new JFrame("Title" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
int guiWidth = width / 2;
int guiHeight = height / 2;
frame.setSize( guiWidth, guiHeight );
Container pane = frame.getContentPane();
// Create main label
String labelString ="Test";
JLabel label =new JLabel( labelString );
label.setBackground(new Color( 179, 0, 0 ) );
label.setForeground(new Color( 235, 191, 16 ) );
label.setHorizontalAlignment( CENTER );
label.setOpaque(true );
label.setFont(new Font( 揙ld English Text MT? 0, 48 ) );
pane.add( label, BorderLayout.NORTH );
frame.setLocationRelativeTo(null );
frame.setVisible(true );
}catch( Exception e){
e.printStackTrace();
}
}
As you can see, right now the font needs to be hard coded. I'd like it to be dynamic, based on the size of the screen the user has.
Sorry if the partial double post annoys anybody; I figured it would be less annoying than resurrecting my old post.
Any help on this would be great, and thank you!
Jezzica85
Message was edited by:
jezzica85
Message was edited by:
jezzica85

