Java Jframe 将我的按钮居中):<

发布于 2025-01-08 16:07:28 字数 1077 浏览 1 评论 0 原文

这是我的代码:

public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("Vex Development Studio 2.0");
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setLocation(10,10);
//make variables
 File newproject;

 Container content = frame.getContentPane();
 GridBagConstraints gbc = new GridBagConstraints ();
Dimension buttonsize = new Dimension(75,25);  
Button about;
about = new Button("About");
about.setPreferredSize(buttonsize);
 //add content
 content.setLayout(new GridBagLayout());
    content.setBackground(Color.white);
    gbc.gridx = 0;
    gbc.gridy = 0;
    content.add(about,gbc);
    //main stuff
    //about button
    about.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae){
          JOptionPane.showMessageDialog(null, "Example", "About", 1);
          }
          });
    //some extra crap
    frame.setSize(700, 500);
    frame.show();
    //end
}
}

我拥有所有正确的代码,并且拥有所有必需的导入,问题是:我需要将按钮位于窗口的左上角。提前致谢! (:

Here is my code:

public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("Vex Development Studio 2.0");
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setLocation(10,10);
//make variables
 File newproject;

 Container content = frame.getContentPane();
 GridBagConstraints gbc = new GridBagConstraints ();
Dimension buttonsize = new Dimension(75,25);  
Button about;
about = new Button("About");
about.setPreferredSize(buttonsize);
 //add content
 content.setLayout(new GridBagLayout());
    content.setBackground(Color.white);
    gbc.gridx = 0;
    gbc.gridy = 0;
    content.add(about,gbc);
    //main stuff
    //about button
    about.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae){
          JOptionPane.showMessageDialog(null, "Example", "About", 1);
          }
          });
    //some extra crap
    frame.setSize(700, 500);
    frame.show();
    //end
}
}

I have all the code right, and I have all the required imports, the problem is: I need the buttons to be at the top left corner of the window. Thanks in advance! (:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眸 2025-01-15 16:07:28

因为在你的 gridbaglayout 中,只有一个正方形。

我建议使用 JButton 而不是 Button。

混合 AWT 和 Swing 是一个坏主意。

Because in your gridbaglayout, there's only one square.

And I suggest to use a JButton instead of Button.

mixing AWT and Swing is a bad idea.

凉城 2025-01-15 16:07:28

要将按钮锚定到内容窗格的左上角,您可以使用

gbc.anchor = GridBagConstraints.NORTHWEST;

或 等效的新符号,我相信您可以使用

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

To anchor the button to top left of the content pane, you can use

gbc.anchor = GridBagConstraints.NORTHWEST;

or equivalently in new notation, I believe you can use

gbc.anchor = GridBagConstraints.FIRST_LINE_START;
染火枫林 2025-01-15 16:07:28

您必须允许按钮自行移动:

setLayout(null);

将其放在创建按钮之前,它将允许您自由移动它们。

You have to allow the buttons to move on their own:

setLayout(null);

Put this before you create the button and it will allow you to move them freely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文