!!!!!我发现了一个大问题,我正在覆盖已经预定义的 isModal 方法,因此它不会保持并等待用户输入!现在打自己的脸:)!!!!!
我在学校做一个关于 GUI 的项目。目前的主要话题是4层架构师的东西,这里我们应该使用一个JDialog来输入一个人的信息,从而在程序的JFrame中更新一个JList。
我的问题是,当我使用我的添加人按钮时,JDialog 被设置为可见和模态,因此它应该等待在用户按下 JDialog 上的添加按钮后更新 JFrame 上的 JList。
但是但是但是,它不起作用,我和我的很多老师谈过它,他们无法弄清楚为什么它不起作用。
额外的信息是我在 mac 上编程,但如果程序在 windows 机器上运行,它就可以工作。
我尝试在网上搜索具体问题,但找不到任何与 mac 上的 JDialog java 问题相关的内容。
有没有人有过这样的经历?
JFrame:
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import model.Person;
import service.Service;
public class MainFrame extends JFrame {
private JList<Person> jlList;
private JScrollPane jsrPane;
private JButton addButton, updateButton;
private Controller controller;
private MyDialog myDialog;
public MainFrame(){
controller = new Controller();
this.setTitle("Marks lille MainFrame.");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setLocation(300, 200);
this.setLayout(null);
jlList = new JList<>();
jlList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jsrPane = new JScrollPane(jlList);
jsrPane.setBounds(10, 10, 400, 500);
this.add(jsrPane);
addButton = new JButton("Add Person");
addButton.setBounds(420, 110, 100, 25);
addButton.addActionListener(controller);
this.add(addButton);
updateButton = new JButton("Update");
updateButton.setBounds(420, 150, 100, 25);
updateButton.addActionListener(controller);
this.add(updateButton);
}
private class Controller implements ActionListener{
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(addButton)){
myDialog = new MyDialog("Add Person");
myDialog.setVisible(true);
System.out.println("Test");
if(myDialog.isModal()){
jlList.setListData(Service.getPersons().toArray(new Person[0]));
}
System.out.println("Test");
}else if(ae.getSource().equals(updateButton)){
Person ps = jlList.getSelectedValue();
myDialog = new MyDialog("Update Person");
myDialog.setVisible(true);
if(myDialog.isModal()){
Service.removePerson(ps);
jlList.setListData(Service.getPersons().toArray(new Person[0]));
}
}
}
}
}
JDialog:
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import service.Service;
public class MyDialog extends JDialog {
private JButton okButton, cancelButton;
private JLabel lblName, lblTitle;
private JTextField txfName, txfTitle;
private JCheckBox ckbRetire;
private Controller controller;
private boolean modalResult;
public MyDialog(String title){
controller = new Controller();
this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
this.setTitle(title);
setModalityType(JDialog.DEFAULT_MODALITY_TYPE);
this.setLayout(null);
this.setSize(300, 300);
this.setLocation(300, 200);
okButton = new JButton("Add");
okButton.setBounds(100, 250, 80, 25);
okButton.addActionListener(controller);
this.add(okButton);
cancelButton = new JButton("Cancel");
cancelButton.setBounds(180, 250, 80, 25);
cancelButton.addActionListener(controller);
this.add(cancelButton);
lblName = new JLabel("Name:");
lblName.setBounds(10, 10, 100, 25);
this.add(lblName);
txfName = new JTextField();
txfName.setBounds(5, 35, 200, 25);
this.add(txfName);
lblTitle = new JLabel("Title:");
lblTitle.setBounds(10, 70, 100, 25);
this.add(lblTitle);
txfTitle = new JTextField();
txfTitle.setBounds(5, 95, 200, 25);
this.add(txfTitle);
ckbRetire = new JCheckBox("retired");
ckbRetire.setBounds(10, 150, 100, 25);
this.add(ckbRetire);
}
private class Controller implements ActionListener{
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(okButton)){
String name = txfName.getText().trim();
String title = txfTitle.getText().trim();
boolean retired = ckbRetire.isSelected();
Service.createPerson(name, title, retired);
modalResult = true;
MyDialog.this.setVisible(false);
}else if(ae.getSource().equals(cancelButton)){
modalResult = false;
MyDialog.this.setVisible(false);
}
}
}
public boolean isModal(){
return modalResult;
}
}
主要方法:
package gui;
public class MainFrameApp {
/**
* @param args
*/
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
}
如果您需要 Service、Dao 和 Model 类,请告诉我。
编辑 #1:在 Java build 1.7.0_09-b05 上运行
编辑 #2:尝试将 super 设置为 JDialog,并将 JFrame 设置为 super 。 尝试过 setModal,尽管它已经过时了。尝试在调用 JDialog 时设置模式,仍然没有成功
最佳答案
我发现了一个大问题,我正在覆盖已经预定义的 isModal 方法,因此它不会保留并等待用户输入!现在打自己的脸 :)
关于java - Mac OS JDialog 不会暂停执行,即使是模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286553/