下面是一个利用按钮控制行为的实例,修改以下就可以满足你的要求
//MyUI.javaimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;class JP 
extends JPanel
implements ActionListener
{
public JP()  
{
//对象定义区
BUTFirst=new JButton("First");  
BUTSecond=new JButton("Second");
LBLInfo=new JLabel("Label1");
//对象显示区
add(BUTFirst);
add(BUTSecond);
add(LBLInfo);
//注册事件监听器
BUTFirst.addActionListener(this);
BUTSecond.addActionListener(this);
} public void actionPerformed(ActionEvent evt)
{
/*按事件对象的命令字段获取对象源,不利于处理多语言的国际程序
String source=evt.getActionCommand();
 if (source=="First")
 {
LBLInfo.setText("你按的是第一个按钮");
                                    //上面的这行代码按你的要求修改
 }
 else if (source=="Second")
 {
LBLInfo.setText("你按的是第二个按钮");
                                    //上面的这行代码按你的要求修改
 } repaint();*/
//按事件对象辨别事件源
Object sourceObj=evt.getSource(); if (sourceObj==BUTFirst)
{
LBLInfo.setText("你按的是第一个按钮");
}
else if (sourceObj==BUTSecond)
{
LBLInfo.setText("你按的是第二个按钮");
} repaint();
} /*public void paintConponent(Graphics g)
{

g.drawString("test Frame",0,0);
}*/ private JButton BUTFirst;
private JButton BUTSecond;
private JLabel LBLInfo;
};class FrmTest
extends JFrame

{
public FrmTest()
{
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System.exit(0);}
});//利用内部类实现窗口监听接口的适配器类
setSize(200,200);
setLocation(200,200);
Container content=getContentPane();
content.add(new JP());
}

};public class  MyUI
{
public static void main(String[] args) 
{
 FrmTest test=new FrmTest();
 test.show();
}
}
/*--by bookbobby(书呆)-+
 |            |
 |  你说爱我只是习惯  |
 |  再也不是喜欢    |
 |  我给你的爱     |
 |  已不再温暖     |
 |            |
 +--by bookbobby(书呆)-*/

解决方案 »

  1.   

    要用到多线程吧
    //MyUI.javaimport javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class MyUI{
        public static void main(String args[]){
            JF jf = new JF();
        }
    };class JF implements ActionListener,Runnable{
        private JFrame jf;
        private JButton jb;
        private JLabel jl;
        public JF(){
            jf = new JFrame("hello");       
            jb = new JButton("begin");
            jl = new JLabel("");
            Container cp = jf.getContentPane();
            cp.setLayout(new GridLayout(2,1));
            jb.addActionListener(this);
            cp.add(jb);
            cp.add(jl);
            jf.pack();
            jf.setVisible(true);        
        }
        public void actionPerformed(ActionEvent ae){
            if(ae.getActionCommand()=="begin"){
                jb.setText("stop");
                shouldStop = false;
                Thread t = new Thread(this);
                t.start();
                
            }else{
                jb.setText("begin");
                shouldStop = true;
            }
        }    
        
        private boolean shouldStop = false;    
        public void run(){
            int i=0;
            while(!shouldStop){
                i++;
                jl.setText(String.valueOf(i));     
                jf.repaint();     
            }
        }
        
    };
      

  2.   

    to  AYellow(北斗猪) 
    请问我在以JFrame为继承的frame中怎么调用新的线程
      

  3.   

    to  AYellow(北斗猪) 
    我先把分给你,希望能有回答
      

  4.   

    为什么AYellow(北斗猪) 这个程序无法调试跟踪?