!!!!!そして、私は大きな問題を発見しました.すでに定義済みのisModalメソッドを上書きしているため、ユーザー入力を保持して待機しません! 今すぐ顔を平手打ちしてください:)!!!!!!!!
学校で GUI に関するプロジェクトを行っています。現時点での主なトピックは 4 層アーキテクトに関するものです。ここでは、JDialog を使用して人物に関する情報を入力し、それによってプログラムの JFrame 内の JList を更新する必要があります。
私の問題は、ユーザーの追加ボタンを使用すると、JDialog が表示とモーダルの両方に設定されるため、ユーザーが JDialog の追加ボタンを押した後、JFrame の JList の更新を待機する必要があることです。
しかし、しかし、それはうまくいきません。私はそれについて多くの教師と話しましたが、なぜうまくいかないのか理解できません。
追加情報は、私が mac でプログラミングしているということですが、プログラムが windows マシンで実行されている場合は機能します。
Web で特定の問題を検索してみましたが、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ビルド1.7.0_09-b05で実行
編集#2:スーパーをJDialogに設定し、JFrameをスーパーとして設定しようとしました。時代遅れですが、setModalを試しました。JDialogを呼び出すときにモーダルを設定しようとしましたが、まだ成功しません