编程语言
首页 > 编程语言> > java-Jprogressbar.setStringpainted(true);在画两个弦

java-Jprogressbar.setStringpainted(true);在画两个弦

作者:互联网

这段代码造成了一个问题,当我单击按钮时,将两个字符串分别绘制为一个水平和一个垂直,但是只需要绘制水平字符串,所以请告诉我该怎么做?

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已由组件定义,并具有预定义的功能…您似乎想更改…

您将要么需要定义自己的新属性即可控制或更改UI委托的工作方式,这是很多工作,因为您需要为可能要支持的每种外观提供一个属性. ..

您可以作弊(一点),而不是进行自定义绘画,例如,使用JLabel …

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委托,在我复制并粘贴第三个方法之后,我放弃了,因为这将提供更多的工作,而不是提供奖励)

标签:jprogressbar,custom-component,paintcomponent,java,swing
来源: https://codeday.me/bug/20191013/1907251.html