package cn.jjw.dzr.awt;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AwtTest
{
public static void main(String[] args)
{
Component com = new Frame("wo de chuangkou");
com.setSize(400, 300);
com.setLocation(200, 100);
com.setBounds(300, 200, 400, 300);
com.setVisible(true);
((Frame)com).addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e)
{
super.windowClosing(e);
System.out.println(e);
System.exit(1);
}
});
((Frame)com).setLayout(new FlowLayout());
Button button =new Button("按钮");
((Frame)com).add(button);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
System.out.println("ActionPerformed:"+e);
final Dialog dialog = new Dialog(new Frame());
//这个Frame代表什么呢。
//代表dialog的父窗口么。
dialog.setTitle("Antoher");
dialog.setVisible(true);
dialog.setLocation(100, 200);
dialog.setSize(300, 300);
dialog.setBackground(Color.BLUE);
}
});
button.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
System.out.println("Click: "+e.getButton());
final Dialog dialog = new Dialog(new Frame());
//这个Frame代表什么呢。
//代表dialog的父窗口么。
dialog.setTitle("Hello");
dialog.setVisible(true);
dialog.setLocation(300, 200);
dialog.setSize(300, 300);
dialog.setBackground(Color.BLUE);
dialog.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e)
{
// TODO Auto-generated method stub
// super.windowClosing(e);
System.out.println(e);
// dialog.setVisible(false);
//为什么执行上面的语句,系统会提示我需要将dialog修改成final;
//如何设置关闭该对话框?
//dialog没有destory或者exit方法。
//而且
dialog.dispose();
}
});
}
@Override
public void mousePressed(MouseEvent e)
{
System.out.println("Pressed: "+e.getButton());
}
@Override
public void mouseReleased(MouseEvent e)
{
System.out.println("Release: "+e.getButton());
}
@Override
public void mouseEntered(MouseEvent e)
{
System.out.println("Entered: "+e.getButton());
}
@Override
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub
}
});
}
}
such as the above code :
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
System.out.println("ActionPerformed:"+e);
final Dialog dialog = new Dialog(new Frame());
//这个Frame代表什么呢。
//代表dialog的父窗口么。
dialog.setTitle("Antoher");
dialog.setVisible(true);
dialog.setLocation(100, 200);
dialog.setSize(300, 300);
dialog.setBackground(Color.BLUE);
}
});
and
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
System.out.println("Click: "+e.getButton());
final Dialog dialog = new Dialog(new Frame());
//这个Frame代表什么呢。
//代表dialog的父窗口么。
dialog.setTitle("Hello");
dialog.setVisible(true);
dialog.setLocation(300, 200);
dialog.setSize(300, 300);
dialog.setBackground(Color.BLUE);
dialog.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e)
{
// TODO Auto-generated method stub
// super.windowClosing(e);
System.out.println(e);
// dialog.setVisible(false);
//为什么执行上面的语句,系统会提示我需将dialog修改成final;
//如何设置关闭该对话框?
//dialog没有destory或者exit方法。
//而且
dialog.dispose();
}
seemingly will play the same effect.
------ Solution ---------------------------------------- ----
usually I have used actionlistener rarely used mouseClick, specific differences is not clear , it may depend on the source code for these two classes , and passing under study
------ Solution - ------------------------------------------
button ActionListener either response has focus , press the Enter / spacebar , and can respond to mouse clicks.
The best option is to use ActionListener.
------ For reference only -------------------------------------- -
better choice is to use the Action to create a JButton.
没有评论:
发表评论