Java Netbeans 覆盖 JPanel 中的paint() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18416342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Netbeans override paint() method in a JPanel
提问by topher
Hi I'm a newbie programmer
I want to develop a Java program's GUI using Netbeans IDE
嗨,我是一名新手程序员,
我想使用 Netbeans IDE 开发 Java 程序的 GUI
Using Netbeans GUI Builder,
First, I create a new JFrame Form
Then, I add a JPanel from the toolbar/palette
使用 Netbeans GUI Builder,
首先,我创建了一个新的 JFrame 表单,
然后,我从工具栏/调色板添加了一个 JPanel
Question is,
How can I override the paint()
function of the newly created JPanel
?
问题是,
如何覆盖paint()
新创建的功能JPanel
?
I want to draw a background and some spheres inside the JPanel
,
I tried using getGraphics()
function to paint and draw, it does the job, but it won't draw anymore when I call repaint()
我想画一个背景和里面的一些领域JPanel
,
我尝试使用getGraphics()
功能油漆和借鉴,它的工作,但是当我把它不会再画repaint()
Should I create a new class implementing JPanel
or JComponent
, with my custom paint()
function, instead ?
(If it so, how can I do it with Netbeans GUI Builder?)
我应该使用我的自定义函数创建一个实现JPanel
或的新类吗?
(如果是这样,我如何使用 Netbeans GUI Builder 来做到这一点?)JComponent
paint()
Similar Question :
类似问题:
(but it doesn't use Netbeans GUI Builder)
(但它不使用 Netbeans GUI Builder)
采纳答案by a_horse_with_no_name
The usual way to do this is to create your own JPanel
subclass (e.g. MyJPanel
) and implement the paint()
method there.
执行此操作的常用方法是创建您自己的JPanel
子类(例如MyJPanel
)并在paint()
那里实现该方法。
After you implemented that class, switch to your form, select the panel and then use "Custom creation code" in the the "Code" tab of the panel's properties to create an instance of MyJPanel
instead of JPanel
实现该类后,切换到表单,选择面板,然后使用面板属性的“代码”选项卡中的“自定义创建代码”创建一个实例,MyJPanel
而不是JPanel
This has the slight disadvantage that you will need to cast the instance variable to MyJPanel
each time you want to access methods that are defined in MyJPanel
but not in JPanel
. If you never need to do that, this is the quickest method.
这有一个轻微的缺点,即MyJPanel
每次要访问在 中定义MyJPanel
但不在JPanel
. 如果您永远不需要这样做,这是最快的方法。
If you want to access additional methods in your panel class (without casting the instance variable each time), it's easier to remove the existing JPanel and add a "Bean" using the new class.
如果您想访问面板类中的其他方法(每次不强制转换实例变量),删除现有的 JPanel 并使用新类添加“Bean”会更容易。
This is done by clicking on the "Choose Bean" button in the palette:
这是通过单击调色板中的“选择 Bean”按钮来完成的:
Once you click OK you can place the panel on your form and NetBeans will create an instance variable of the type MyJPanel
(instead of JPane) and you can access all methods defined in that class. Note that the class must be compiled before you can add it that way!
单击“确定”后,您可以将面板放在表单上,NetBeans 将创建该类型MyJPanel
(而不是 JPane)的实例变量,您可以访问该类中定义的所有方法。请注意,必须先编译该类,然后才能以这种方式添加它!
回答by Vinz243
You shall not implement paint, if I understand well, because it invoke paintBorder, paintChildren and paintComponent. Use paint if you you want to handle the border and the children, with component, but it is not recommended. Use paintComponent instead.
如果我理解得很好,您不应实现paint,因为它会调用paintBorder、paintChildren 和paintComponent。如果要使用组件处理边框和子项,请使用油漆,但不建议使用。改用paintComponent。
回答by Costis Aivalis
Hi I'm a newbie programmer I want to develop a Java program's GUI using Netbeans IDE
嗨,我是一名新手程序员,我想使用 Netbeans IDE 开发 Java 程序的 GUI
Hi! Since you are a newbie programmer, use Netbeans just as a regular text editor. In order to do it, create plain Java classes instead of Forms and do the coding yourself. The reason is that you have to understand Swing first, before having to deal with Netbeans approach.
你好!由于您是一名新手程序员,请将 Netbeans 用作常规文本编辑器。为此,请创建纯 Java 类而不是 Forms 并自己进行编码。原因是您必须先了解 Swing,然后才能处理 Netbeans 方法。
Should I create a new class implementing JPanel or JComponent, with my custom paint() function, instead ?
我应该使用自定义的paint() 函数创建一个实现JPanel 或JComponent 的新类吗?
You could certainly only extend JPanel or JComponent, since they are not Interfaces.
您当然只能扩展 JPanel 或 JComponent,因为它们不是接口。
If you insist on using Netbeans Mattise GUI editor, then create a new JPanel Form and override it's paintComponent
method.
如果您坚持使用 Netbeans Mattise GUI 编辑器,则创建一个新的 JPanel 表单并覆盖它的paintComponent
方法。
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// do your painting here...
g.setColor(Color.RED);
g.fillRect(6, 20, 12, 120);
g.setColor(Color.WHITE);
g.drawString("test", 50, 25);
}
Finally just add the component to your JFrame Form's constructor:
最后只需将组件添加到 JFrame 表单的构造函数中:
add(new MyNewJPanel());
pack();
Make sure that your JFrame's ContentPane has an appropriate layout.
确保您的 JFrame 的 ContentPane 具有适当的布局。