简体   繁体   English

JProgressBar 看起来不正确

[英]JProgressBar doesn't look correct

Even using the following code the JProgressBar I'm using to display the progress of a download is far from the Windows 10 native look: in other programs the green part of the progress bar fills the entire height of the progress bar but in Java it doesn't seem to be the case.即使使用以下代码,我用来显示下载进度的 JProgressBar 与 Windows 10 本机外观相差甚远:在其他程序中,进度条的绿色部分填充了进度条的整个高度,但在 Java 中它没有好像不是这样。 Also when updated the progress bar feels very chunky compared to other programs.此外,与其他程序相比,更新进度条时感觉非常笨拙。

Here's how it looks normally https://i.imgur.com/xmhWrxf.png这是它的正常外观https://i.imgur.com/xmhWrxf.png

In java https://i.imgur.com/81T0hbF.png在 java https://i.imgur.com/81T0hbF.png

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(true);

the main JFrame of my app我的应用程序的主要 JFrame

    private JButton button = new JButton("Test");

    private JProgressBar progressBar = new JProgressBar();

    private JPanel panel = new JPanel();

    public MainFrame()
    {

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        progressBar.setMinimum(0);
        progressBar.setMaximum(100);
        progressBar.setValue(60);

        progressBar.setEnabled(true);

        progressBar.setPreferredSize(new Dimension(300, 25));

        this.panel.add(progressBar);
        this.panel.add(button);

        this.setSize(400, 600);
        this.setContentPane(panel);
        this.setVisible(true);
    }

This made my progressbar look much better in my opinion. 我认为这使我的进度栏看起来好多了。

public static Font defaultFont = new Font(Font.decode(null).getName(), Font.PLAIN, 12);
progressbar.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        progressbar.setBackground(new Color(230, 230, 230));
        progressbar.setForeground(new Color(120, 230, 90));
        progressbar.setUI(new BasicProgressBarUI() {
            @Override
            protected Color getSelectionBackground() {
                return new Color(50, 50, 50);
            }

            @Override
            protected Color getSelectionForeground() {
                return new Color(50, 50, 50);
            }
        }

Edit: you can optionally use progressbar.setStringPainted(true) to show the percentage 编辑:您可以选择使用progressbar.setStringPainted(true)显示百分比

You should post the source code so that we can have a look at it. 您应该发布源代码,以便我们对其进行查看。

My guess is that the size of progress bar needs to be adjusted. 我的猜测是进度栏的大小需要调整。

Use the setSize() method to reduce the size of the bar. 使用setSize()方法减小条形的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM