sayhello对话框练习

xiaoxiao2021-02-28  39

package sayHello; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { JButton bt1 =new JButton("说你好"); JLabel lb =new JLabel(); public Main() { setLayout(new FlowLayout()); add(bt1); add(lb); this.setSize(300,200); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); bt1.addActionListener(new BtListener()); } class BtListener implements ActionListener{ public void actionPerformed(ActionEvent e) { Child dlg =new Child(Main.this); dlg.setSize(200,100); dlg.setTitle("模态对话框"); dlg.setLocationRelativeTo(null);//定位在显示器正中间 dlg.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); dlg.setModal(true);//设置模态对话框 dlg.lb.setText("你好");//向对话框传值 dlg.setVisible(true); } } public static void main(String[] args) { new Main(); } } package sayHello; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Child extends JDialog { Main main =null;//表示探出此对话框的主窗口,由主窗口创建对话框时传递过来 JLabel lb =new JLabel(); public Child(Main main) { this.main=main;//主窗口传来后,即可对主窗口及其组件进行操作 setLayout(new FlowLayout()); add(lb); } }
转载请注明原文地址: https://www.6miu.com/read-2623970.html

最新回复(0)