JDialog 没有出现
JDialog does not appear
该程序大部分运行正常,但无法打开任何 window。它应该在桌面右下角显示一个小对话框。但是对于另一个人来说,编译相同的代码没有 problems.We 具有相同的 Java 运行时(1.8_u40)。我该如何解决这个问题?
我把代码放在下面:
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ProgressDialog {
private JDialog dialogFrame;
private JProgressBar progressBar;
private JLabel headingLabel;
private Uploader callerUploader;
public ProgressDialog() {
dialogFrame = new JDialog();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException ex) {
System.err.println(ex.toString());
}
dialogFrame.setSize(200, 50);
dialogFrame.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.insets = new Insets(5, 5, 5, 5);
headingLabel = new JLabel();
Font f = headingLabel.getFont();
f = new Font(f.getFontName(), Font.BOLD, f.getSize());
headingLabel.setFont(f);
headingLabel.setOpaque(false);
dialogFrame.add(headingLabel, constraints);
dialogFrame.setUndecorated(true);
// Bottone
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx = 0;
constraints.weighty = 0;
JButton xButton = new JButton("X");
xButton.setMargin(new Insets(1, 4, 1, 4));
xButton.setFocusable(false);
dialogFrame.add(xButton, constraints);
// Progress bar
constraints.gridx = 0;
constraints.gridy = 1;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.gridwidth = 2;
progressBar = new JProgressBar();
progressBar.setMaximum(100);
progressBar.setMinimum(0);
Dimension dim = new Dimension();
dim.width = 130;
dim.height = 20;
progressBar.setMinimumSize(dim);
progressBar.setStringPainted(true);
progressBar.setBorderPainted(true);
dialogFrame.add(progressBar, constraints);
xButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialogFrame.dispose();
stoppedUploaderClose();
}
});
}
private void autoPosition() {
// Per il posizionamento in basso a destra
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
// altezza taskbar
Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(dialogFrame.getGraphicsConfiguration());
dialogFrame.setLocation(scrSize.width - 5 - dialogFrame.getWidth(), scrSize.height - 5 - toolHeight.bottom
- dialogFrame.getHeight());
}
public void destroy() {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(500);
for (float i = 1.00f; i >= 0; i -= 0.01f) {
dialogFrame.setOpacity(i);
Thread.sleep(15);
}
dialogFrame.dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
public void setUploader(Uploader callerUploader) {
this.callerUploader = callerUploader;
}
public void set(int n) {
progressBar.setValue(n);
progressBar.setString(n + "");
}
public void setMessage(String headingLabel) {
this.headingLabel.setText(headingLabel);
autoPosition();
dialogFrame.setShape(new RoundRectangle2D.Double(1, 1, 200, 50, 20, 20));
dialogFrame.setVisible(true);
}
public void setWait() {
headingLabel.setText("Waiting link...");
}
public void close() {
dialogFrame.dispose();
}
public void stoppedUploaderClose() {
try {
callerUploader.stopUpload();
} catch (Exception e) {
e.printStackTrace();
}
}
}
您正在从后台线程中进行 Swing 调用以更改 GUI 的 Swing 组件的状态,这可能而且将会导致不可预测的错误。最好使用 Swing 计时器而不是 Thread.sleep(...)
,因为计时器的 ActionListener 中的所有代码都在 Swing 事件线程上调用。
还有,我想知道
- 上传器 class 所做的,...可能会创建很长的 运行 代码,这引出了一个问题,它是否在后台线程中被适当地调用?这看起来是 SwingWorker 的好地方。
- 如何调用此对话框代码?
要获得更直接的帮助,请考虑创建并发布一个 sscce or a minimal example program/mcve,将您的代码压缩到仍然可以编译和运行的最小位,没有外部依赖性(例如需要 link数据库或图像),没有与您的问题无关的额外代码,但仍能说明您的问题。
编辑:好的,我通常不这样做,但我在您的 GitHub project link, and as I suspected, Uploader does long-running code on the Swing event thread, and makes calls to this class you've posted above. I suggest that you use a SwingWorker for long-running code, that you use its setProgress(...)
method to change its progress state from 0 to 100, and that you use a PropertyChangeListener to listen for changes to this state, and then set the JProgressBar's value based on this. It will take a lot of work on your part, but will be well worth it. For the details, look at: Lesson: Concurrency in Swing
中查看了您的代码
我有一些示例 SwingWorker 程序,您可以在这里找到:
- Cant get JProgressBar to update from SwingWorker class
- MVC Progress Bar Threading
- How do I make my SwingWorker example work properly?
- JProgressBar without JButton and PropertyChangeListener
- Understanding Java ExecutorService
该程序大部分运行正常,但无法打开任何 window。它应该在桌面右下角显示一个小对话框。但是对于另一个人来说,编译相同的代码没有 problems.We 具有相同的 Java 运行时(1.8_u40)。我该如何解决这个问题?
我把代码放在下面:
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ProgressDialog {
private JDialog dialogFrame;
private JProgressBar progressBar;
private JLabel headingLabel;
private Uploader callerUploader;
public ProgressDialog() {
dialogFrame = new JDialog();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException ex) {
System.err.println(ex.toString());
}
dialogFrame.setSize(200, 50);
dialogFrame.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.insets = new Insets(5, 5, 5, 5);
headingLabel = new JLabel();
Font f = headingLabel.getFont();
f = new Font(f.getFontName(), Font.BOLD, f.getSize());
headingLabel.setFont(f);
headingLabel.setOpaque(false);
dialogFrame.add(headingLabel, constraints);
dialogFrame.setUndecorated(true);
// Bottone
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx = 0;
constraints.weighty = 0;
JButton xButton = new JButton("X");
xButton.setMargin(new Insets(1, 4, 1, 4));
xButton.setFocusable(false);
dialogFrame.add(xButton, constraints);
// Progress bar
constraints.gridx = 0;
constraints.gridy = 1;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.gridwidth = 2;
progressBar = new JProgressBar();
progressBar.setMaximum(100);
progressBar.setMinimum(0);
Dimension dim = new Dimension();
dim.width = 130;
dim.height = 20;
progressBar.setMinimumSize(dim);
progressBar.setStringPainted(true);
progressBar.setBorderPainted(true);
dialogFrame.add(progressBar, constraints);
xButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialogFrame.dispose();
stoppedUploaderClose();
}
});
}
private void autoPosition() {
// Per il posizionamento in basso a destra
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
// altezza taskbar
Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(dialogFrame.getGraphicsConfiguration());
dialogFrame.setLocation(scrSize.width - 5 - dialogFrame.getWidth(), scrSize.height - 5 - toolHeight.bottom
- dialogFrame.getHeight());
}
public void destroy() {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(500);
for (float i = 1.00f; i >= 0; i -= 0.01f) {
dialogFrame.setOpacity(i);
Thread.sleep(15);
}
dialogFrame.dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
public void setUploader(Uploader callerUploader) {
this.callerUploader = callerUploader;
}
public void set(int n) {
progressBar.setValue(n);
progressBar.setString(n + "");
}
public void setMessage(String headingLabel) {
this.headingLabel.setText(headingLabel);
autoPosition();
dialogFrame.setShape(new RoundRectangle2D.Double(1, 1, 200, 50, 20, 20));
dialogFrame.setVisible(true);
}
public void setWait() {
headingLabel.setText("Waiting link...");
}
public void close() {
dialogFrame.dispose();
}
public void stoppedUploaderClose() {
try {
callerUploader.stopUpload();
} catch (Exception e) {
e.printStackTrace();
}
}
}
您正在从后台线程中进行 Swing 调用以更改 GUI 的 Swing 组件的状态,这可能而且将会导致不可预测的错误。最好使用 Swing 计时器而不是 Thread.sleep(...)
,因为计时器的 ActionListener 中的所有代码都在 Swing 事件线程上调用。
还有,我想知道
- 上传器 class 所做的,...可能会创建很长的 运行 代码,这引出了一个问题,它是否在后台线程中被适当地调用?这看起来是 SwingWorker 的好地方。
- 如何调用此对话框代码?
要获得更直接的帮助,请考虑创建并发布一个 sscce or a minimal example program/mcve,将您的代码压缩到仍然可以编译和运行的最小位,没有外部依赖性(例如需要 link数据库或图像),没有与您的问题无关的额外代码,但仍能说明您的问题。
编辑:好的,我通常不这样做,但我在您的 GitHub project link, and as I suspected, Uploader does long-running code on the Swing event thread, and makes calls to this class you've posted above. I suggest that you use a SwingWorker for long-running code, that you use its setProgress(...)
method to change its progress state from 0 to 100, and that you use a PropertyChangeListener to listen for changes to this state, and then set the JProgressBar's value based on this. It will take a lot of work on your part, but will be well worth it. For the details, look at: Lesson: Concurrency in Swing
我有一些示例 SwingWorker 程序,您可以在这里找到:
- Cant get JProgressBar to update from SwingWorker class
- MVC Progress Bar Threading
- How do I make my SwingWorker example work properly?
- JProgressBar without JButton and PropertyChangeListener
- Understanding Java ExecutorService