我正在開發一個包含多個 JMenu 專案的程式,這些專案對應于不同的用戶需求。我已經在我的主框架中為他們分配了可敬的行動。我的問題是 EditUsers 對話框永遠回圈前一個 JDialog(AddUserDialog,它作業正常并回應被點擊,按預期作業),因此導致 Eclipse 凍結和終止。我要提前感謝任何會以任何可能的方式幫助我的好心人。
public class MainFrame extends JFrame implements ActionListener{
//------------------all variables and values have been assigned, JMenu Items are the focus------------
users = new JMenu("Users");
menuBar.add(users);
addUsers = new JMenuItem("Add User");
users.add(addUsers);
addUsers.addActionListener(this);
editUsers = new JMenuItem("Edit User");
users.add(editUsers);
editUsers.addActionListener(this);
auditUsers = new JMenuItem("Audit User");
users.add(auditUsers);
auditUsers.addActionListener(this);
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
//example for ActionListener in the first JMenuItem, working
if(ae.getSource() == interfaceOpt) {
JOptionPane.showMessageDialog(null, "Test");
System.out.println("Test");
}
//working
else if(ae.getSource()==sql){
System.out.println("Test1");
}
//working
else if(ae.getSource()==addUsers) {
try {
AddUserDialog addDialog=new AddUserDialog();
addDialog.setModalityType(ModalityType.APPLICATION_MODAL);
addDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}catch(Exception ex) {
ex.printStackTrace();
}
System.out.println("Test2");
}
-------------------------------------HERE IS THE ISSUE------------------------------------------
//not working, forever-looping the AddUserDialog class
else if(ae.getSource()==editUsers) {
try {
EditUsers editDialog=new EditUsers();
editDialog.setModalityType(ModalityType.APPLICATION_MODAL);
editDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}catch(Exception ex) {
ex.printStackTrace();
}
System.out.println("Test3");
}
我的背景關系截圖(JMenu 用戶):

點擊添加用戶:

最后,當我嘗試運行“編輯用戶選項”時,CPU 卡頓了。(所以沒有截圖)
目前我正在使用 AddUserDialog 類:
public class AddUserDialog extends JDialog implements ActionListener{
//a bunch of attributes
//do stuff
public AddUserDialog(){
//construct staff
}
}
EditUsers 類:
public class EditUsers extends AddUserDialog implements ActionListener{
//do stuff
public EditUsers{
//construct stuff
}
}
注意:這些類本身可以正常作業。如果人們有興趣,我將分享這兩個課程的內容,并大致了解我的程式的目的。
uj5u.com熱心網友回復:
我的示例有一個帶有兩個選單項的簡單框架。每個人都擁有一個不同的對話實體。對于您,請閱讀代碼中的注釋。我相信您將需要不同的對話框類,這些類必須遵循我將在下面的代碼中展示的模式
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class JDialogDemo extends JFrame {
public static void main (String[] args) {
JDialogDemo frame = new JDialogDemo();
JMenuBar menuBar = new JMenuBar();
JMenu usersMenu = new JMenu("Users");
menuBar.add(usersMenu);
JMenuItem addUserItem = new JMenuItem("Add User");
JMenuItem editUserItem = new JMenuItem("Edit User");
// Each menu item has its own listener to handle events
addUserItem.addActionListener(new MenuActionListener("Add User"));
editUserItem.addActionListener(new MenuActionListener("Edit User"));
usersMenu.add(addUserItem);
usersMenu.add(editUserItem);
frame.setJMenuBar(menuBar);
frame.setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/*
* My example is very simplistic. If you need to have different (custom)
* dialogs, each menu item must implement the class that wraps the
* dialog that pertains to it. For instance, you may have an "Add"
* dialog and an "edit" dialog. Each one, should follow this pattern.
*/
private static class MenuActionListener implements ActionListener {
private String title;
public MenuActionListener(String title) {
this.title = title;
}
@Override
public void actionPerformed (ActionEvent e) {
final JOptionPane optionPane = new JOptionPane(title,
JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
JMenuItem menuItem = (JMenuItem)e.getSource();
final JDialog dialog = new JDialog((JFrame)menuItem.getParent().getParent(), "Click a button", true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
optionPane.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (dialog.isVisible()
&& (e.getSource() == optionPane)
&& (prop.equals(JOptionPane.VALUE_PROPERTY))) {
dialog.setVisible(false);
}
}
});
dialog.pack();
dialog.setVisible(true);
}
}
}
此外,您必須記住,如果在添加新用戶或編輯現有用戶時執行的邏輯開銷很大,則需要在 UI 執行緒之外執行此邏輯。事實上,即使不是,在 Java Swing 用于處理 UI 事件的同一執行緒中執行后臺行程也是一個壞主意。因此,您必須在后臺執行這些行程。一種方便的方法是使用SwingWorker執行緒。您可能需要對此進行調查,并確保您的添加和編輯用戶行程是在單獨的SwingWorker執行緒中處理的。
更新:
這也不是運行 Swing 應用程式的最佳方式。您應該使用SwingUtilities#invokeLater()“創建和顯示”GUI。有很多關于如何做到這一點的例子。您甚至可能自己已經知道如何做到這一點。如果您需要這方面的幫助并希望我更新上面的代碼,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/508249.html
上一篇:垂直JScrollPane內的水平JScrollPane
下一篇:java.lang.NoClassDefFoundError:sun/reflect/Reflection從jar運行時
