Jprogressbar.setStringpainted(真);画两个字符串 [英] Jprogressbar.setStringpainted(true); is painting two strings

查看:148
本文介绍了Jprogressbar.setStringpainted(真);画两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码创建的问题是,当我单击按钮时,两个字符串被绘制为一个水平和一个垂直,但只需要绘制水平字符串,所以请告诉我该怎么做???

This code is creating a problem that is when I click the button two strings are being painted one horizontal and one vertical, but need only horizontal string to be painted, so please tell what should I do???

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;





public class R implements ActionListener {


    static int y;
    CustomProgressBar b = new CustomProgressBar();



    public static void main(String arg[]) throws Exception {
        new R();
    }



    public R() throws Exception {
        JFrame f = new JFrame();
        JButton btn = new JButton("Click");
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setUndecorated(true);
        f.setLayout(new FlowLayout());
        btn.addActionListener(this);
        f.add(b);
        f.add(btn);
        f.setVisible(true);
    }



    class CustomProgressBar extends JProgressBar{


        private static final long serialVersionUID = 1L;
        private boolean isStringToBePainted = false;
        public CustomProgressBar() {
            super(JProgressBar.VERTICAL,0,100);
        }



        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(isStringToBePainted ) {
                Dimension size = CustomProgressBar.this.getSize();
                if( CustomProgressBar.this.getPercentComplete()<0.9  )
                    R.y = (int)( size.height - size.height * CustomProgressBar.this.getPercentComplete() );     
                String text = getString();
                g.setColor(Color.BLACK );
                g.drawString(text, 0, R.y);
            }
        }
        @Override
        public void setStringPainted(boolean b) {
            super.setStringPainted(b);
            isStringToBePainted=b;
        }
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        b.setStringPainted(true);
    }

}


推荐答案

问题是,属性 setStringPainted 已经由组件定义,并且具有预定义的功能......您知道它似乎想要更改...

The problem is, the property setStringPainted is already defined within by the component and has a predefined functionality...which you know seem to want to change...

你要么需要定义一个你可以控制的新属性,要么改变UI委托的工作方式,这是你需要做的很多工作为每个外观和感觉提供一个你可能想要支持...

You will either, need to define a new property of your own which you can control OR change the way the UI delegate works, which is a lot of work as you will need to provide one for each look and feel you might want to support...

你可以作弊(一点点)而不是做自定义绘画...并使用一个 JLabel 相反,例如......

Instead of doing custom painting, you could cheat (a little)... and use a JLabel instead, for example...

class CustomProgressBar extends JProgressBar {

    private static final long serialVersionUID = 1L;
    private boolean isStringToBePainted = false;

    private JLabel progress;

    public CustomProgressBar() {
        super(JProgressBar.VERTICAL, 0, 100);

        progress = new JLabel("0%");
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weighty = 1;
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.SOUTH;
        add(progress, gbc);
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        Dimension labelSize = progress.getPreferredSize();
        Insets insets = getInsets();
        if (size.width < labelSize.width) {
            size.width = insets.left + insets.right + labelSize.width;
        }
        return size;
    }

}

你仍然需要提供你自己的属性来打开或关闭它,但这是一个想法...

You're still going to need to provide your own property to turn it on or off, but it's an idea...

(ps-我快速了解了尝试实现我自己的UI委托在我复制并粘贴了我的第三种方法之后,我放弃了,因为它只是更多的工作然后奖励会提供)

(ps- I had a quick look at trying to implement my own UI delegate, after I copy and pasted my third method, I gave up, as it would just be more work then the reward would provide)

这篇关于Jprogressbar.setStringpainted(真);画两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆