这个程序的要求是用paint方法显示一行字符串,包含“放大”和“缩小”按钮,当单击“放大”按钮时,显示的字符串字体放大一号,当单击“缩小”按钮时,显示的字符串字体缩小一号。
import java.awt.*;
import java.awt.event.*;
public class Change extends Frame implements ActionListener{
int size=20;
private Button big=new Button("放大");
private Button small=new Button("缩小");
private class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent we){
       System.exit(0);}
}
Font my=new Font("Dialog",Font.BOLD+Font.ITALIC,size);
String fonts="Hi,guy,best wishes"; public Change(){
super("this is change font");
setBackground(Color.green);
setup();big.addActionListener(this);
small.addActionListener(this);
addWindowListener(new WindowCloser());
show();
 setSize(300,300);
}
public void setup(){
Panel buttons=new Panel();
buttons.setLayout(new FlowLayout());
buttons.add(big);
buttons.add(small);
setLayout(new BorderLayout());
add("South",buttons);
}

public void paint(Graphics g){
g.setFont(my);
g.drawString("Dialog"+fonts,50,50);
}
public void xiao(){
size-=1;
}
public void da(){
size+=1;}public void actionPerformed(ActionEvent e){
if(e.getSource()==small)
  xiao();
else if(e.getSource()==big)
 da();
 repaint();
 }
 public static void main(String args[]){
 Change x=new Change();
 }
 }  

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;public class Change extends Frame implements ActionListener
    {
        int size = 44;
        private Button big = new Button("放大");
        private Button small = new Button("缩小");    private class WindowCloser extends WindowAdapter
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        }    Font my = new Font("Dialog", Font.BOLD + Font.ITALIC, size);
        String fonts = "Hi,guy,best wishes";    public Change()
        {
            super("this is change font");
            setBackground(Color.green);
            setup();        big.addActionListener(this);
            small.addActionListener(this);
            addWindowListener(new WindowCloser());
            show();
            setSize(300, 300);
        }    public void setup()
        {
            Panel buttons = new Panel();
            buttons.setLayout(new FlowLayout());
            buttons.add(big);
            buttons.add(small);
            setLayout(new BorderLayout());
            add("South", buttons);
        }    public void paint(Graphics g)
        {
            my=new Font("Dialog", Font.BOLD + Font.ITALIC, size);
            g.setFont(my);
            g.drawString("Dialog" + fonts, 50, 50);
        }    public void xiao()
        {
            size -= 1;
        }    public void da()
        {
            size += 1;
        }    public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == small)
                xiao();
            else if (e.getSource() == big)
                da();
            repaint();
        }    public static void main(String args[])
        {
            Change x = new Change();
        }
    }