Swing app不在JPanel中绘制JComponent

时间:2014-04-07 12:21:13

标签: java swing awt

我目前正在使用Swing库和Graphics2D编写Java绘图程序。我的主GUI类扩展了JComponent,我试图将它放在JPanel中,并将Jarame中的JPanel放在屏幕上。但是,在启动程序时,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;

        }
    }

}

1 个答案:

答案 0 :(得分:2)

您已将PaintGUI添加到框架,但LayoutManager不知道尺寸,无法设置所需的尺寸。

设置首选大小(或覆盖getPreferredSize以返回所需大小)

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