难题, java swing 里的按钮点击问题 MouseListener,ActionListener 都不能动态改变组件内容。
最近需要写一个小的swing,需要在点击按钮后处理一些逻辑,并把处理结果实时在textArea显示。无法实现,最后发现Button的MouseListener,Actio...
最近需要写一个小的swing,需要在点击按钮后处理一些逻辑,并把处理结果实时在 textArea显示。无法实现,最后发现Button的MouseListener,ActionListener 事件监听并不会实时改变组件内容,它其实是记录改变的内容,在事件结束后才改变组件。
下面是用一个按钮点击事件 改变JLablede的完整代码。请问高手如果能实现动态改变。谢谢~
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ChangeFrame extends JFrame{
static JLabel jl ;
/**
* @param args
*/
public static void main(String[] args) {
ChangeFrame changeFrame = new ChangeFrame();
changeFrame.setSize(300, 300);
changeFrame.setContentPane(greatPane());
changeFrame.setVisible(true);
}
private static Container greatPane() {
JPanel jp = new JPanel();
jl = new JLabel("00000");
JButton jbu = new JButton("gg");
// jbu.addMouseListener(new MouseListener() {
//@Override
//public void mouseReleased(MouseEvent e) {
//}
//@Override
//public void mousePressed(MouseEvent e) {
//// TODO Auto-generated method stub
//
//}
//
//@Override
//public void mouseExited(MouseEvent e) {
//// TODO Auto-generated method stub
//
//}
//
//@Override
//public void mouseEntered(MouseEvent e) {
//// TODO Auto-generated method stub
//}
//
//@Override
//public void mouseClicked(MouseEvent e) {
//// TODO Auto-generated method stub
//}
//});
jbu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
th.start();
jl.setText("1111");
try {
Thread.sleep(1000L);
jl.setText("2222");
Thread.sleep(1000L);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
});
jp.add(jl);
jp.add(jbu);
return jp;
}
static Thread th = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
jl.setText("2222");
}};
} 展开
下面是用一个按钮点击事件 改变JLablede的完整代码。请问高手如果能实现动态改变。谢谢~
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ChangeFrame extends JFrame{
static JLabel jl ;
/**
* @param args
*/
public static void main(String[] args) {
ChangeFrame changeFrame = new ChangeFrame();
changeFrame.setSize(300, 300);
changeFrame.setContentPane(greatPane());
changeFrame.setVisible(true);
}
private static Container greatPane() {
JPanel jp = new JPanel();
jl = new JLabel("00000");
JButton jbu = new JButton("gg");
// jbu.addMouseListener(new MouseListener() {
//@Override
//public void mouseReleased(MouseEvent e) {
//}
//@Override
//public void mousePressed(MouseEvent e) {
//// TODO Auto-generated method stub
//
//}
//
//@Override
//public void mouseExited(MouseEvent e) {
//// TODO Auto-generated method stub
//
//}
//
//@Override
//public void mouseEntered(MouseEvent e) {
//// TODO Auto-generated method stub
//}
//
//@Override
//public void mouseClicked(MouseEvent e) {
//// TODO Auto-generated method stub
//}
//});
jbu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
th.start();
jl.setText("1111");
try {
Thread.sleep(1000L);
jl.setText("2222");
Thread.sleep(1000L);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
});
jp.add(jl);
jp.add(jbu);
return jp;
}
static Thread th = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
jl.setText("2222");
}};
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询