Reputation: 137
So, I just need to add a title to my swing. Here's the code:
import javax.swing.*;
import java.util.Scanner;
public class Wall extends JFrame {
public static void main(String[] args) {
new Wall();
}
public Wall() {
final String title = "Wall Game";
this.setSize(300,300); //sets the screen
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle(title);
this.setVisible(true);
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("Welcome to the Wall Game!");
JLabel label2 = new JLabel("Click the button to read the instructions!");
panel1.add(label1);
panel1.add(label2);
this.add(panel1);
}
}
I want the Welcome to the Wall Game! part of this to be bigger. Is it possible to make it bigger? If there's a bonus, I wonder if you can add different fonts. But that's not important right now.
Thanks to anyone who knows :P.
Upvotes: 1
Views: 71
Reputation: 703
Font font = new Font("Font Name", Font.TYPE_OF_FONT, size);
yourLabel.setFont(font);
Basically, you're creating a "new" font (not really "new" because Arial
already exists, but you're just making it bigger (deriving it)).
The first parameter: "Font Name"
is self-explanatory: The name of the font; e.g Arial
.
The second parameter: Font.TYPE_OF_FONT
determines what type your font is: for example, Font.ITALIC
italicizes your label. Font.BOLD
bolds your label. For more information, see here.
The third parameter: size
, which is, in fact, the size! Changing size
to 42 makes the font size 42, and changing it to 12345.67890f changes the font size to 12345.67890.
Applying the font
label.setFont(font);
Pretty self-explanatory.
For more information, read this link.
Upvotes: 1
Reputation: 366
I believe all you need is something like this:
JLabel label1 = new JLabel("Welcome to the Wall Game!");
label1.setFont(new java.awt.Font("Arial", 1, 24));
This will set the font to type Arial, '1' will set it to plain, and '24' is the font size. Just read this if you want to do more with the font: https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html
Upvotes: 2