如何从netbeans中的另一个类方法调用java swing jpanel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19812806/
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
How to call the java swing jpanel from another class method in netbeans?
提问by HackJavaMagicNumber
I have a Jpanel and I have to call this Jpanel from a class which have the main method. I have called the jpanel method inside my main method. But it not displaying. So how to do it in netbeans?
我有一个 Jpanel,我必须从具有 main 方法的类中调用这个 Jpanel。我在主方法中调用了 jpanel 方法。但它不显示。那么如何在netbeans中做到这一点呢?
public class A
{
public static void main(String[] args)
{
// TODO code application logic here
B b=new B();
b.caller();
}
}
Jpanel Class:
Jpanel类:
class B extends Jpanel{
public void caller()
{
initComponents();
}
}
where init component method is private.So I called this from another method inside the class which is public but nothing working. Help to solve this in netbeans
其中 init 组件方法是私有的。所以我从类中的另一个方法调用它,该方法是公共的但没有任何作用。帮助在netbeans中解决这个问题
采纳答案by dic19
I have called the jpanel method inside my main method. But it not displaying.
我在主方法中调用了 jpanel 方法。但它不显示。
JPanelis a generic lightweight container. It cannot be visible by itslef, you need to add it into a Window
(tipically JFrame
or JDialog
) in order to make it visible:
JPanel是一个通用的轻量级容器。它本身不可见,您需要将其添加到 a Window
(tipically JFrame
or JDialog
) 中以使其可见:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new B());
frame.pack();
frame.setVisible(true);
}
});
}
class B extends JPanel {
public B() {
initComponents();
}
private void initComponents(){...}
}
Take a look to How to Use Panelsand How to Make Frames (Main Windows)tutorials.
查看如何使用面板和如何制作框架(主窗口)教程。
回答by arcy
First of all, this has nothing to do with Netbeans - your question is entirely about how to write operating Swing code.
首先,这与 Netbeans 无关 - 您的问题完全是关于如何编写操作 Swing 代码。
Second, you haven't given enough information for us to know that you understand how Swing works, so it is difficult to give you correct advice. A JPanel doesn't display all on its own; it has to go into a container built for displaying on its own -- typically a JFrame.
其次,您没有提供足够的信息让我们知道您了解 Swing 的工作原理,因此很难为您提供正确的建议。JPanel 不会自行显示所有内容;它必须进入一个为自己显示而构建的容器——通常是一个 JFrame。
Your question about calling a JPanel from a different class, even a class that does not itself have (other) references to UI components, can be boiled down to a complete program of no more than 50 lines. Try writing that, and then ask a question about whatever you're having trouble with.
您关于从不同的类调用 JPanel 的问题,即使是一个本身没有(其他)对 UI 组件的引用的类,可以归结为一个不超过 50 行的完整程序。试着写下那个,然后就你遇到的任何问题提出一个问题。
回答by Ankit Rustagi
Assuming the caller method initializes the jpanel, it should return a JPanel, which you can then add to your JFrame
假设调用者方法初始化 jpanel,它应该返回一个 JPanel,然后您可以将其添加到 JFrame
class B extends Jpanel{
public JPanel caller() // notice no void, but JPanel
{
initComponents();
}
}
Then inside main
然后在主里面
public static void main(String[] args)
{
// TODO code application logic here
B b=new B();
myframe.setContentPane(b.caller());
}
回答by arti gupta
Drag your japnel class onto jframe (create a new jframe class..name it jpanel_frame) Then call constructor of jpanel_frame from already existing jframe class from which you want to call jpanel_frame . new jpanel_frame().setVisible(true);
将您的 japnel 类拖到 jframe (创建一个新的 jframe 类..将其命名为 jpanel_frame)然后从要从中调用 jpanel_frame 的现有 jframe 类调用 jpanel_frame 的构造函数。新 jpanel_frame().setVisible(true);