gpt4 book ai didi

java - Graphics2D JPanel 显示但按钮不显示?

转载 作者:行者123 更新时间:2023-12-02 10:08:22 24 4
gpt4 key购买 nike

我一直在尝试弄清楚如何将 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 编程的初学者,期待您的回复。

最佳答案

看看Painting in AWT and SwingPerforming Custom Painting有关绘画如何工作以及应如何使用它的更多详细信息。

您问题的基本答案是,您通过不调用 super.paint 破坏了绘制过程。 ,这将依次调用 paintChildren并做一些其他非常重要的事情。

这是您应该避免覆盖 paint 的众多原因之一而是青睐paintComponent (别忘了调用super.paintComponent)

Not only these, check how he adds the components to the contentpane. I would say the main problem is that.

谢谢乔治。

您将按钮添加到 jpb ,您添加 jpbJFrame ...

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

然后你替换 contentPaneCarIcon

frame.setContentPane(new CarIcon());

您应该停下来阅读 How to Use Root Panes更好地了解内容 Pane 是什么及其工作原理

关于java - Graphics2D JPanel 显示但按钮不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55173547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com