package test.swing.layout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class TestAbsoluteLayout extends JFrame { private JPanel contentPane; public static void main(String[] args) { TestAbsoluteLayout frame = new TestAbsoluteLayout(); frame.setVisible(true); } public TestAbsoluteLayout(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); //(左邊界,上邊界,宽度,長度) contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(10, 10, 10, 10)); //(上,左,下,右) setContentPane(contentPane); //當Layout設定為null代表以絕對路徑方式描述components之間的關係 contentPane.setLayout(null); JButton btnNewButton = new JButton("提交"); btnNewButton.setBounds(10, 10, 87, 23); //(左邊界,上邊界,宽度,長度) contentPane.add(btnNewButton); JLabel lblNewLabel = new JLabel("顯示文字標籤!"); lblNewLabel.setBounds(300, 237, 100, 15); //(左邊界,上邊界,宽度,長度) contentPane.add(lblNewLabel); } }
執行結果
沒有留言:
張貼留言