来先看段代码,其中有错误。import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Panel;public class FontInApplet extends Applet implements ActionListener
{
private Button big = new Button("放大");
private Button small = new Button("缩小");
private String str = "this is my first applet program"; public void init(){
Panel panel = new Panel(new FlowLayout());
setLayout(new BorderLayout());
panel.add(big);
panel.add(small);
add(panel,BorderLayout.NORTH); big.addActionListener(this);
small.addActionListener(this); setSize(300,300);
setVisible(true);
} public void actionPerformed(ActionEvent e){
//Graphics g = new Graphics();
if(e.getSource()==big){
g.setFont(new Font(str,Font.PLAIN,14));
}
if(e.getSource()==small){
g.setFont(new Font(str,Font.PLAIN,10));
}
}
public void paint(Graphics g){
g.drawString(str,150,150);
}
}想了很多方法,但是都无法正确改变字体。还有,paint方法何时调用?不胜感激。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Panel;public class FontInApplet extends Applet implements ActionListener
{
private Button big = new Button("放大");
private Button small = new Button("缩小");
private String str = "this is my first applet program"; public void init(){
Panel panel = new Panel(new FlowLayout());
setLayout(new BorderLayout());
panel.add(big);
panel.add(small);
add(panel,BorderLayout.NORTH); big.addActionListener(this);
small.addActionListener(this); setSize(300,300);
setVisible(true);
} public void actionPerformed(ActionEvent e){
//Graphics g = new Graphics();
if(e.getSource()==big){
g.setFont(new Font(str,Font.PLAIN,14));
}
if(e.getSource()==small){
g.setFont(new Font(str,Font.PLAIN,10));
}
}
public void paint(Graphics g){
g.drawString(str,150,150);
}
}想了很多方法,但是都无法正确改变字体。还有,paint方法何时调用?不胜感激。
解决方案 »
- 帮忙看看这个小程序的错误在哪里--运行通过,不能输出
- 求算法,请大家进来看看 谢谢了
- 关于随机取值的问题???高手指教!!
- 「50分求助贴」Servlet相关,太简单了,就是我不会!!!
- 如何用java实现查看一个目录的当前使用和未使用空间的情况!
- 给大家提供一段在GUI设计当中,当toolbar太长了如何自动换行的代码
- 请问各位高手!!在WIN2000中运行JAVA程序!!一下子运行完了,还没有看到结果有何办法可以延迟关闭
- 一个很简单的问题,我不知道,请各位帮忙。
- 在LINUX下CLASS文件如何执行!
- 谁对java的图形技术比较熟悉的帮帮忙。
- 关于hashset的问题
- unicode中文字符和英文字符的疑惑
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Button;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Panel;public class Test extends Applet implements ActionListener
{
private Button big = new Button("放大");
private Button small = new Button("缩小");
private String str = "this is my first applet program"; public void init(){
Panel panel = new Panel(new FlowLayout());
setLayout(new BorderLayout());
panel.add(big);
panel.add(small);
add(panel,BorderLayout.NORTH); big.addActionListener(this);
small.addActionListener(this); setSize(300,300);
setVisible(true);
} public void actionPerformed(ActionEvent e){
Graphics g = this.getGraphics();
if(e.getSource()==big){
this.setFont(new Font(str,Font.PLAIN,20));
repaint();
}
if(e.getSource()==small){
this.setFont(new Font(str,Font.PLAIN,10));
repaint();
}
}
public void paint(Graphics g){
g.drawString(str,150,150);
}
}我看了下api,g.drawString 是用当前的字体,所以用this.setFont(),可以啦