java中通过按钮改变graphics 中的字体大小,为什么这么写没有效果?应该如何改正?
importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclasstestextendsApp...
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class test extends Applet implements ActionListener {
private int size = 12;
Button magnify;
Button lessen;
Font f = new Font("宋体",Font.BOLD ,size);
public void init(){
Panel buttons = new Panel();
magnify = new Button("放大");
lessen = new Button("缩小");
buttons.setLayout(new FlowLayout());
buttons.add(magnify);
buttons.add(lessen);
setLayout(new FlowLayout());
add(buttons);
magnify.addActionListener(this);
lessen.addActionListener(this);
}
public void paint(Graphics g){
//注意,判断时候一定是 == 而不是 = !
g.setFont(f);
g.drawString("Java is great!", 50, 100);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == magnify){
size ++;
repaint();
}else if(e.getSource() == lessen){
size --;
repaint();
}
}
} 展开
import java.awt.*;
import java.awt.event.*;
public class test extends Applet implements ActionListener {
private int size = 12;
Button magnify;
Button lessen;
Font f = new Font("宋体",Font.BOLD ,size);
public void init(){
Panel buttons = new Panel();
magnify = new Button("放大");
lessen = new Button("缩小");
buttons.setLayout(new FlowLayout());
buttons.add(magnify);
buttons.add(lessen);
setLayout(new FlowLayout());
add(buttons);
magnify.addActionListener(this);
lessen.addActionListener(this);
}
public void paint(Graphics g){
//注意,判断时候一定是 == 而不是 = !
g.setFont(f);
g.drawString("Java is great!", 50, 100);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == magnify){
size ++;
repaint();
}else if(e.getSource() == lessen){
size --;
repaint();
}
}
} 展开
2个回答
展开全部
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class test extends Applet implements ActionListener {
private int size = 12;
Button magnify;
Button lessen;
public void init(){
Panel buttons = new Panel();
magnify = new Button("放大");
lessen = new Button("缩小");
buttons.setLayout(new FlowLayout());
buttons.add(magnify);
buttons.add(lessen);
setLayout(new FlowLayout());
add(buttons);
magnify.addActionListener(this);
lessen.addActionListener(this);
}
public void paint(Graphics g){
//注意,判断时候一定是 == 而不是 = !
Font f = new Font("宋体",Font.BOLD ,size); //把字体对象放到这new
g.setFont(f);
g.drawString("Java is great!", 50, 100);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == magnify){
size ++;
repaint();
}else if(e.getSource() == lessen){
size --;
repaint();
}
}
}
//因为你这个applet 程序 加载时只初始化一次 你把字体对象放在成员变量的位置
那么初始化一次以后就永远不变了 你怎么改变size都不会重新new一个对象f出来
当然就不改变大小拉
import java.awt.*;
import java.awt.event.*;
public class test extends Applet implements ActionListener {
private int size = 12;
Button magnify;
Button lessen;
public void init(){
Panel buttons = new Panel();
magnify = new Button("放大");
lessen = new Button("缩小");
buttons.setLayout(new FlowLayout());
buttons.add(magnify);
buttons.add(lessen);
setLayout(new FlowLayout());
add(buttons);
magnify.addActionListener(this);
lessen.addActionListener(this);
}
public void paint(Graphics g){
//注意,判断时候一定是 == 而不是 = !
Font f = new Font("宋体",Font.BOLD ,size); //把字体对象放到这new
g.setFont(f);
g.drawString("Java is great!", 50, 100);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == magnify){
size ++;
repaint();
}else if(e.getSource() == lessen){
size --;
repaint();
}
}
}
//因为你这个applet 程序 加载时只初始化一次 你把字体对象放在成员变量的位置
那么初始化一次以后就永远不变了 你怎么改变size都不会重新new一个对象f出来
当然就不改变大小拉
追问
那应该怎么修改呢?
追答
我晕 我发的就是改好的啊····
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询