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);
}
}
麻烦大家看一下,这个程序,为什么加了红色的两行后就发现错误,但是如果删除这两行,则运行出来的东西是空白,真是头疼。
----------------解决方案--------------------------------------------------------
看不懂是要实现什么功能!
----------------解决方案--------------------------------------------------------
报告什么错?。
----------------解决方案--------------------------------------------------------
报告是对的
但是只要把那些java.awt中设计界面的语句删掉了,就可以了,不知道是不是awt只能用于applet中而不能用于application
----------------解决方案--------------------------------------------------------
不要删什么语句,楼主的程序应该没什么问题
PS:我没有编译,
还有,你的处理画图的代码不应该写到actionPerformed,而应该写到paint或者paintComponent方法里去
----------------解决方案--------------------------------------------------------
这些方法是类中自带的方法吗?
----------------解决方案--------------------------------------------------------
哪些方法
----------------解决方案--------------------------------------------------------
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);
}
Graphics g=getGraphics(); 方法有这么调用的么?
----------------解决方案--------------------------------------------------------
可以的啊,我再书上看到的可以用drawString但是其他的好象没见到过,我是说paint()这类方法不是自定义的,而是类自带的是吗
----------------解决方案--------------------------------------------------------
不知道你的程序要做什么的,这样改了一下,没错了,你看着办吧,你在用“{”符号时最好注意一点,你漏了
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 test
{
public static void main(String args[])
{
myframe frame1=new myframe();
frame1.setVisible(true);
}
}
----------------解决方案--------------------------------------------------------