JButton 的对齐问题

发布于 2024-10-04 20:55:43 字数 1473 浏览 0 评论 0原文

这可能是在深夜发布的一个非常愚蠢的问题。我正在尝试创建两个 JButton,一个放在另一个之上。但由于某种原因,它没有正确对齐。底部按钮 b2 的左边缘稍微位于顶部底部 b1 左边缘的左侧。

这是代码:

class thistrial extends JPanel implements ActionListener
 {
 ...
    public thistrial()
    {
     .....  
    add(new JSeparator(SwingConstants.HORIZONTAL));

    //ADD THE START AND STOP BUTTONS
    Border raisedBorder = BorderFactory.createRaisedBevelBorder();


    b1 = new JButton("START");
    b1.setVerticalTextPosition(AbstractButton.CENTER);
    b1.setHorizontalTextPosition(AbstractButton.CENTER);
    b1.setPreferredSize(new Dimension(220,100));
    add(new JSeparator(SwingConstants.HORIZONTAL));

    b2 = new JButton("STOP");
    b2.setPreferredSize(new Dimension(220,100));
    b2.setVerticalTextPosition(AbstractButton.CENTER);
    b2.setHorizontalTextPosition(AbstractButton.CENTER);
             add(b1);  
    add(b2);  
          .............
}
   }
/** MAIN function **/
 public static void main(String args[])

 {
        //Create and set up the window.
        JFrame frame = new JFrame();
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setSize(398,480); 
    frame.setLocation(300,200);
    frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    //Create and set up the content pane.
    thistrial newContentPane = new thistrial();
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.setVisible(true);
}

我能做什么呢?

This might be a very stupid question posted at the dead of the night. I am trying to create two JButtons placed one on top of the other. However for some reason, it is not getting properly aligned. The left edge of the bottom button b2 appears slightly to the left of the left edge of top bottom b1.

Here is the code:

class thistrial extends JPanel implements ActionListener
 {
 ...
    public thistrial()
    {
     .....  
    add(new JSeparator(SwingConstants.HORIZONTAL));

    //ADD THE START AND STOP BUTTONS
    Border raisedBorder = BorderFactory.createRaisedBevelBorder();


    b1 = new JButton("START");
    b1.setVerticalTextPosition(AbstractButton.CENTER);
    b1.setHorizontalTextPosition(AbstractButton.CENTER);
    b1.setPreferredSize(new Dimension(220,100));
    add(new JSeparator(SwingConstants.HORIZONTAL));

    b2 = new JButton("STOP");
    b2.setPreferredSize(new Dimension(220,100));
    b2.setVerticalTextPosition(AbstractButton.CENTER);
    b2.setHorizontalTextPosition(AbstractButton.CENTER);
             add(b1);  
    add(b2);  
          .............
}
   }
/** MAIN function **/
 public static void main(String args[])

 {
        //Create and set up the window.
        JFrame frame = new JFrame();
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setSize(398,480); 
    frame.setLocation(300,200);
    frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    //Create and set up the content pane.
    thistrial newContentPane = new thistrial();
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.setVisible(true);
}

What can I do about it?

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

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

发布评论

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

评论(2

寄与心 2024-10-11 20:55:43

您的示例扩展了 JPanel,默认情况下使用 FlowLayout。相反,您可以尝试 GridLayout,如 所示布局管理器视觉指南

Your example extends JPanel, which uses FlowLayout by default. Instead, you might try GridLayout, as shown in A Visual Guide to Layout Managers.

摘星┃星的人 2024-10-11 20:55:43

我建议使用正确的布局管理器。你的代码没有显示任何内容。

I'd suggest using the right layout manager. Your code shows none.

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