Java 面板 BufferedImage 不工作

标签 java swing graphics panel bufferedimage

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 < 文本。 我该如何解决这个问题?

抱歉我的英语不好..

最佳答案

这里我对您的代码进行了更改。并将评论放到关注的地方。看看它:

enter image description here

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

关于Java 面板 BufferedImage 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15660735/

相关文章:

java - Hibernate:运行查询以获取日期范围内的结果时没有结果

java - 如何创建 Ellipse2D 数组?

java - 在执行 mmap 时,C 或 Java 会有明显的性能差异吗?

java - Thread.currentThread() 在多核/多处理器系统上的语义?

Java 将多个JPanel 对象添加到JFrame 中

java - JButton Action 监听器

java - KeyListener 不能与 dispose() 一起使用;

c - glCreateShader 正在崩溃

c++ - SDL 2.0 - 渲染 : PNG file doesn't render

Java:无需身份验证即可建立SSL连接