Swing 调整大小缓慢/跳跃 [英] Swing resize slow/jumpy

查看:53
本文介绍了Swing 调整大小缓慢/跳跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用简单的渐变扩展了 JComponent 并调整了paintComponent() 方法,制作了自己的BottomBar.

I made my own BottomBar with a simple gradient extending JComponent and adjusting the paintComponent() method.

然后我将它添加到使用 BorderLayout 的 JFrame 的 SOUTH.

Then I add it to the SOUTH of my JFrame which uses BorderLayout.

一开始一切看起来都是正确的.当我调整框架的大小时,BottomBar 会重新绘制并正确设置到新位置.想法是,它发生的时间晚了几毫秒,以便人们可以看到 JFrame 的背景一秒钟.

Everything looks correct at the beginning. When I resize the frame the BottomBar gets repainted and set to the new position correctly. The think is, it happens a few milliseconds to late, so that one can see the JFrame 's background for a second.

有趣的是,当我将执行环境设置为 Java-SE 1.6 时,它可以工作...(而不是 1.7)另外,我在 mac 上运行它,如果这有区别的话.

The funny thing is, that when I set the execution environment to Java-SE 1.6 it works... (instead of 1.7) Also, Im running it on a mac, if that makes a difference.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;


public class Main {

    public static void main(String args[]){
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Resize Example");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JButton(), BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

<小时>

代码 - 底部栏示例

主要内容:

public class Main {
    public static void main(String args[]){
        Frame window = new Frame();
        window.setSize(500, 400);
        window.setVisible(true);
    }
}

框架:

import java.awt.BorderLayout;

import javax.swing.JFrame;


public class Frame extends JFrame{
    private static final long serialVersionUID = 1L;

    public Frame() {
        setLayout( new BorderLayout() );
        getContentPane().add( BorderLayout.SOUTH, new BottomBar() );
    }
}

底部栏

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JComponent;

public class BottomBar extends JComponent {
    private static final long serialVersionUID = 1L;

    public BottomBar() {
        setSize(200, 30);
        setPreferredSize( new Dimension(200, 30) );
    }

    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        GradientPaint gradient = new GradientPaint(0, 0, new Color(185, 185, 185), 0, getHeight() , new Color(151, 151, 151) );
        g2.setPaint(gradient);
        g2.fillRect(0, 0, getWidth(), getHeight());

        g2.setColor( new Color(64, 64, 64) );
        g2.drawLine(0, 0, getWidth(), 0);
        g2.setColor( new Color(215, 215, 215) );
        g2.drawLine(0, 1, getWidth(), 1);
    }
}

推荐答案

我无法重现您在 1.6 中描述的效果;您可以在 1.7 上尝试下面的 sscce.请注意,对于您的示例的一些建议:

I am unable to reproduce the effect you describe on 1.6; you might try the sscce below on 1.7. Note, several suggestions for your example:

  • 避免 setXxxxSize(),如此处所述.如果您只想在 SOUTH 中设置一个 30 像素高的条,请覆盖 getPreferredSize(),如下所示.如果您以后决定添加组件,则需要添加布局管理器.

  • Avoid setXxxxSize(), as discussed here. If you just want a 30 pixel high bar in SOUTH, override getPreferredSize() as shown below. If you later decide to add components, you'll need to add a layout manager.

@Override
public Dimension getPreferredSize() {
    return new Dimension(0, 30);
}

  • 使用 pack()Window 采用封闭组件的首选大小.我在 CENTER 中添加了任意大小的 JPanel;调整框架大小以查看条在 SOUTH 中如何水平增长.

  • Use pack() to let the Window adopt the preferred sizes of the enclosed components. I've added an arbitrary size JPanel to the CENTER; resize the frame to see how the bar grows horizontally in SOUTH.

    Swing GUI 对象应该事件调度线程.

    Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /** @see https://stackoverflow.com/a/13610367/230513 */
    public class Main {
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame frame = new JFrame("BottomBar");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new JPanel() {
    
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(320, 240);
                        }
                    }, BorderLayout.CENTER);
                    frame.add(new BottomBar(), BorderLayout.SOUTH);
                    frame.pack();
                    frame.setLocationByPlatform(true);
                    frame.setVisible(true);
                }
            });
        }
    
        private static class BottomBar extends JComponent {
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(0, 30);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
                GradientPaint gradient = new GradientPaint(
                    0, 0, new Color(185, 185, 185),
                    0, getHeight(), new Color(151, 151, 151));
                g2.setPaint(gradient);
                g2.fillRect(0, 0, getWidth(), getHeight());
                g2.setColor(new Color(64, 64, 64));
                g2.drawLine(0, 0, getWidth(), 0);
                g2.setColor(new Color(215, 215, 215));
                g2.drawLine(0, 1, getWidth(), 1);
            }
        }
    }
    

    这篇关于Swing 调整大小缓慢/跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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