6

我有一个进度对话框窗口,其中包含 3 个 JComponent:JLabel、JProgressBar、JButton,它们用作来自不同线程的应用程序不同部分的默认对话框窗口。因此,当我尝试更改标签的值时,它不会清除它下面的背景,它只是在旧文本上绘制新文本。包装类不会覆盖任何方法,它只是将方法调用委托给它包含的组件。

这是代码:

  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 但该解决方案不适合我。

有没有人遇到过这样的问题?

谢谢。

这是班级的可爱版本。但正如我所说,我无法让它正常工作。希望这可以帮助。

    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;
    }

}
4

6 回答 6

4

试试setOpaque(true)标签,这应该会导致它清除其背景。

于 2011-01-05T18:34:21.040 回答
2

repaint()设置文本后,在适当的组件上尝试 a 。

于 2011-01-05T18:24:45.180 回答
2

尝试扩展 JPanel 而不是 JComponent。

JComponent 没有任何代码来绘制其背景。因此,使组件不透明并不能消除旧文本。保持标签不透明,并使用不透明的 JPanel 作为组件的容器。

也许您在某处使用具有 alpha 值的背景。如果是这样,您可以查看背景透明度以了解您可能遇到的一些问题。

否则,只有您可以将您的工作代码与非工作代码进行比较,看看有什么不同。

于 2011-01-05T19:40:21.677 回答
2

问题在于不透明度。在我们的默认外观中,“Panel.background”属性设置为new Color(135, 15, 19, 0). 所以默认情况下所有面板都是不透明的。该标签称为repaint()方法,并且由于所有父面板都是非透明的,因此未执行后台刷新。

谢谢大家的回复。

于 2011-01-06T14:08:17.637 回答
1

camickr 的答案是正确的方法;但是,您的测试明显出错的一件事是您的摆动线程使用。

我怀疑它也可能与它是一个对话有关。对话框通常会在调用 showDialog() 时阻塞 SWMET;因此,通常在 showDialog() 中创建第二个事件线程。在这里,您不是从 SWMET 调用 showDialog(),而是从 main 调用它——也许您会看到一些奇怪的效果。

public static void main(String args[]) throws Exception {
 SwingUtils.invokeLater(new Runnable() {
 public void run() {
   ProgressDialog dialog = new ProgressDialog(frame, "Title", "Message");
   dialog.showDialog(true);
 }
 });
}
于 2011-01-05T20:02:15.437 回答
-2

你需要把super.paintComponent(g);

于 2015-12-11T19:51:34.267 回答