![](/img/trans.png)
[英]How do I swap the left hand and right hand sides of a C/C++ assignment statement in gvim?
[英]Aligning JComponents to left- and right-hand sides of a JPanel
我有一個包含兩個JComponents的JPanel,比如兩個JButton,btnLeft和btnRight。 我希望這兩個按鈕水平對齊,我希望btnLeft位於JPanel的左側,而btnRight位於JPanel的右側,兩者之間留有任何空間。
我知道我可以通過添加一個水平支柱在 BoxLayout中做到這一點,我必須在其中指定其間的空間量 ,但必須有一個更簡單的方法,而不必指定其間的剩余空間。
我該怎么做呢?
聽起來像horizontalGlue就是你要找的東西:
JComponent comp = new JPanel();
comp.setLayout(new BoxLayout(comp, BoxLayout.LINE_AXIS));
comp.add(new JLabel("left"));
comp.add(Box.createHorizontalGlue());
comp.add(new JLabel("right"));
如果您不介意垂直拉伸按鈕,為什么不嘗試:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JFrame1 {
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton btn1 = new JButton("Btn1");
JButton btn2 = new JButton("Btn2");
frame.setLayout(new BorderLayout());
frame.setSize(500, 400);
frame.add(btn1, BorderLayout.WEST);
frame.add(btn2, BorderLayout.EAST);
frame.show();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.