彈窗(Dialog)的使用
阿新 • • 發佈:2022-04-09
彈窗視窗的建立:
1 //彈窗的視窗 2 class MyDialogDemo extends JDialog{ 3 public MyDialogDemo() { 4 this.setVisible(true); 5 this.setBounds(100,100,500,500); 6 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 7 8 Container container = this.getContentPane();9 container.setLayout(null); 10 11 container.add(new Label("1234")); 12 } 13 }
1 //主視窗 2 public class DialogDemo extends JFrame { 3 public static void main(String[] args) { 4 new DialogDemo(); 5 } 6 7 public DialogDemo() { 8 this.setVisible(true); 9 this.setSize(700,500); 10 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 11 12 //JFrame 放東西,容器 13 Container container = this.getContentPane(); 14 //絕對佈局 15 container.setLayout(null); 16 17 //按鈕 18 JButton button = newJButton("點選彈出一個對話方塊"); 19 button.setBounds(30,30,200,50); 20 21 //點選這個按鈕時候,彈出一個彈窗 22 button.addActionListener(new ActionListener() { 23 public void actionPerformed(ActionEvent e) { 24 new MyDialogDemo(); 25 } 26 }); 27 28 container.add(button); 29 } 30 }