gpt4 book ai didi

java - SwingUtilities.invokeLater : Issues

转载 作者:行者123 更新时间:2023-12-02 06:37:56 26 4
gpt4 key购买 nike

我从来不理解SwingUtilities.invokeLater,这就是我一直避免使用它直到现在的原因。但是确保 Swing Thread 安全非常重要。好吧,请原谅,因为这是我第一次这样做。我试图在一些数据库查询后关闭窗口。所以,据我所知,UI 中的任何更新都必须由 SwingUtilities.invokeLater 处理。这是我的工作:

主要内容:

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new forminsert().setVisible(true);
forminsert f=new forminsert();
}
});

}
public forminsert() {
initComponents();
}

public class forminsert extends javax.swing.JFrame {
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);

jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jPanel1.setBorder(javax.swing.BorderFactory.createMatteBorder(3, 3, 3, 3, new java.awt.Color(0, 204, 204)));
/////***REST CODE***/////

}

添加按钮上的监听器

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)               {                                         
new Thread(new thread1()).start();

}
public class thread1 implements Runnable
{

public void run() {
insert ins=new insert();
////code for inserting///
}
}

浏览按钮上的监听器

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)                {                                         
JFileChooser ss=new JFileChooser();
////Code to choose the file////
}

这里是问题所在,代码退出。

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

forminsert f=new forminsert();f.call();
}

public void call()
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
jPanel1.setVisible(false);
getRootPane().setVisible(false);

}
});

屏幕: screen short

请解释我哪里出错了,我不想使用 System.exit,请原谅我问了太多问题。谢谢。

更新:

这是@trashgod 要求的示例工作代码

package faltur;

import java.io.File;
import javax.swing.JFileChooser;

public class insert extends javax.swing.JFrame {

/** Creates new form insert */
public insert() {
initComponents();
}


@SuppressWarnings("unchecked")

private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);

jPanel1.setForeground(new java.awt.Color(240, 240, 240));

jButton1.setText("Browse..");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("exit");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(31, 31, 31)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(153, 153, 153)
.addComponent(jButton2)))
.addContainerGap(99, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(66, 66, 66)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(87, 87, 87)
.addComponent(jButton2)
.addContainerGap(94, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser ss=new JFileChooser();
ss.showOpenDialog(jTextField1);

File f=ss.getSelectedFile();

path=f.toString();
System.out.println(path);

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
insert in=new insert();
in.exit();/////////////CALLS exit()/////////////////modify this//////
}


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new insert().setVisible(true);
}
});
}
public void exit()////////////////////////MODIFY THIS///////////////////
{
jPanel1.getRootPane().setVisible(false);

}

private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;

public String path="";
}

请运行并告知如何修改 exit() 以便它关闭窗口。再次感谢。

最佳答案

因为您的框架的默认关闭操作是EXIT_ON_CLOSE,您可以通过dispatchEvent()发送WindowEvent.WINDOW_CLOSING,如图here .给定一个 JFrame f

private static final String EXIT = "Exit";
private Action exit = new AbstractAction(EXIT) {

@Override
public void actionPerformed(ActionEvent e) {
f.dispatchEvent(new WindowEvent(
f, WindowEvent.WINDOW_CLOSING));
}
};
private JButton b = new JButton(exit);

另见 How to Use Actions并遵循常见的coding conventions .

附录:根据修改后的代码,这是一种退出程序的方法。注意类名通常以大写字母开头,而实例名以小写字母开头。

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
insert.dispatchEvent(new WindowEvent(
insert, WindowEvent.WINDOW_CLOSING));
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
insert = new Insert();
insert.setVisible(true);
}
});
}

private static Insert insert;
...

关于java - SwingUtilities.invokeLater : Issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com