我有一個進度對話框窗口,其中包含3個JComponents:JLabel的,不確定JProgressBar,JButton的,這是在不同應用程序的不同部分使用的默認對話框窗口線程。所以當我嘗試更改標籤的值時,它不會清除它下面的背景,它只是在舊的背景上繪製新的文本。包裝類不會覆蓋它只是將方法調用委託給它所包含的組件的任何方法。JLabel的油漆覆蓋舊的新文本,設置文本被稱爲後
這裏是代碼:
public void setNote(String note) {
this.note = note;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
label.setText(ProgressDialog.this.note);
}
});
}
實際的結果是類似於http://www.daniweb.com/forums/post1073367.html#post1073367 但這種解決辦法是不適合我。
有沒有人遇到這樣的問題?
謝謝。
這是類的cuted版本。但正如我所說,我不能讓它工作不正確。希望這可以幫助。
public class Tesssst {
public static void main(String [] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ProgressDialog dialog = new ProgressDialog(frame, "Title", "Message");
dialog.showDialog(true);
}
}
class ProgressDialog extends JComponent {
/**
*
*/
private JProgressBar progressBar;
private JLabel label;
private JFrame parentComponent;
private String title;
private String note;
private boolean canceled;
private boolean cancelEnabled;
private JButton btnCancel;
private JPanel contentPanel;
public ProgressDialog(JFrame parentComponent, String title, String message) {
this.parentComponent = parentComponent;
this.title = title;
progressBar = new JProgressBar();
label = new JLabel();
contentPanel =new JPanel();
canceled = false;
cancelEnabled = true;
setNote(message);
setOpaque(true);
}
public void setNote(String note) {
this.note = note;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
label.setText(ProgressDialog.this.note);
}
});
}
public String getNote() {
return note;
}
protected void initDialog() {
setBorder(new EmptyBorder(6, 6, 6, 6));
contentPanel = new JPanel();
contentPanel.setOpaque(true);
setLayout(new BorderLayout());
add(contentPanel);
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("ololo");
}
});
contentPanel.setLayout(new GridBagLayout());
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.insets = new Insets(2, 0, 0, 0);
label.setOpaque(true);
contentPanel.add(label, gbc);
} // label
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.NORTH;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(4, 0, 4, 0);
contentPanel.add(progressBar, gbc);
} // progressBar
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(4, 0, 4, 0);
contentPanel.add(btnCancel, gbc);
btnCancel.setEnabled(cancelEnabled);
} // cancel*/
} // funciton
public boolean isCanceled() {
return canceled;
}
public void showDialog() {
showDialog(false);
}
public void showDialog(boolean modal) {
JDialog dialog = new JDialog(parentComponent, true);
initDialog();
dialog.getContentPane().add(contentPanel);
dialog.setSize(400,400);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
if (modal) {
dialog.setAlwaysOnTop(true);
}
dialog.setVisible(true);
dialog.toFront();
}
public void cancel() {
canceled = true;
}
}
您可以發佈SSCCE嗎? – jzd 2011-01-05 18:25:19
對不起。我試圖以清晰的形式複製它,但我不能。我明白,沒有SSCCE就很難說什麼,但我只是想知道是否有人遇到這個問題。 – 2011-01-05 18:35:48