显卡在标题栏渲染 [英] Graphics rendering in title bar

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

问题描述

图形渲染保持在标题栏中。
我用封装在一个JLabel缓冲图像,并使用所得到的图形对象绘制矩形在我的code。这是JFrame类的构造函数的重要组成部分:

 超();
        BufferedImage的图像=新的BufferedImage(680581,BufferedImage.TYPE_INT_ARGB);
        m_graphicsObject = image.getGraphics();        JLabel的标签=新的JLabel(新ImageIcon的(图片));        //按键,鼠标事件和其他控件使用侦听器处理措施
        //这些监听器是类
        BTN1 =的新的JButton(走!);
        //btn1.set$p$pferredSize(new尺寸(100,30));
        btn1.addActionListener(新button_go_Click()); //监听器1        BTN2 =的新的JButton(清!);
        //btn2.set$p$pferredSize(new尺寸(100,30));
        btn2.addActionListener(新button_clear_Click()); //监听器2        //随时添加创建按钮/控制形成
        的JPanel面板=新JPanel(新网格布局(20,2));
        panel.add(BTN1);
        panel.add(BTN2);        容器窗格= this.getContentPane();        pane.add(标签);
        pane.add(面板,BorderLayout.EAST);
        this.setSize(680581);
        this.setVisible(真);


解决方案

问题是你没有考虑到框架的边框(也可能是菜单栏以及)设置框架的大小时...

而不是使用 this.setSize(680581)这将导致渲染​​的帧边框内(外到不可见的空间)的图像,你应该简单叫的JFrame#包,让框架决定(它的内容基础上,preferred大小)如何最好地它的大小自我

左,绝对上浆,右preferred浆纱

 公共类SimpleImageLabel {    公共静态无效的主要(字串[] args){
        新SimpleImageLabel();
    }    公共SimpleImageLabel(){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                尝试{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }赶上(ClassNotFoundException的前){
                }赶上(InstantiationException前){
                }赶上(IllegalAccessException前){
                }赶上(UnsupportedLookAndFeelException前){
                }                JLabel的imageLabel =新的JLabel();                尝试{
                    imageLabel.setIcon(新的ImageIcon(ImageIO.read(新文件(/路径/要/图像))));
                }赶上(例外五){
                }
                JFrame的帧=新的JFrame(测试);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(新的BorderLayout());
                frame.add(imageLabel);
                frame.pack(); //< - 更好的方法
// frame.setSize(imageLabel.get preferredSize()); //< - 未将更好的办法
                frame.setLocationRelativeTo(NULL);
                frame.setVisible(真);
            }
        });
    }
}

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...

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天全站免登陆