Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write a string on a jpanel centered

I would write on a JPanel a String much bigger respect than other element.which could be the way to paint a string in terms of simply?There is a method to do this?

like image 959
Mazzy Avatar asked Nov 30 '22 14:11

Mazzy


1 Answers

You could add the text as a JLabel component and change its font size.

public static void main(String[] args) {
    NewJFrame1 frame = new NewJFrame1();
    frame.setLayout(new GridBagLayout());
    JPanel panel = new JPanel();
    JLabel jlabel = new JLabel("This is a label");
    jlabel.setFont(new Font("Verdana",1,20));
    panel.add(jlabel);
    panel.setBorder(new LineBorder(Color.BLACK)); // make it easy to see
    frame.add(panel, new GridBagConstraints());
    frame.setSize(400, 400);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(NewJFrame1.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

The code will run and look like this : GUI

see also Java layout manager vertical center for more info

like image 72
MaVRoSCy Avatar answered Dec 04 '22 11:12

MaVRoSCy