在另一个类函数中访问动态组件(即新标签)?
我目前正在开发 Swing Applet,并且在其他类函数中引用我的自定义 AWT Canvas 组件(非常简单的扩展类)时遇到问题,例如通常使用 Netbean (7.0) 设计器创建的任何其他组件(即按钮) 。
我在此处添加的自定义画布元素,我确信这将是适当的位置(特别是在所有其他生成的组件刚刚在同一区域中创建之后)
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
CustomCanvas myCan = new CustomCanvas();
myCan.setBounds(100, 100, 200, 200);
getContentPane().add(myCan);
...
但是,与生成的组件不同,我无法通过名称访问它们,并且似乎无法访问他们也可以通过其他方式(this.myCan)。我已经设置了一个示例函数,它将处理 Swing 表单上的(生成的)按钮,以操作先前实例化的 myCan 组件:
private void btnManipCanvasActionPerformed(java.awt.event.ActionEvent evt) {
//Essentially Was wanting to call something here such as myCan.getGraphics().setRect...
}
您知道在那里访问 myCan 的方法吗?我是否应该将组件的自定义初始化放在不同的区域中,以便可以公开访问它们?
I am currently working on a Swing Applet, and am having issues referencing my custom AWT Canvas component (very simple extended class) in other class functions, such as with any other component (i.e. button) normally created with Netbean (7.0)'s designer.
My custom canvas element I add here, I was sure this would be the appropriate place (especially after all other generated components were just created in the same area)
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
CustomCanvas myCan = new CustomCanvas();
myCan.setBounds(100, 100, 200, 200);
getContentPane().add(myCan);
...
However, unlike the generated components, I cannot access them by name and cannot seem to access them through other means (this.myCan) either. I have set up a sample function that will handle a (generated) button on the Swing form to manipulate the previously instantiated myCan component:
private void btnManipCanvasActionPerformed(java.awt.event.ActionEvent evt) {
//Essentially Was wanting to call something here such as myCan.getGraphics().setRect...
}
Do you know of a way to access myCan there? Am I supposed to place custom initializations of components in a different area so they can be publicly accessed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将画布设置为实例变量,就像(我猜)Netbeans 设计器创建的所有其他组件一样:
Just make the canvas an instance variable, as (I guess) all the other components created by the Netbeans designer:
可能存在问题或绘画缺失,因为您可能将 ATW 组件 与Swing JComponets,
如果没有任何来自 OpenGL 的特殊信息,请查找 JPanel 而不是 AWT Canvas 以及 Swing 将所有内容重定向到 JLabel
请阅读如何LayoutManagers 可以避免
setBounds(int, int, int, int);
there are possible issues or painting lacks because you probably mixing ATW Components with Swing JComponets,
if there nothing special that came from OpenGL, then look for JPanel instead of AWT Canvas and for all panting in Swing redirect everythigns to JLabel
please read how LayoutManagers works to avoids
setBounds(int, int, int, int);