标题栏中的图形渲染 [英] Graphics rendering in title bar

查看:32
本文介绍了标题栏中的图形渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图形在标题栏中不断呈现.我使用封装在 jlabel 中的缓冲图像,并使用生成的图形对象在我的代码中绘制矩形.这是jframe类构造函数的重要部分:

The graphics keep rendering in the title bar. I use a buffered Image encapsulated in a jlabel and use the resultant graphic objects to draw rectangles in my code. This is the important part of the jframe class constructor:

super();
        BufferedImage image=new BufferedImage(680,581,BufferedImage.TYPE_INT_ARGB);
        m_graphicsObject =image.getGraphics();

        JLabel label=new JLabel(new ImageIcon(image));

        // buttons, mouse events and other controls use listeners to handle actions
        // these listener are classes
        btn1 = new JButton("Go!");
        //btn1.setPreferredSize(new Dimension(100, 30));
        btn1.addActionListener(new button_go_Click()); //listener 1

        btn2 = new JButton("Clear!");
        //btn2.setPreferredSize(new Dimension(100, 30));
        btn2.addActionListener(new button_clear_Click()); //listener 2

        //always add created buttons/controls to form
        JPanel panel=new JPanel(new GridLayout(20,2));
        panel.add(btn1);
        panel.add(btn2);

        Container pane = this.getContentPane();

        pane.add(label);
        pane.add(panel, BorderLayout.EAST);
        this.setSize(680,581);
        this.setVisible(true);

推荐答案

问题是您在设置框架大小时没有考虑框架的边框(可能还有菜单栏)...

The problem is you're not taking into consideration the frame's border (and possibly the menu bar as well) when setting the size of the frame...

而不是使用 this.setSize(680,581) 这会导致图像呈现在框架边界内(并超出不可见空间),您应该简单地调用 JFrame#打包并让框架自行决定如何最好地调整其大小(基于其内容的首选大小)

Instead of using this.setSize(680,581) which is will cause the image to rendered inside the frames borders (and beyond into non-visible space), you should simple call JFrame#pack and let the frame decide how best to size it self (based on the preferred size of it's content)

左,绝对尺寸,右首选尺寸

Left, absolute sizing, right preferred sizing

public class SimpleImageLabel {

    public static void main(String[] args) {
        new SimpleImageLabel();
    }

    public SimpleImageLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JLabel imageLabel = new JLabel();

                try {
                    imageLabel.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/image"))));
                } catch (Exception e) {
                }


                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(imageLabel);
                frame.pack();  // <-- The better way
//                frame.setSize(imageLabel.getPreferredSize()); // <-- The not better way
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }


}

这篇关于标题栏中的图形渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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