Reputation: 21
package guilabq;
import java.awt.*;
import javax.swing.*;
public class GUI {
JFrame f=new JFrame();//Creating a frame
JTextArea area=new JTextArea();
JScrollPane scroll=new JScrollPane(area);
JRadioButton b1=new JRadioButton("Wrap");
JRadioButton b2=new JRadioButton("Wrap Words");
JRadioButton b3=new JRadioButton("Wrap Characters");
ButtonGroup grp=new ButtonGroup();
JPanel p1=new JPanel();//Creating a panel
GUI() {
grp.add(b1);
grp.add(b2);//Grouping the buttons
grp.add(b3);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.setLayout(new GridLayout(1,3));
p1.setBorder(new TitledBorder("Wrap Options"));
f.add(scroll,BorderLayout.CENTER);
f.add(p1,BorderLayout.SOUTH);
f.setLocationRelativeTo(null);//Here I have tried to center the frame
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 300);
f.setVisible(true);
}
public static void main(String[]args) {
new GUI();
}
}
Here I have tried to center the frame by using the method setLocationRelativeTo(null);
but I don't understand why it is not appearing in the center. It is appearing a little below and to the right from center.
Upvotes: 1
Views: 63
Reputation: 206
Move
f.setLocationRelativeTo(null);//Here I have tried to center the frame
after
f.setSize(400, 300);
like this:
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 300);
/** Moved here **/
f.setLocationRelativeTo(null);//Here I have tried to center the frame
f.setVisible(true);
Upvotes: 5