简体   繁体   English

Java不在JPanel上绘制

[英]Java Doesn't draw on JPanel

This is my main class: 这是我的主要课程:

public class Sad {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sad window = new Sad();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Sad() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 512, 399);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout(0, 0));

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, "name_12361565901507");
        panel.setLayout(null);

        JButton btnNes = new JButton("Nes");
        btnNes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                Grafik grafik = new Grafik(20, 20, 100);
                panel.add(grafik);
            }
        });
        btnNes.setBounds(90, 146, 89, 23);
        panel.add(btnNes);
    }


}

And this is drawing class 这是绘画课

public class Grafik extends JPanel{

    private int x;
    private int y;
    private int r;

    public Grafik(int x, int y, int r){
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 =(Graphics2D) g;

        Ellipse2D circ = new Ellipse2D.Float(x, y, r, r);
        g2.setColor(Color.RED);
        g2.draw(circ);


        }

}

They are in same package. 它们在同一个包装中。 And when i click button its suposed to draw Ellipse in red color, but it doesn't show anything. 当我点击按钮时,它会用红色绘制Ellipse,但它没有显示任何内容。 Can someone help me? 有人能帮我吗? BTW Sorry for bad english BTW抱歉英语不好

It's because you don't call panel.setBounds() , revalidate() and repaint() . 这是因为你没有调用panel.setBounds()revalidate()repaint()

  • But you shouldn't use a null layout anyway: Use layout managers . 但是你不应该使用null布局:使用布局管理器
  • You should call super.paintComponent(g) at the beginning of the paintComponent method. 您应该在paintComponent方法的开头调用super.paintComponent(g)
  • Rather than adding a new component to the panel after every button press you might want to just toggle a boolean value inside the Grafik instance which determines wheter the ellipse should be visible or not. 在按下每个按钮后,您可能只想在Grafik实例内切换一个布尔值,而不是在面板上添加一个新组件,这样可以确定椭圆应该是否可见。
  • If you want the ellipse to be "smooth", you can call g2.setRenderingHint(hintKey, hintValue) . 如果希望椭圆“平滑”,可以调用g2.setRenderingHint(hintKey, hintValue)

Modified code including my suggestions: 修改后的代码包括我的建议

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Sad {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sad window = new Sad();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Sad() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 512, 399);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Grafik grafik = new Grafik(20, 20, 100);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(grafik);

        JButton btnNes = new JButton("Nes");
        btnNes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                grafik.setEllipseVisible(true);
                panel.repaint();
            }
        });

        JPanel btnPanel = new JPanel();
        btnPanel.add(btnNes);
        panel.add(btnPanel, BorderLayout.SOUTH);

        frame.setContentPane(panel);
    }

}

class Grafik extends JPanel {

    private int x;
    private int y;
    private int r;
    private boolean ellipseVisible;

    public Grafik(int x, int y, int r) {
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isEllipseVisible()) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            Ellipse2D circ = new Ellipse2D.Float(x, y, r, r);
            g2.setColor(Color.RED);
            g2.draw(circ);
        }
    }

    public boolean isEllipseVisible() {
        return ellipseVisible;
    }

    public void setEllipseVisible(boolean ellipseVisible) {
        this.ellipseVisible = ellipseVisible;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM