Graphics2D JPanel显示但按钮不?

问题描述 投票:0回答:1

我一直试图弄清楚如何将Graphics2D对象添加到JFrame以及同一帧/面板对象的按钮。我希望按钮能够以某种方式编辑图像,但是我在使按钮和图像出现在同一个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);      
        }
}

以下是我看到的结果:Car Image

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

java swing user-interface awt graphics2d
1个回答
1
投票

看看Painting in AWT and SwingPerforming Custom Painting,了解有关绘画如何运作以及如何使用它的更多细节。

你问题的基本答案是,你没有调用super.paint打破了绘制过程,paintChildren将调用paint并做一些其他非常重要的事情。

这是为什么你应该避免重写paintComponent而不是赞成super.paintComponent(并且不要忘记打电话给jpb)的众多原因之一

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

谢谢乔治。

你添加按钮到jpb,你添加JFramejpb.add(zoomInButton); jpb.add(zoomOutButton); frame.add(jpb, BorderLayout.SOUTH); ...

contentPane

然后你用CarIcon替换frame.setContentPane(new CarIcon());

How to Use Root Panes

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

© www.soinside.com 2019 - 2024. All rights reserved.