为什么帧在运行时没有在此 GUI 程序中居中?


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();
    }
}

在这里,我试图通过使用setLocationRelativeTo(null);方法来居中框架,但我不明白为什么它没有出现在中心。

Move

f.setLocationRelativeTo(null);//Here I have tried to center the frame

f.setSize(400, 300);

:

    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);

相关内容

  • 没有找到相关文章

最新更新