弹窗(Dialog)的使用
弹窗窗口的建立:
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 = new JButton("点击弹出一个对话框"); 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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义