Java不会在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);


        }

}

它们在同一包装中。当我单击按钮时,它本应以红色绘制椭圆,但它什么也没显示。有人能帮我吗?顺便说一句抱歉英语不好

卢卡斯·罗特(Lukas Rotter)

这是因为你不叫panel.setBounds()revalidate()repaint()

  • 但是无论如何您都不应该使用空布局:使用布局管理器
  • 您应该super.paintComponent(g)paintComponent方法的开头进行调用
  • 与其在每次按下按钮后向面板添加新组件,不如只是在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;
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JScrollPane不会在JPanel上滚动

来自分类Dev

JLabel不会在JPanel中截断

来自分类Dev

virtualenv不会在Windows上激活

来自分类Dev

代码不会在断点上停止

来自分类Dev

Koken不会在DigitalOcean上加载

来自分类Dev

Steam不会在15.10上启动

来自分类Dev

SharedPreference不会在Android上保存

来自分类Dev

圆圈不会在画布上绘制

来自分类Dev

OnLongClickListener不会在ViewPager上触发

来自分类Dev

pywal不会在startx上运行

来自分类Dev

不会在Ant UNZIP上覆盖

来自分类Dev

伺服不会在FPGA上停止

来自分类Dev

不会在 IE 上执行的 CSS

来自分类Dev

BorderLayout不会在容器中对齐JPanel

来自分类Dev

Java的SWT:GC不会在画布上绘制简单的矩形,但是会在图像上绘制它

来自分类Dev

APNS-java不会在iOS应用程序上增加徽章编号

来自分类Dev

Java调试不会在带有NetBeans的Windows上展开WAR文件

来自分类Dev

Java 不会在双逗号“格式错误的行”上崩溃

来自分类Dev

肉桂不会在* Ubuntu 13.10上运行

来自分类Dev

flush()不会在流程OutputStream上刷新

来自分类Dev

:hover状态不会在iOS上结束

来自分类Dev

GADInterstitial不会在iOS上触发任何委托方法

来自分类Dev

bash不会在远程ssh命令上加载节点

来自分类Dev

UIWebView是否不会在iOS 7上“ scrollsToTop”?

来自分类Dev

Execvp不会在未知命令上返回错误

来自分类Dev

嵌套属性值不会在EDIT上返回

来自分类Dev

phpstorm不会在enter上缩进新行

来自分类Dev

JavaDoc将不会在jenkins上发布

来自分类Dev

重大位置更改不会在设备上触发