【问题标题】:Java Panel BufferedImage Not WorkingJava 面板 BufferedImage 不工作
【发布时间】:2013-03-17 15:29:16
【问题描述】:

AnaPencere.java

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class AnaPencere {
    JFrame anaPencere;
    BufferedImage bImageLEFT;
    BufferedImage bImageRIGHT;
    public static void main(String[] args){
        AnaPencere apencere = new AnaPencere();
    }

    public AnaPencere() {
            anaPencere = new JFrame("Main Window");
            anaPencere.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            anaPencere.setSize(613, 253);
            anaPencere.setLocationRelativeTo(null);
            anaPencere.setVisible(true);
            anaPencere.add(new left());
            anaPencere.add(new right());

    }

    private class left extends JPanel{
        private left() {
            this.setBounds(0, 0, 313, 253);
            this.setFocusable(true);
            bImageLEFT = new BufferedImage(313, 253, BufferedImage.TYPE_INT_ARGB);
            paintComponent(bImageLEFT.createGraphics());
        }

        public void paintComponent(Graphics g){
            Graphics2D g2 = (Graphics2D)g;
            g2.drawString(">  LEFT  <", Font.BOLD, 13);
        }
    }

    private class right extends JPanel{
        private right() {
            this.setBounds(313, 0, 300, 253);
            this.setFocusable(true);
            bImageRIGHT = new BufferedImage(313, 253, BufferedImage.TYPE_INT_ARGB);
            paintComponent(bImageRIGHT.createGraphics());
        }

        public void paintComponent(Graphics g){
            Graphics2D g2 = (Graphics2D)g;
            g2.drawString(">  RIGHT  <", Font.BOLD, 13);
        }
    }
}

(我设置jpanels坐标左右类) 当我运行我的代码时,我只显示 > LEFT 文本,我看不到 > RIGHT 文本。 我怎样才能解决这个问题?

对不起我的英语不好..

【问题讨论】:

    标签: java swing graphics panel bufferedimage


    【解决方案1】:

    在这里,我对您的代码进行了更改。并将cmets放在有关的地方。看看吧:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    
    public class AnaPencere {
        JFrame anaPencere;
        BufferedImage bImageLEFT;
        BufferedImage bImageRIGHT;
        public static void main(String[] args){
            AnaPencere apencere = new AnaPencere();
        }
    
        public AnaPencere() {
                anaPencere = new JFrame("Main Window");
                anaPencere.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                anaPencere.setSize(613, 253);
                anaPencere.setLocationRelativeTo(null);
                JPanel pLeft = new left();
                JPanel pRight = new right();
                anaPencere.getContentPane().add(pLeft,BorderLayout.WEST);
                anaPencere.getContentPane().add(pRight,BorderLayout.EAST);
                anaPencere.setVisible(true);//Set visible true after all components are added
        }
    
        private class left extends JPanel{
            private left() {
                //this.setBounds(0, 0, 313, 253);//Don't set Bounds. Use Layouts provided by Swing API
                setPreferredSize(new Dimension(150,200));//Set the preferredSize
                this.setFocusable(true);
                bImageLEFT = new BufferedImage(313, 253, BufferedImage.TYPE_INT_ARGB);
                //paintComponent(bImageLEFT.createGraphics());// No need to call it explicitly. Swing will do it for itself.
            }
    
       public void paintComponent(Graphics g){
            super.paintComponent(g);//call the paintComponent method of JPanel class
            Graphics2D g2 = (Graphics2D)g;
            g2.setFont(new Font(g.getFont().getName(),Font.BOLD,13));//set Font in this way
            g2.drawString(">  LEFT  <", 10, 20);//10 is x coordinate , 20 is y cordinate
        }
        }
    
        private class right extends JPanel{
            private right() {
                //this.setBounds(313, 0, 300, 253);/Don't set Bounds. Use Layouts provided by Swing API
                setPreferredSize(new Dimension(150,200));//Set the preferredSize
                this.setFocusable(true);
                bImageRIGHT = new BufferedImage(313, 253, BufferedImage.TYPE_INT_ARGB);
               // paintComponent(bImageRIGHT.createGraphics());//No need to call it explicitly. It is called by swing components itself.
            }
    
            public void paintComponent(Graphics g){
                 super.paintComponent(g);//call the paintComponent method of JPanel class
                Graphics2D g2 = (Graphics2D)g;
                g2.setFont(new Font(g.getFont().getName(),Font.BOLD,13));//set Font in this way
                g2.drawString(">  RIGHT  <", 10, 20);//10 is x coordinate , 20 is y cordinate
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复,但我只看到了再次无法使用> LEFT
    • 我使用默认的 gui,我只想要一帧,但使用 bufferedimage 左右创建两个 jpanel 类我使用两个paintComponent 并绘制左右字符串
    • 恕我直言,ONE 不应该显式调用 paintComponent(...) 方法。让 Swing 担心这方面,尽管在这种情况下您可以简单地设置对象的状态,并调用 repaint()。在此编辑之前的答案更合适。这个link 可能会给你更多的想法,关于我对这个更新的答案的担忧。
    • -1,用于显式调用 paintComponent(bImageRIGHT.createGraphics()); :(
    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多