将 JButton 向右对齐

发布于 2024-12-28 06:21:32 字数 914 浏览 1 评论 0原文

我正在用 java 创建一个界面,我想将按钮向右对齐。我已经尝试过,但它不起作用。有人可以告诉我该怎么做吗?

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Button_Alignment extends JFrame{
    public JPanel header,body,footer;
    public JButton add1;
    public JButton save;
    public Button_Alignment(){
        super("BUTTON");
        GridLayout g1 = new GridLayout(3,1);
        setLayout(g1);
        //////
        header = new JPanel();
        JButton add1 = new JButton("add");
        header.add(add1);
        JButton save = new JButton("save");
        header.add(save);
        //////
        add(header);
        header.setBackground(Color.cyan);
    }
    public static void main(String[] args){
        Button_Alignment ba = new Button_Alignment();
        ba.setSize(400, 400);
        ba.setVisible(true);
    }
}

I am creating an interface in java and i want to align the button to the right. I have try but its not working. Can someone tell me how to do it?

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Button_Alignment extends JFrame{
    public JPanel header,body,footer;
    public JButton add1;
    public JButton save;
    public Button_Alignment(){
        super("BUTTON");
        GridLayout g1 = new GridLayout(3,1);
        setLayout(g1);
        //////
        header = new JPanel();
        JButton add1 = new JButton("add");
        header.add(add1);
        JButton save = new JButton("save");
        header.add(save);
        //////
        add(header);
        header.setBackground(Color.cyan);
    }
    public static void main(String[] args){
        Button_Alignment ba = new Button_Alignment();
        ba.setSize(400, 400);
        ba.setVisible(true);
    }
}

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

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

发布评论

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

评论(4

几味少女 2025-01-04 06:21:32

您当前的布局管理器 (GridLayout )正在创建,包含 3 行和一列。因此,添加到 JFrame 的组件将从上到下垂直显示。更糟糕的是,GridLayout 将在所有 3 个组件之间分配空间,这意味着您的按钮将向两个方向拉伸,这几乎肯定不是你需要。

我会考虑使用替代布局管理器。对于简单的布局,我倾向于使用 BorderLayoutFlowLayout。对于更复杂的布局,我倾向于 GridBagLayout,尽管还有其他人更喜欢 MigLayout

更多信息请参见此处

Your current layout manager (GridLayout) is being created with 3 rows and a single column. Hence, the components you add to the JFrame will appear vertically from top to bottom. Worse still, GridLayout will aportion space equally amongst all 3 components, meaning that your buttons will stretch in both directions, which is almost certainly not what you require.

I would consider using an alternative layout manager. For simple layouts I tend to favour BorderLayout or FlowLayout. For more complex layouts I lean towards GridBagLayout although there are others who prefer MigLayout.

More information here.

从﹋此江山别 2025-01-04 06:21:32

尝试这样:

JButton save = new JButton ("save");
setLayout (new BorderLayout ());
add (save, BorderLayout.EAST);

Try like this:

JButton save = new JButton ("save");
setLayout (new BorderLayout ());
add (save, BorderLayout.EAST);
我不是你的备胎 2025-01-04 06:21:32

一种快速而肮脏的方法是将按钮[或者直接包裹按钮的容器,如果您想在按钮旁边添加其他组件]使用 BorderLayout 并为该按钮[或包装容器]使用 BorderLayout.EAST 布局约束的容器。

A quick and dirty way is to put the button [or a container immediatly wrapping the button, if you want to add other components on the right next to your button] it in a container that uses the BorderLayout and use the BorderLayout.EAST layout constraint for that button [or wrapping container].

守不住的情 2025-01-04 06:21:32

您将 GridLayout 设置为 JFrame 构造函数而不是 JPanel (JPanel 默认情况下具有 FlowLayout),我认为请

header.setLayout(new GridLayout(3,1));
header.add(add1);
header.add(save);

注意--->但当前 ComponentOrientations 中的 GridLayout 从左到右开始,然后是第三个。网格为空

,然后仅添加 JFrame#add(JPanel),根据您的情况

add(header);

you set GridLayout to the JFrame constructor instead of JPanel (JPanel has by default FlowLayout), I think that

header.setLayout(new GridLayout(3,1));
header.add(add1);
header.add(save);

notice ---> but GridLayout in current ComponentOrientations to start from left to right, then 3rd. grid is empty

then only to add JFrame#add(JPanel), in your case

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