import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// 定义jpanel
class mypanel extends JPanel implements ActionListener
{
private JButton bt;
public mypanel()
{
bt=new JButton("the one");
bt.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
Graphics g=getGraphics();
Object src=evt.getSource();
if(src.equals(bt))
Font f=new Font("Serif",Font.PLAIN,15);
setBackground(Color.yellow);
g.setFont(f);
g.setColor(Color.red);
g.drawString("you are right",20,20);
}
}
// 定义JFrame
class myframe extends JFrame implements WindowListener
{
public mypanel p1=new mypanel();
public myframe()
{
setSize(300,300);
setTitle("the first application");
setLocation(200,200);
Container pane =getContentPane();
pane.add(p1);
addWindowListener(this);
}
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
public void windowClosed(WindowEvent evt)
{
}
public void windowOpened(WindowEvent evt)
{
}
public void windowIconified(WindowEvent evt)
{
}
public void windowDeiconified(WindowEvent evt)
{
}
public void windowActivated(WindowEvent evt)
{
}
public void windowDeactivated(WindowEvent evt)
{
}
}
//定义main 函数
public class a0
{
public static void main(String args[])
{
myframe frame1=new myframe();
frame1.setVisible(true);
}
}
麻烦大家看一下,这个程序,为什么加了红色的两行后就发现错误,但是如果删除这两行,则运行出来的东西是空白,真是头疼。