Jprogressbar.setStringpainted(true); is painting two strings

て烟熏妆下的殇ゞ 提交于 2019-12-02 11:10:59

问题


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

}

回答1:


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...

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...

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- 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)



来源:https://stackoverflow.com/questions/25416336/jprogressbar-setstringpaintedtrue-is-painting-two-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!