簡體   English   中英

Java不在JPanel上繪制

[英]Java Doesn't draw on 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);
        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);
    }


}

這是繪畫課

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);


        }

}

它們在同一個包裝中。 當我點擊按鈕時,它會用紅色繪制Ellipse,但它沒有顯示任何內容。 有人能幫我嗎? BTW抱歉英語不好

這是因為你沒有調用panel.setBounds()revalidate()repaint()

  • 但是你不應該使用null布局:使用布局管理器
  • 您應該在paintComponent方法的開頭調用super.paintComponent(g)
  • 在按下每個按鈕后,您可能只想在Grafik實例內切換一個布爾值,而不是在面板上添加一個新組件,這樣可以確定橢圓應該是否可見。
  • 如果希望橢圓“平滑”,可以調用g2.setRenderingHint(hintKey, hintValue)

修改后的代碼包括我的建議

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