给你一个例子,应该能回答你的问题package com.usys.AWT1;import java.awt.*;public class TextAreaTest extends Frame { public TextAreaTest() {
this.setTitle("Test TextArea");
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(bold = new Checkbox("Bold"));
p.add(italic = new Checkbox("Italic"));
this.add("South",p);
this.add("East",new Scrollbar(Scrollbar.VERTICAL));
this.add("North",new Scrollbar(Scrollbar.HORIZONTAL));
fox = new fox2Canvas();
this.add("Center",fox);
} public boolean action(Event evt,Object arg){
if (evt.target.equals(bold)||evt.target.equals(italic))
{
int m =(bold.getState()? Font.BOLD:0) + (italic.getState()?Font.ITALIC:0);
fox.setFont(m);
}
else return super.action(evt,arg);
return true;
} public boolean handleEvent(Event evt){
if (evt.id == Event.WINDOW_DESTROY) System.exit(0);
return super.handleEvent(evt);
} public static void main(String[] args){
Frame tf = new TextAreaTest();
tf.resize(300,200);
tf.show();
} private fox2Canvas fox;
private Checkbox bold;
private Checkbox italic;
}class fox2Canvas extends Canvas{
public fox2Canvas(){
this.setFont(Font.PLAIN);
} public void setFont(int m){
setFont(new Font("Helvetica",m,18));
repaint();
} public void paint(Graphics g){
g.drawString("fdsfdsfdsfdsfdsf",0,50);
}}
this.setTitle("Test TextArea");
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(bold = new Checkbox("Bold"));
p.add(italic = new Checkbox("Italic"));
this.add("South",p);
this.add("East",new Scrollbar(Scrollbar.VERTICAL));
this.add("North",new Scrollbar(Scrollbar.HORIZONTAL));
fox = new fox2Canvas();
this.add("Center",fox);
} public boolean action(Event evt,Object arg){
if (evt.target.equals(bold)||evt.target.equals(italic))
{
int m =(bold.getState()? Font.BOLD:0) + (italic.getState()?Font.ITALIC:0);
fox.setFont(m);
}
else return super.action(evt,arg);
return true;
} public boolean handleEvent(Event evt){
if (evt.id == Event.WINDOW_DESTROY) System.exit(0);
return super.handleEvent(evt);
} public static void main(String[] args){
Frame tf = new TextAreaTest();
tf.resize(300,200);
tf.show();
} private fox2Canvas fox;
private Checkbox bold;
private Checkbox italic;
}class fox2Canvas extends Canvas{
public fox2Canvas(){
this.setFont(Font.PLAIN);
} public void setFont(int m){
setFont(new Font("Helvetica",m,18));
repaint();
} public void paint(Graphics g){
g.drawString("fdsfdsfdsfdsfdsf",0,50);
}}
1.我使用AWT编写Application,我在windowClosing()里写入以下两条语名中的任何一条
MyFrame.setVisialbe(false);或System.exit(0);想实现Frame窗口的关闭。但是不论我点击右上角的关闭按钮,还是左上角系统菜单中的关闭选项都关不了。应该如何关闭Application的Frame窗口?
2.如果要为单击一个按钮增加处理程序,可不可以不使用actionPerformed()而使用mousePressed()或mouseClicked()
2.如果要为单击一个按钮增加处理程序,可不可以不使用actionPerformed()而使用mousePressed()或mouseClicked()
MyFrame.setVisialbe(false);或System.exit(0);想实现Frame窗口的关闭。但是不论我点击右上角的关闭按钮,还是左上角系统菜单中的关闭选项都关不了。应该如何关闭Application的Frame窗口?
2.如果要为单击一个按钮增加处理程序,可不可以不使用actionPerformed()而使用mousePressed()或mouseClicked()
public void windowClosing(WindowEvent e){
System.exit(0);
}
}import javax.swing.JFrame;
public class ExitFrame extends JFrame{
public ExitFrame(){
super("Frame Title");
setSize(100,140);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ExitWindow exit = new ExitWindow();
addWindowListener(exit);
setVisible(true);
}
public static void main(String[] args){
ExitFrame sf = new ExitFrame();
}
}