当用户点击新JFrame
{{AdminFrame
按钮时,我正在加载addLocation
addUser
(在其上添加用户和JFrame
按钮) 1}}加载并获取用户信息。当信息提交给服务器时需要一些时间(因为服务器是基于网络的),在那段时间我的两个面板都卡住了...我想要的是让addUser
可点击和 use-able ,以便用户可以添加新用户或添加位置...
这是我的AdminFrame
添加按钮的模板代码...不写原始代码,因为原始代码太乱了..
AdminPanel
现在这里是我的AddUser Panel的adduser按钮Templete代码Orignal代码太乱了..
public class AdminPanel extends JFrame{
public AdminPanel(){
Initalize();//Do Initalize Stuff
}
public void addBtnActionPerformed(){
//load Child Form
}
}//End of class
答案 0 :(得分:4)
据我所知,你的问题是你没有在事件调度线程上创建你的GUI,而是你创建另一个JFrame
,你的任务可能很长初始线程 再次 - 因为第一个JFrame
也是在初始线程上创建的。
1)不要不必要地扩展JFrame
类,因为你可能想扩展另一个非常必要的类,但由于java中缺少多重继承而无法扩展。而是在JFrame
创建和实例,并在实例上调用方法,如:
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(..);
frame.add(..);
2)在Event Dispatch Thread上创建Swing组件,如下所示:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//create Swing and UI components here
}
});
3)见The Use of Multiple JFrames, Good/Bad Practice?。而是使用JDialog
,我认为这将是您的目标或CardLayout
(允许在单个JFrame
/ Container上的各种组件之间翻转。
4)在EDT上使用Swing Worker执行长时间运行的任务(否则GUI,包括应用程序的其他打开窗口,将无法响应)
答案 1 :(得分:4)
您需要使用SwingWorker将繁重/冗长的操作分配到另一个线程,允许UI-Thread(EDT)响应事件。
这是一个非常简单的代码,展示了如何使用SwingWorker(这里连接到服务器的连接是Thread.sleep()
):
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestJFrame implements ActionListener {
private JProgressBar progress;
private JButton startButton;
private JButton testButton;
private SwingWorker<Void, Integer> worker;
public void initUI() {
JFrame frame = new JFrame(TestJFrame.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
progress = new JProgressBar(0, 100);
startButton = new JButton("Start work");
testButton = new JButton("Test me while work is in progress");
startButton.addActionListener(this);
testButton.addActionListener(this);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(startButton);
buttonPanel.add(testButton);
frame.add(buttonPanel, BorderLayout.NORTH);
frame.add(progress, BorderLayout.SOUTH);
frame.setSize(600, 400);
frame.setVisible(true);
}
private void showTestDialog() {
if (worker != null) {
JOptionPane.showMessageDialog(testButton, "You made a test. See how I still respond while heavy job is in progress?");
} else {
JOptionPane.showMessageDialog(testButton,
"You made a test, but no job is progress. Hit the \"Start work\" button and hit me again after.");
}
}
private void startWork() {
if (worker != null) {
return;
}
startButton.setEnabled(false);
worker = new SwingWorker<Void, Integer>() {
@Override
protected Void doInBackground() throws Exception {
// Outside EDT, we cannot modify the UI, but we can perform lengthy operations
// without blocking the UI
for (int i = 0; i < 10; i++) {
publish(i * 10);
Thread.sleep(1000);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
// Inside EDT, here we can modify the UI
super.process(chunks);
// We only care about the last one
progress.setValue(chunks.get(chunks.size() - 1));
}
@Override
protected void done() {
// Inside EDT, he we can modify the UI
super.done();
progress.setValue(100);
startButton.setEnabled(true);
worker = null;
}
};
worker.execute();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
startWork();
} else if (e.getSource() == testButton) {
showTestDialog();
}
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestJFrame().initUI();
}
});
}
}