怎么做的拖動窗口內部的按鈕始終居中呢?
很簡單把按鈕放入Box中進行了。
代碼如下:
import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; public class Frame_Box4 { public static void main(String[] agrs) { JFrame frame=new JFrame("Java示例程序"); Box b1=Box.createHorizontalBox(); //創建橫向Box容器 frame.add(b1); //將外層橫向Box添加進窗體 b1.add(Box.createVerticalStrut(200)); //添加高度為200的垂直框架 b1.add(new JButton("左")); //添加按鈕1 b1.add(Box.createHorizontalStrut(40)); //添加長度為40的水平框架 b1.add(new JButton("右")); //添加按鈕2 b1.add(Box.createHorizontalGlue()); //添加水平膠水 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100,100,400,200); frame.setVisible(true); } }
拖動窗口結果如下:
原文鏈接:http://c.biancheng.net/view/1212.html