???

import java.awt.*;
import java.awt.event.*;
import java.swing.*;
import java.util.*;
class counter extends Observable{
private int val=0;
public counter(int v){this.val=v;}
public void inc(){
val++;
setChanged();
notifyObservers();
}
public void dec(){
val--;
setChanged();
notifyObservers();
}
public int getCounter(){return val;}}class view extends JFrame implements Observer{
Container content;
JLable jl=new JLable("1");
public view(){
content=getContentPane();
content.setLayout(new FlowLayout());
content.add(jl);
setSize(400,300);
setVisible(true);
}
public void update(Observable ob,Object arg){
int v=((counter)ob).getVal();
jl.setText(v);
repaint();
}}public class test extends JFrame implements ActionListener{
Container c;
JLable jl=new JLable();
JButton jb1=new JButton("inc");
JButton jb2=new JButton("dec");
counter ct=new counter(1);
public test(){
c=getContentPane();
c.setLayout(new FlowLayout());
c.add(jl);
c.add(jb1);
c.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
view v=new view();
ct.addObserver(v);
setSize(300,200);
setVisible(true);}public void actionPerformed(ActionEvent event){
Object o=e.getSource();
if(o==jb1){
ct.inc();
jl.setText(((counter)o).getVal());
}else if(o==jb2){
ct.dec();
jl.setText(((counter)o).getVal());
}}
public static void main(String[] args){
new test();
}
}

解决方案 »

  1.   

    哧哧,上面错太多,现在好了
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    class counter extends Observable{
    private int val=0;
    public counter(int v){this.val=v;}
    public void inc(){
    val++;
    setChanged();
    notifyObservers();
    }
    public void dec(){
    val--;
    setChanged();
    notifyObservers();
    }
    public int getVal(){return val;}}class view extends JFrame implements Observer{
    Container content;
    JLabel jl=new JLabel("1");
    public view(){
    content=getContentPane();
    content.setLayout(new FlowLayout());
    content.add(jl);
    setSize(400,300);
    setVisible(true);
    }
    public void update(Observable ob,Object arg){
    int v=((counter)ob).getVal();
    jl.setText("value="+v);
    repaint();
    }}public class test extends JFrame implements ActionListener{
    Container c;
    JLabel lj=new JLabel("val=  1");
    JButton jb1=new JButton("inc         ");
    JButton jb2=new JButton("dec          ");
    counter ct=new counter(1);
    public test(){
    c=getContentPane();
    c.setLayout(new FlowLayout());
    c.add(lj);
    c.add(jb1);
    c.add(jb2);
    jb1.addActionListener(this);
    jb2.addActionListener(this);
    view v=new view();
    ct.addObserver(v);
    setSize(100,200);
    setVisible(true);
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){System.exit(0);}
    });}public void actionPerformed(ActionEvent e){
    Object o=e.getSource();
    if(o==jb1){
    ct.inc();
    int l=ct.getVal();
    lj.setText("value="+l);
    }
    if(o==jb2){
    ct.dec();
    int l=ct.getVal();
    lj.setText("value="+l);
    }
    repaint();
    }
    public static void main(String[] args){
    new test();
    }
    }
      

  2.   

    你要是实在无聊就写个notebook玩玩