⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialogexp.java

📁 Java的swing的课堂练习。。 。
💻 JAVA
字号:
/**
 * @(#)DialogExp.java
 *
 *
 * @author 
 * @version 1.00 2007/11/12
 */
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class MyDialog extends JDialog implements ActionListener {
	static final int YES=1,NO=0;
	int message=-1;
	JButton yes,no;
	MyDialog(JFrame f,String s,boolean b) {
	 super(f,s,b);
	 yes=new JButton("Yes");
	 yes.addActionListener(this);
	 no=new JButton("No");
	 no.addActionListener(this);
	 setLayout(new FlowLayout());
	 add(yes);
	 add(no);
	 setBounds(60,60,100,100);
	 addWindowListener(new WindowAdapter()
	     { public void windowClosing(WindowEvent e)
	       { message=-1;
	         setVisible(false);
	       }
	   }
	   );
	}
	public void actionPerformed(ActionEvent e) {
	 if (e.getSource()==yes) 
	 { message=YES;
	   setVisible(false);
	 }
	 else if (e.getSource()==no)
	 { message=NO;
	   setVisible(false);
	 }
	}
	public int getMessage()
	{ return message;
	}
}
class Dwindow extends JFrame implements ActionListener {
 JTextArea text;
 JButton button;
 MyDialog dialog;
 Dwindow(String s) {
  super(s);
  text=new JTextArea(5,22);
  button=new JButton("打开对话框");
  button.addActionListener(this);
  setLayout(new FlowLayout());
  add(button);
  add(text);
  dialog=new MyDialog(this,"我有模式",true);
  
  setBounds(60,60,300,300);
  setVisible(true);
  validate();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public void actionPerformed(ActionEvent e) {
 	if (e.getSource()==button)
 	{ dialog.setVisible(true);
 	  if (dialog.getMessage()==MyDialog.YES)
 	  	text.append("\n你单击了对话框的yes按钮");
 	  else if (dialog.getMessage()==MyDialog.NO)
 	  	text.append("\n你单击了对话框的no按钮");
 	  else if (dialog.getMessage()==-1)
 	  	text.append("\n你单击了对话框的关闭图标");
 	}
 }
}
public class DialogExp {
        
    /**
     * Creates a new instance of <code>DialogExp</code>.
     */
    public DialogExp() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new Dwindow("Window with dialog");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -