Jprogressbar.setStringpainted(真);正在画两个字符串

时间:2014-08-21 00:00:24

标签: java swing paintcomponent custom-component jprogressbar

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

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 个答案:

答案 0 :(得分:2)

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

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

而不是自定义绘画,你可以作弊(一点点)......然后使用JLabel代替......

Progress

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

}

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

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