程序中用Swing组件时应注意,任何java程序都是由主线程中的main()方法开始执行,当main()方法中的JFrame调用setVisiable(true) 后,引发了另一个线程——事件分派线程,而actionPerformed()方法及paintComponent()方法都是在事件分派Swing线程中被调用,所以如果在actionPerformed()方法中有大量的耗时操作,将会导致用户界面无法响应用户请求,造成用户界面锁死。在这种情况下,最好在 actionPerformed()方法中将耗时操作的程序部分移至一个新的Swing线程中实现,这样程序在进行大量计算的同时又可以及时地响应用户请求。
问题:在JFrame中调用actionPerformed(),如果方法执行时间过长,需要新开一个线程操作,否则用户界面会卡死
示例代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestSwingGUILock {
TestSwingGUILock() {
initGUI();
void costMoreTime() {
double d = 0; // 耗费大量时间的操作
for (int i = 0; i < Integer.MAX_VALUE; i++)
d += i;
System.out.println("I have do so many works!The result is" + d);
import
java
.awt.Image;
import
java
.awt.event.
Action
Event;
import
java
.awt.event.
Action
Listener;
import
java
.sql.SQLException;
import
java
.util.List;
import
java
x.
swing
.BorderFactory;
import
java
x.
swing
.Box;
import
java
x.
swing
.ImageIcon;
import
java
x.
swing
.JButton;
import
java
x.
swing
.JDialog;
import
java
x.
swing
.JFrame;
import
java
x.
swing
.JLabel;
import
java
x.
swing
.JOptionPane;
import
java
x.
swing
.JPanel;
import
java
x.
swing
.JPasswordField;
import
java
x.
swing
.JTextArea;
import
java
x.
swing
.JTextField;
import
java
x.xml.bind.util.ValidationEventCollector;
import com.shou.LoginUtil.LoginUser;
import com.shou.dao.LoginDao;
import com.shuo.util.ValidCode;
public class LoginFjame extends JFrame implements
Action
Listener {
private JFrame frame = new JFrame("登录");
private JPanel panel = new JPanel();
private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题
private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel
private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名
private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel
private JPasswordField passText = new JPasswordField(20); // 密码框隐藏
private JLabel verCodeLa = new JLabel("验证码:"); // 验证码
private JTextField inputCode = new JTextField(); // 验证码框
private ValidCode vcode = new ValidCode(); // 验证码内容
JTextField jt_code;
private JButton loginButton = new JButton("登录"); // 创建登录按钮
private JButton registerButton = new JButton("注 册"); // 创建注册按钮
private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮
private JButton exitButton = new JButton("退出");
JTextField field = null;
public LoginFjame() {
System.out.println("=====================================");
System.out.println("== 龍丶逸小说系统 ==");
System.out.println("== V1.1.1.0 ==");
System.out.println("=====================================");
WinLogin();
public void WinLogin() {
panel.setLayout(null); // 设置布局为 null
// 创建标题名称
this.tiel.setFont(new Font("宋体", 1, 20));
this.tiel.setBounds(150, 30, 300, 25);
this.panel.add(this.tiel);
// 创建 UserJLabel
this.userLabel.setFont(new Font("宋体", 1, 13));
this.userLabel.setBounds(70, 80, 80, 25);
this.panel.add(userLabel);
// 创建文本域用于用户输入
this.userText.setBounds(145, 80, 165, 25);
this.panel.add(this.userText);
// 注册
this.registerButton.setFont(new Font("宋体", 1, 15));
this.registerButton.setContentAreaFill
ed
(false);
this.registerButton.setBorderPaint
ed
(false);
/* registerButton.setBackground(Color.r
ed
); */
this.registerButton.setBounds(320, 80, 100, 25);
this.panel.add(this.registerButton);
// 变成小手
this.registerButton.setCursor(Cursor.getPr
ed
efin
ed
Cursor(Cursor.HAND_CURSOR));
// 创建PassJLabel
this.passLabel.setFont(new Font("宋体", 1, 13));
this.passLabel.setBounds(70, 110, 80, 25);
this.panel.add(this.passLabel);
// 密码输入框 隐藏
this.passText.setBounds(145, 110, 165, 25);
this.panel.add(this.passText);
// 忘记密码
this.newPasswordButton.setFont(new Font("宋体", 1, 15));
this.newPasswordButton.setContentAreaFill
ed
(false);
this.newPasswordButton.setBorderPaint
ed
(false);
/* registerButton.setBackground(Color.r
ed
); */
this.newPasswordButton.setBounds(320, 110, 100, 25);
this.panel.add(this.newPasswordButton);
// 变成小手
this.newPasswordButton.setCursor(Cursor.getPr
ed
efin
ed
Cursor(Cursor.HAND_CURSOR));
// 验证码code
this.verCodeLa.setFont(new Font("宋体", 1, 13));
this.verCodeLa.setBounds(70, 140, 80, 25);
this.panel.add(this.verCodeLa);
// 验证码框
this.inputCode.setBounds(145, 140, 165, 25);
this.panel.add(this.inputCode);
// 验证码图片
this.vcode.setBounds(320, 140, 165, 25);
this.panel.add(this.vcode);
System.out.println(this.vcode);
// 创建登录按钮
this.loginButton.setFont(new Font("宋体", 1, 15));
this.loginButton.setBounds(95, 190, 80, 25);
this.panel.add(this.loginButton);
// 变成小手
this.loginButton.setCursor(Cursor.getPr
ed
efin
ed
Cursor(Cursor.HAND_CURSOR));
// 退出按钮
this.exitButton.setFont(new Font("宋体", 1, 15));
this.exitButton.setBounds(230, 190, 80, 25);
this.panel.add(this.exitButton);
// 变成小手
this.exitButton.setCursor(Cursor.getPr
ed
efin
ed
Cursor(Cursor.HAND_CURSOR));
// 设置窗体的位置及大小
this.frame.setSize(460, 355);
frame.setLocationRelativeTo(null); // 在屏幕
中
居
中
显示
frame.add(this.panel); // 添加面板
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭
//设置按钮
this.registerButton.add
Action
Listener(this); //注册按钮
this.newPasswordButton.add
Action
Listener(this); //忘记密码
this.loginButton.add
Action
Listener(this); //登录
this.exitButton.add
Action
Listener(this); //退出
// 往窗体里放其他控件
frame.setVisible(true); // 设置窗体可见
@Override
public void
action
Perf
orm
ed
(
Action
Event e) {
JButton bt = (JButton) e.getSource();
// 获取按钮信息
String str = bt.getText();
// 获取用户名
String name = this.userText.getText().trim();
// 获取密码
String password = this.passText.getText().trim();
// 获取验证码
String code = this.inputCode.getText().trim();
// 获取jsp验证码
String vcode = this.vcode.getCode();
// 登录
if (str.equals("登录")) {
System.out.println("登录");
// 验证码转为大写
String Dcode = code.toUpperCase();
String Dvcode = vcode.toUpperCase();
// 验证码判断
if (Dcode.equals(Dvcode)) {
//获取页面的用户名
String username=this.userText.getText().trim();
// 根据用户名查看是否有该用户
try {
List loginUser=new LoginDao().queryAll(username);
String a=loginUser.toString();
System.out.println(a.toString());
if(!a.toString().equals("[]")){
//密码判断
String mysqlPasword=loginUser.get(0).l_password();
if(mysqlPasword.equals(password)){
//登录成功
JOptionPane pane = new JOptionPane("登录成功");
JDialog dialog = pane.creat
eD
ialog(this, "警告");
dialog.show();
}else{
JOptionPane pane = new JOptionPane("密码错误错误,请重新输入");
JDialog dialog = pane.creat
eD
ialog(this, "警告");
dialog.show();
}else{
JOptionPane pane = new JOptionPane("用户名错误,请重新输入");
JDialog dialog = pane.creat
eD
ialog(this, "警告");
dialog.show();
/*System.out.println(loginUser.toString());
String sqlUername=loginUser.get(0).getL_username();*/
/*int sqlpassword=loginUser.get(0).getL_power();*/
/*System.out.println("loginF:"+sqlUername);*/
} catch (SQLException e1) {
// TODO Auto-generat
ed
catch block
e1.printStackTrace();
} else {
JOptionPane pane = new JOptionPane("验证码错误,请重新输入");
JDialog dialog = pane.creat
eD
ialog(this, "警告");
System.out.println(dialog.getFont());
dialog.show();
} else
// 退出
if (str.equals("退出")) {
System.out.println("退出");
System.exit(0);
} else
// 注册
if (str.equals("注 册")) {
System.out.println("注 册");
// 注册
else if (str.equals("忘记密码")) {
System.out.println("忘记密码");
} else {
System.out.println("异常错误");
public boolean isValidCodeRight() {
System.out.println(this.jt_code.getText());
if (this.jt_code == null) {
return false;
if (this.vcode == null) {
return true;
if (this.vcode.getCode().equals(this.jt_code.getText())) {
return true;
return false;
public static void main(String[] args) {
new LoginFjame();
如果你想要的话,JButton的一些
action
Perf
orm
ed
()
方法
在按下JTextField里面的ENTER时执行,那么我猜你可以使用来自AbstractButton类的doClick()
方法
来实现这一点.虽然这种
方法
,可能会按下ENTER键覆盖JTextField的原始行为:(请看下面粘贴的代码,看看这是什么,适合您的需求:-) !!!import
java
.awt.*;import j...
我在
java
中
为simpleGUI编写了一个小代码.package guidemo1;import
java
.awt.event.
Action
Event;import
java
.awt.event.
Action
Listener;import
java
x.
swing
.JButton;import
java
x.
swing
.JFrame;public class GuiDemo1 implements ...
我正在创建一个需要一些简单输入的gui应用程序,但是,当我单击JFrame
中
的按钮时,我正在使用的
action
Perf
orm
ed
方法
不会被触发/触发(没有任何反应).我似乎无法弄清楚我错过了什么(如果有帮助,那就是
java
新手).感谢您的帮助/建议.这是所有代码://gui classpublic class guiUser extends JFrame implements
Action
List...
public void
action
Perf
orm
ed
(
Action
Event e) 这是接口
Action
Listener里面定义的一个抽象
方法
,所有实现这个接口的类都要重写这个
方法
。一般情况下,这是在编写GUI程序时,组件发生“有意义”的事件时会调用这个
方法
,比如按钮被按下,文本框内输入回车时都会触发这个事件,然后调用你编写的事件处理程序。实现过程大体如下:编写一个
Action
Listener类...
在
java
AWT
中
,事件源(如 按钮,滚动条)有一些
方法
,允许你注册时间监听器,对这些事件做出所需的响应
通知一个事件监听器发生了某件事件时,这个事件的相关信息会封装在一个事件对象
中
,在
Java
中
,所有的事件对象最终都派生于
Java
.util.EventObject类。当然每个事件类型还有子类,例如,
Action
Event,WindowE
我正在从教程
中
学习GUI,并且可以通过几个指针来解决当前无法正常工作的最佳
方法
.背景:我有一个包含main
方法
的类,它扩展了JFrame,并初始化了一个扩展JPanel(panel1)的类 .panel1包含一个硬编码的.txt文件URL,该文件的内容决定了帧
中
显示的内容(这很好) . 我正在尝试修改程序,以便用户可以从他们的机器
中
选择.txt文件...为此,我创建了一个名为panel2的第三个...
在写监听器类的时候里面都会有一个
action
Perf
orm
ed
方法
,可是从来没有(手动)调用过这个
方法
,那么它到底是怎么被调用的呢?上网查了一下,就像main
方法
一样,它是由系统调用的。
java
Swing
GUI 入门-图片和控件可视化觉得有用的话,欢迎一起讨论相互学习~加入一张图片一个Imagelabel 设置居
中
,命名为logoLabel,Custom Create 打勾.注意要保持图片的尺寸,要不然太大了把其他的内容都遮住了private void createUIComponents() {logoLabel = new JLabel();ImageIcon logoI...
但是对这种
方法
有一个特定的调用,只是它不在你的代码
中
,而是在JVM
中
. 按钮推动启动内部事件,引导JVM告诉按钮通知其所有侦听器已被推送 . 这将导致调用所有附加的
Action
Listener的
action
Perf
orm
ed
方法
.protect
ed
void fire
Action
Perf
orm
ed
(
Action
Event event)哪里通知所有已注册对此事件类型的通知感兴趣的听众 . 使用e...
内部类(inner class)定义内部类是定义在另一个类
中
的类。需要内部类的原因:内部类
方法
可以访问该类定义所在的域
中
的数据,包括私有的数据。内部类可以对同一个包
中
的其他类隐藏起来。当想要定义一个回调函数且不想编写大量代码时,使用匿名内部类比较便捷。四种内部类:1. 普通内部类1 class TalkClock {23 private int interval;4 private...
Action
Listener是一个接口,里面只有一个
方法
是
action
Perf
orm
ed
(),比如你有一个button bt,上面注册了2个
Action
Listener对象lis1, lis2,即bt.add
Action
Listener(lis1);bt.add
Action
Listener(lis2);当你点击一下这个button后,会有
java
的一个进程侦听到windows的这个消息,然
之前花了很多时间,找了很多
方法
,都没有把
Swing
插件给安装好,甚至几乎下载了NetBeans来进行尝试安装,利用其来进行
Swing
的练习。
经过一番努力,终于成功安装好了
Swing
插件,现在来分享一下我的安装经历。
二、安装
Swing
插件
1.在help
中
找到Eclipse marketplace
2.搜索Windowbuilder
3.直接安装即可
没错,就是这么简单,之前说需要找到对应的版本,官方文档上的说明也只有4.2版本之前的说明(可能没有更新,也可能新的这个插件很强,都兼容了)