Java Graphics2D JPanel显示但按钮不显示?

Java Graphics2D JPanel显示但按钮不显示?,java,swing,user-interface,awt,graphics2d,Java,Swing,User Interface,Awt,Graphics2d,我一直在试图找出如何将Graphics2D对象添加到JFrame,以及如何将按钮添加到同一个frame/panel对象。我希望按钮能够以某种方式编辑图像,但是我很难让按钮和图像出现在同一个JFrame上。下面是我看到的代码和结果窗口,我做错了什么?谢谢你抽出时间 package carEditor; import java.awt.*; import java.awt.geom.*; import javax.swing.*; import java.awt.Graphics; import

我一直在试图找出如何将Graphics2D对象添加到JFrame,以及如何将按钮添加到同一个frame/panel对象。我希望按钮能够以某种方式编辑图像,但是我很难让按钮和图像出现在同一个JFrame上。下面是我看到的代码和结果窗口,我做错了什么?谢谢你抽出时间

package carEditor;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CarIcon extends JPanel{

        public void paint(Graphics g){

            int x = 10;
            int y = 50;
            int width = 100;

            Graphics2D g2 = (Graphics2D) g;
            Rectangle2D.Double body
            = new Rectangle2D.Double(x, y + width / 6,
            width - 1, width / 6);
            Ellipse2D.Double frontTire
            = new Ellipse2D.Double(x + width / 6, y + width / 3,
            width / 6, width / 6);

            Ellipse2D.Double rearTire
            = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
            width / 6, width / 6);

            // The bottom of the front windshield
            Point2D.Double r1
            = new Point2D.Double(x + width / 6, y + width / 6);
            // The front of the roof
            Point2D.Double r2
            = new Point2D.Double(x + width / 3, y);
            // The rear of the roof
            Point2D.Double r3
            = new Point2D.Double(x + width * 2 / 3, y);
            // The bottom of the rear windshield
            Point2D.Double r4
            = new Point2D.Double(x + width * 5 / 6, y + width / 6);

            Line2D.Double frontWindshield
            = new Line2D.Double(r1, r2);
            Line2D.Double roofTop
            = new Line2D.Double(r2, r3);
            Line2D.Double rearWindshield
            = new Line2D.Double(r3, r4);

            g2.fill(frontTire);
            g2.fill(rearTire);
            g2.setColor(Color.red);
            g2.fill(body);
            g2.draw(frontWindshield);
            g2.draw(roofTop);
            g2.draw(rearWindshield);
        }   
        public static void main(String[] args){
            JFrame frame= new JFrame(); 

            JPanel jpb = new JPanel();

            JButton zoomOutButton = new JButton("Zoom Out");
            JButton zoomInButton = new JButton("Zoom In");

            frame.setLayout(new FlowLayout());

            //zoomOutButton.addActionListener(event ->
            //      textField.setText("Goodbye"));

            //zoomInButton.addActionListener(event ->
            //textField.setText("Hello"));

            jpb.add(zoomInButton);
            jpb.add(zoomOutButton);
            frame.add(jpb, BorderLayout.SOUTH);
            frame.setContentPane(new CarIcon());

            //frame.pack();

            frame.setSize(600, 400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);      
        }
}
以下是我看到的结果:

此框架上应该有按钮,但没有。为什么呢?感谢您的帮助,我是Java GUI编程的初学者,我期待着您的回复。

请看一看,了解有关绘画如何工作以及如何使用绘画的更多详细信息

您的问题的基本答案是,您没有调用
super.paint
,这就破坏了绘制过程,而super.paint将反过来调用
paintChildren
,并执行一系列其他非常重要的操作

这就是为什么您应该避免重写
paint
,而应该选择
paintComponent
(并且不要忘记调用
super.paintComponent
)的众多原因之一


不仅如此,还要检查他如何将组件添加到contentpane。我想说的主要问题是

谢谢你,乔治

将按钮添加到
jpb
,将
jpb
添加到
JFrame

jpb.add(zoomInButton);
jpb.add(zoomOutButton);
frame.add(jpb, BorderLayout.SOUTH);
然后将
contentPane
替换为
CarIcon

frame.setContentPane(new CarIcon());

您应该停下来仔细阅读,以便更好地了解内容窗格是什么以及它是如何工作的

不仅仅是这些,还要检查他是如何将组件添加到内容窗格的。“我想说的主要问题是,”乔治。这是一连串的连锁反应