英文:
How to draw shape inside of JPanel that is inside JFrame
问题
我在使用Netbeans GUI添加了一个JPanel,但在其中绘制形状时遇到了问题。现在,我不知道在哪里添加绘制该JPanel内部圆形的代码,以及如何在目前为空的JPanel中插入并调用它,以等待绘制这个形状。我已经将目标JPanel设置为流布局。
Netbeans Designer创建了一个大的类,在这个类中,我有整个带有这个JPanel的框架,我希望将它保留在其中,因为我实际上无法以其他任何方式添加它,因为Designer不允许我更改主要的initComponents方法,而所有组件现在都在其中。我已经阅读了教程和先前的帖子,但没有人真正在使用Netbeans Designer时遇到过这个问题。
因此,有人可以帮助我在这个框架类中添加适当的方法,以及如何从我要绘制的JPanel中调用它。该JPanel的大小为50x50像素。
所以根据@Abra的建议,我修改了一些代码:
所以我创建了一个新的Circle类,并进行了一些调整,因为我不想创建一个新的框架,而是将其放在JPanel中。
```java
public class Circle extends JPanel {
private Color color;
public Circle(Color color) {
this.color = color;
setPreferredSize(new Dimension(30, 30));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.drawOval(0, 0, getWidth(), getHeight());
}
}
然后,我在Designer中打开了JPanel,并添加了运行它的代码,将其放在initComponents方法中,就像这样:
circlePanel.setPreferredSize(new Dimension(40, 40));
new Circle(Color.RED).showGUI();
PanelDS.add(circlePanel);
其中,circlePanel是绘制的目标,位于PanelDS内部。虽然这种方式不起作用,但Netbeans在代码中没有显示错误。另外,如何将颜色传递给Circle类呢?
<details>
<summary>英文:</summary>
I have issue with drawing shapes inside of JPanel that I already added using Netbeans GUI. Now, I have no idea where to add code for drawing a circle inside of that JPanel and how to insert and call it in the JPanel that is sitting empty now, waiting for this shape to be drawn. I already set up destination JPanel to be Flow layout.
Netbeans Designer created a big class in which I have entire frame with this JPanel, and I want to keep it inside of it as I can't really add it any other way because Designer doesn't let me change main initComponents method in which all components are sitting now. I have been reading tutorials and previous posts but noone really encountered this using Netbeans Designer.
SO can someone just help me with adding proper method in this frame class and how to call it from JPanel I want to draw in. JPanel is 50x50 pixels.
So as per @Abra, I changed some code:
so I made a new Circle Class, adjusted it a bit as I don't want to create a new frame but put this in JPanel.
public class Circle extends JPanel {
Color color;
public void circle(Color color) {
this.color = color;
setPreferredSize(new Dimension (30,30));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(0, 0, r, r);
g.setColor(color);
}
private void showGUI() {
JPanel panel = new JPanel();
panel.add(this, FlowLayout.CENTER);
panel.setVisible(true);
}
}
Then I opened JPanel in Designer, and added code to run it, in initComponents method like this:
circlePanel.setPreferredSize(new java.awt.Dimension(40, 40));
new Circle().showGUI();
PanelDS.add(circlePanel);
circlePanel is destination for this drawing and is inside PanelDS itself. It doesn't work this way tho, but Netbeans shows no errors in code. Additionally, how can I forward color to circle class.
</details>
# 答案1
**得分**: 1
```java
为了在 `JPanel` 上绘图,您需要重写 `JPanel` 的 `paintComponent()` 方法。为了重写该方法,您需要创建一个继承自 `JPanel` 的类。我认为目前不存在一个能为您生成所需代码的GUI设计工具。因此,您需要编写一个继承自 `JPanel` 的类的代码。
这是一个最简示例。它显示一个蓝色的圆。
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Drawing2 extends JPanel {
private JFrame frame;
public Drawing2() {
setPreferredSize(new Dimension(100, 100));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(25, 25, 50, 50);
}
private void showGui() {
frame = new JFrame("Drawing");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
new Drawing2().showGui();
}
}
当您运行上述代码时,您应该看到以下内容。
<details>
<summary>英文:</summary>
In order to draw on a `JPanel` you need to override the `paintComponent()` method of `JPanel`. In order to override the method, you need to create a class that extends `JPanel`. I don't think that there exists a GUI designer that can generate the required code for you. So you have to write the code of the class that extends `JPanel`.
Here is a minimal example. It displays a blue circle.
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Drawing2 extends JPanel {
private JFrame frame;
public Drawing2() {
setPreferredSize(new Dimension(100, 100));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(25, 25, 50, 50);
}
private void showGui() {
frame = new JFrame("Drawing");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
new Drawing2().showGui();
}
}
Here's what you should see when you run the above code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论