使用Netbeans将Seperate类JPanel组件添加到JFrame类中 [英] Add Seperate class JPanel component into JFrame Class using Netbeans

查看:91
本文介绍了使用Netbeans将Seperate类JPanel组件添加到JFrame类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我希望一切都很好。



今天我有面对有关NetBean IDE的问题。



我创建两个单独的类,一个是MyPanel.java和Frame.java。



MyPanel.java从JPanel扩展



当我使用Netbeans创建JPanel的GUI时。我想简单地将它添加到我的主要Frame.java类中。



每当使用记事本执行此操作时,我已经完成了此任务



Hello,

I hope that all of thing is good.

Today, I have facing problem regarding NetBean IDE.

I am create two seperate classes one is MyPanel.java and Frame.java.

MyPanel.java is extend from JPanel

When I am create GUI of JPanel using Netbeans. I am want to simple add it into my main Frame.java class.

I am already achive this task whenever using Notepad for doing this all stuff

JPanel panel = new JPanel();
...

this.add(panel);
this.setVisible(true);





I JPanel在我的主要班级上可见。但我的主要问题是当我在Netbeans上做同样的工作时,JPanel没有显示在主要的ContentPane上。并且Netbean也没有发现任何错误。



我的所有组件(按钮,TextField,RadioButton等)都添加在MyPanel.java类上。我只是将这个组件添加到主Frame.java类中。



我希望你能尽快给我好的回复。



最好的问候,



Frame.java类



I JPanel is visible on my main class. But my main problem is when I am doing same work on Netbeans JPanel is not showing on main ContentPane. and Netbean is not shwoing any error as well.

My all component(button, TextField, RadioButton etc) is added on MyPanel.java class. I am simply add this component into main Frame.java Class.

I hope that you have give me good and positive reply soon.

Best Regards,

Frame.java class

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    MyPanel jPanel2 = new MyPanel();
  this.add(jPanel2);
    jPanel1.setVisible(false);

    jPanel2.setVisible(true);



}

推荐答案

你需要更新你的进行这些更改后的GUI。致电 JFrame.revalidate() [ ^ ]当你使用netbeans在Jframe中添加任何元素时,netbeans会使用其复杂的布局添加此组件(请参阅自动生成的代码)。
you need to update your GUI after you make those changes. A call for JFrame.revalidate()[^] will do the trick.

。因此,当您使用
this.add(jPanel2);

添加任何元素时,它不会出现在frame.beause因为它没有添加到框架中netbeans用于布局任何组件。所以最好的方法是不要直接在jframe中添加面板,而是首先在jrame中添加jscrollpane。然后在滚动窗格中添加所需的组件(jpanel)。

,its not appear in the frame.because it is not added to the frame what netbeans use to layout any component. So the best way is that do not add the panel directly in jframe instead first add a jscrollpane in jrame. And then add the required component(jpanel) in scrollpane.

jscrollpane.setViewportView(jPanel))


这篇关于使用Netbeans将Seperate类JPanel组件添加到JFrame类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆