Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

java - Graphics rendering in title bar

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);
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

enter image description hereenter image description here

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);
            }
        });
    }


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...