加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在调用set text之后,JLabel在旧文本上绘制新文本

发布时间:2020-12-14 05:46:08 所属栏目:Java 来源:网络整理
导读:我有一个进度对话框窗口,其中包含3个JComponents:JLabel,JProgressBar,JButton,它在不同线程的应用程序的不同部分用作默认对话框窗口.因此,当我尝试更改标签的值时,它不会清除其下的背景,它只是在旧文本上绘制新文本.包装器类不会覆盖它只是将方法调用委托
我有一个进度对话框窗口,其中包含3个JComponents: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相似
但那个解决方案不适合我.

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

谢谢.

这是该类的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));
        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);
            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,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);
            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;
    }

}

解决方法

在标签上尝试 setOpaque(true),这应该使它清除其背景.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读