我有一个非常奇怪的行为,我只能断定它一定是某个地方的 Java 错误。
我在构造函数中更改了一行
super(parent, "Production Plan Export", ModalityType.MODELESS);
至
super(parent, "Production Plan Export", ModalityType.APPLICATION_MODAL);
突然,当我单击按钮打开我的 JDialog 时,它打开了两次,第一次,它根本没有响应。我需要单击 X 按钮来关闭窗口,然后当我这样做时,会出现相同的 JDialog,突然我的所有按钮都打开了。
有没有人见过这种行为?
我在用Java 1.6.0_33
编辑 非常非常奇怪的是,当我尝试在 eclipse 中调试它并在我的构造函数设置断点,然后我转到下一行时,它突然跳转到我在类中的变量并开始遍历我的变量构造函数中的下一行。
我试图重新启动我的计算机和eclipse,但这没有用。我会看看我是否可以创建一个小测试用例
编辑2:
好的,所以我创建了一个可以为我重现它的小应用程序。请注意,我试图删除不相关的代码部分,因此有很多代码对这个测试用例无效。
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class ProductionPlanExportDialog extends JDialog {
private JProgressBar progressbar;
private JLabel message;
private JButton exportButton;
private JButton helpButton;
private int state = JOptionPane.CANCEL_OPTION;
private final Action closeAction = new CloseAction();
private final Action enableExport = new EnableExport();
private boolean updating = false;
private JButton closeButton;
public ProductionPlanExportDialog(Window parent) {
super(parent, "Test", ModalityType.APPLICATION_MODAL); //The JDialog is not centered, and the close button doesn't work
// super(parent, "Test", ModalityType.MODELESS); //The close button works and the jdialog is centered
initGUI();
bindModel();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
close();
}
});
}
public static void main(String args[]) {
new ProductionPlanExportDialog(null);
}
public void initGUI() {
JTabbedPane mainTabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel yAxisPanel = new JPanel();
yAxisPanel.setLayout(new BoxLayout(yAxisPanel, BoxLayout.Y_AXIS));
JPanel progressPanel = new JPanel(new FlowLayout());
progressPanel.add(getProgressbar());
yAxisPanel.add(progressPanel);
yAxisPanel.add(getMessage());
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(getExportButton());
closeButton = new JButton("Close");
buttonPanel.add(closeButton);
buttonPanel.add(getHelpButton());
yAxisPanel.add(buttonPanel);
setSize(937, 605);
getContentPane().add(yAxisPanel);
setVisible(true);
setLocationRelativeTo(null);
}
private void bindModel() {
closeButton.setAction(closeAction);
}
public int getExitStatus() {
return state;
}
private JProgressBar getProgressbar() {
if (progressbar == null) {
progressbar = new JProgressBar();
progressbar.setPreferredSize(new Dimension(1000, 22));
progressbar.setName("progressbar");
progressbar.setStringPainted(true);
progressbar.setVisible(false);
}
return progressbar;
}
private JLabel getMessage() {
if (message == null) {
message = new JLabel();
message.setName("message");
}
return message;
}
private void setUIState() {
updating = true;
assert SwingUtilities.isEventDispatchThread();
try {
closeButton.setEnabled(false);
} finally {
updating = false;
}
}
private void setTextFieldValue(JFormattedTextField textField, long value) {
if(value == Long.MAX_VALUE || value == -Long.MAX_VALUE) {
textField.setText("");
} else {
textField.setValue(value);
}
}
public JButton getExportButton() {
if (exportButton == null) {
exportButton = new JButton();
exportButton.setToolTipText("Preview production plan");
exportButton.setName("exportButton");
exportButton.setText("Preview");
exportButton.setBounds(305, 640, 72, 21);
exportButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
return exportButton;
}
private JButton getHelpButton() {
if (helpButton == null) {
helpButton = new JButton();
helpButton.setBounds(470, 640, 72, 21);
helpButton.setName("helpButton");
helpButton.setText("Help");
helpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String helpText = "This dialog is used to preview and export a production plan.<br/>" +
"<h1>Preview</h1>"
+ "After pressing 'Export', the production plan will be generated. This can take a few minutes.";
JEditorPane helpEditorPane = new JEditorPane("text/html", helpText);
helpEditorPane.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
helpEditorPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(helpEditorPane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
helpEditorPane.setCaretPosition(0);
scrollPane.setPreferredSize(new Dimension(640,445));
JOptionPane.showMessageDialog(helpButton, scrollPane, "Production Plan Export Dialog Help", JOptionPane.INFORMATION_MESSAGE);
}
});
}
return helpButton;
}
private final class EnableExport extends AbstractAction {
@Override
public void actionPerformed(final ActionEvent e) {
if(isAllCheckboxSelected()) {
getExportButton().setText("Export");
getExportButton().setToolTipText("Export production plan");
} else {
getExportButton().setText("Preview");
getExportButton().setToolTipText("Preview production plan");
}
}
}
boolean enableExport() {
return isAllCheckboxSelected();
}
boolean isAllCheckboxSelected() {
return false;
}
private final class CloseAction extends AbstractAction {
public CloseAction() {
super("Close");
}
@Override
public void actionPerformed(final ActionEvent e) {
close();
}
}
private void close() {
state = JOptionPane.OK_OPTION;
setVisible(false);
dispose();
}
}