将 JButton 向右对齐
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您当前的布局管理器 (
GridLayout
)正在创建,包含 3 行和一列。因此,添加到JFrame
的组件将从上到下垂直显示。更糟糕的是,GridLayout
将在所有 3 个组件之间均分配空间,这意味着您的按钮将向两个方向拉伸,这几乎肯定不是你需要。我会考虑使用替代布局管理器。对于简单的布局,我倾向于使用
BorderLayout
或
FlowLayout
。对于更复杂的布局,我倾向于
GridBagLayout
,尽管还有其他人更喜欢
MigLayout
。更多信息请参见此处。
Your current layout manager (
GridLayout
) is being created with 3 rows and a single column. Hence, the components you add to theJFrame
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
orFlowLayout
. For more complex layouts I lean towardsGridBagLayout
although there are others who preferMigLayout
.More information here.
尝试这样:
Try like this:
一种快速而肮脏的方法是将按钮[或者直接包裹按钮的容器,如果您想在按钮旁边添加其他组件]使用
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 theBorderLayout.EAST
layout constraint for that button [or wrapping container].您将
GridLayout
设置为JFrame
构造函数而不是JPanel
(JPanel 默认情况下具有FlowLayout
),我认为请注意--->但当前
ComponentOrientations
中的GridLayout
从左到右开始,然后是第三个。网格为空
,然后仅添加
JFrame#add(JPanel)
,根据您的情况you set
GridLayout
to theJFrame
constructor instead ofJPanel
(JPanel has by defaultFlowLayout
), I think thatnotice ---> but
GridLayout
in currentComponentOrientations
to start from left to right,then 3rd. grid is empty
then only to add
JFrame#add(JPanel)
, in your case