600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > java实用教程——组件及事件处理——对话框(dialog)

java实用教程——组件及事件处理——对话框(dialog)

时间:2021-10-09 09:44:27

相关推荐

java实用教程——组件及事件处理——对话框(dialog)

对话框:

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.*;import java.awt.event.*;public class DialogDemo1 {public static void main(String[] args) {Frame frame = new Frame("这里测试Dialog");Dialog d1 = new Dialog(frame, "模式对话框", true);Dialog d2 = new Dialog(frame, "非模式对话框", false);Button b1 = new Button("打开模式对话框");Button b2 = new Button("打开非模式对话框");//设置对话框的大小和位置d1.setBounds(20,30,300,400);d2.setBounds(20,30,300,400);//给b1和b2绑定监听事件b1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {d1.setVisible(true);}});b2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {d2.setVisible(true);}});//把按钮添加到frame中frame.add(b1,BorderLayout.NORTH);frame.add(b2,BorderLayout.SOUTH);//设置WindowListener,监听用户点击X的动作,如果点击X,则关闭窗口frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {//停止当前程序System.exit(0);}});//设置frame最佳大小并可见frame.pack();frame.setVisible(true);}}

import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class DialogDemo2 {public static void main(String[] args) {Frame frame = new Frame("这里测试Dialog");//1.创建两个对话框Dialog对象,一个模式的,一个非模式的Dialog d1 = new Dialog(frame, "模式对话框", true);//创建一个垂直的Box容器,把一个文本框和一个按钮添加到Box容器中Box vBox = Box.createVerticalBox();vBox.add(new TextField(20));vBox.add(new Button("确认"));//把Box容器添加到Dialog中d1.add(vBox);//2.通过setBounds方法设置Dialog的位置以及大小d1.setBounds(20,30,300,200);//3.创建两个按钮Button b1 = new Button("打开模式对话框");//4.给这两个按钮添加点击后的行为b1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {d1.setVisible(true);}});//5.把按钮添加到frame中frame.add(b1,BorderLayout.NORTH);//设置WindowListener,监听用户点击X的动作,如果点击X,则关闭窗口frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {//停止当前程序System.exit(0);}});frame.setBounds(100,100,400,400);//设置frame最佳大小并可见frame.pack();frame.setVisible(true);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。