下面是一个利用按钮控制行为的实例,修改以下就可以满足你的要求
//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(书呆)-*/
//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(书呆)-*/
解决方案 »
- 关于io的初学问题
- JAVA
- 关于Swing JTable和Renderer,BasicTableUI,TableModel之间的相互关系
- 安装Apache时 network domain 应该添什么
- 有熟悉SGIP开发的吗?看了些文档,我现在是一头雾水,没有思路!请高人指点,感激不进!
- 求五子成线算法(棋盘type aa[19][19])(0,空格;1,黑子;2,白子)
- applet装载的时候如何让它显示进度条?
- 利用java API 对文件进行压缩的时候怎么样才能得到相对路径?
- 难道没人知道如何使用java访问lotus邮件的高手吗?
- 画图板代码问题
- 有没有见过这么莫名其妙的问题啊?
- 请教一个很基础基础的问题,请进。。。
//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();
}
}
};
请问我在以JFrame为继承的frame中怎么调用新的线程
我先把分给你,希望能有回答