我目前正在编写使用Swing库和Graphics2D进行绘画的Java绘画程序。我的主要GUI类扩展了JComponent,我试图将其放在JPanel中,并将JPanel放在JFrame中,以便在屏幕上显示它。但是,在启动程序时,JComponent似乎只是一条黑线(边框已设置为组件周围的黑线)。我不明白为什么会这样,并且已经调试了好几个小时。如果有人可以在该程序中找到错误,我将非常高兴。提前致谢。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PaintGUI extends JComponent {

    private static final long serialVersionUID = 1L;

    JButton red, green, blue, clear;
    Image img;
    Graphics2D gfx;
    JFrame drawFrame;
    JPanel btnPan, drawPan;
    MyListener ml;
    Action act;
    int x, y, prevX, prevY;

    public PaintGUI(){
        //Initialisering av panel, frame og content
        drawFrame = new JFrame("IFIPaint");
        drawFrame.setLayout(new BorderLayout());
        btnPan = new JPanel();
        drawPan = new JPanel();
        btnPan.setLayout(new FlowLayout());
        drawPan.setLayout(new BorderLayout());
        this.setEnabled(true);

        //Setter størrelser
        btnPan.setPreferredSize(new Dimension(30, 60));
        btnPan.setMinimumSize(new Dimension(30, 60));
        btnPan.setMaximumSize(new Dimension(30, 60));

        //Ordner knappene
        red = new JButton("Rød");
        green = new JButton("Grønn");
        blue = new JButton("Blå");
        clear = new JButton("Slett alt");

        //Putter knappene på panelet
        btnPan.add(red);
        btnPan.add(green);
        btnPan.add(blue);
        btnPan.add(clear);

        //Legger på action listeners
        act = new Action();
        red.addActionListener(act);
        green.addActionListener(act);
        blue.addActionListener(act);
        clear.addActionListener(act);

        //Fullfører vindu og setter synlighet
        drawFrame.setSize(500, 500);
        drawPan.setBounds(0, 0, 400, 400);
        this.setBounds(0, 0, 400, 400);
        drawPan.add(this);
        this.setBackground(Color.RED);
        drawFrame.add(drawPan, BorderLayout.NORTH);
        drawFrame.add(btnPan, BorderLayout.SOUTH);

        this.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        this.setVisible(true);
        drawPan.setVisible(true);
        btnPan.setVisible(true);
        drawFrame.setVisible(true);

        this.paintComponent(gfx);

        drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        draw();
    }

    public void draw() {
        ml = new MyListener();
        this.addMouseListener(ml);
        this.addMouseMotionListener(ml);
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        if(img == null){
            img = createImage(this.getWidth(),this.getHeight());
            gfx = (Graphics2D) img.getGraphics();
            gfx.setPaint(Color.RED);
            gfx.fillRect(0, 0, this.getSize().width, this.getSize().height);
            gfx.setPaint(Color.RED);
            gfx.dispose();
        }

        gfx.drawImage(img, 0, 0, this);
    }


    class Action implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == red){
                gfx.setPaint(Color.RED);
                repaint();
            } else if (e.getSource() == green){
                gfx.setPaint(Color.GREEN);
                repaint();
            } else if (e.getSource() == blue) {
                gfx.setPaint(Color.BLUE);
                repaint();
            } else if (e.getSource() == clear) {
                gfx.clearRect(0, 0, drawFrame.getWidth(), drawFrame.getHeight());
                repaint();
            }
        }

    }

    class MyListener extends MouseAdapter {

        public void mousePressed(MouseEvent e) {
            prevX = e.getX();
            prevY = e.getY();
            System.out.println("o ye");
        }

        public void mouseDragged(MouseEvent e) {
            x = e.getX();
            y = e.getY();
            gfx.drawLine(prevX, prevY, x, y);
            repaint();
            prevX = x;
            prevY = y;

        }
    }

}

最佳答案

您已将PaintGUI添加到框架中,但是LayoutManager不知道其大小,因此无法设置所需的大小。

设置首选大小(或重写getPreferredSize以返回所需大小)

或向PaintGUI实例添加一些具有首选大小的组件(例如按钮)

关于java - Swing应用未在JPanel中绘制JComponent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22912282/

10-11 00:35