本文介绍了如何将JPanel添加到JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我在Java中有2个课程.
1)扩展Jframe的类
2)另一种扩展JPanel
我尝试在JFram类中添加JPanel,它看起来像这样:
Hello,
i have 2 classes in java.
1)a class that extends Jframe
2)another that extends JPanel
I try to add the JPanel in the JFram class , it looks something like this:
public static void main(String args[]) { // the main is in the JFram class
Othello Oth= new Othello(); // instance of the JFram class
Oth.setLocation(250, 150);
OthelloBoard OB = new OthelloBoard(); // instance of JPanel class
Oth.add(OB);
Oth.setVisible(true);
但是只显示了JFrame,而且看来我添加的JPanel并没有进入JFrame(我正在使用NetBeans).
我该如何解决这个问题?
谢谢...
but only the JFrame got shown and it seems that the JPanel i added did not go onto the JFrame (i am using NetBeans).
How can I solve this problem??
Thank you...
推荐答案
public static void main(String args[]) {
// the main is in the JFram class
Othello Oth= new Othello(); // instance of the JFram class
Oth.setLocation(250, 150);
OthelloBoard OB = new OthelloBoard(); // instance of JPanel class
Oth.setLayout(new GridLayout(1,1));
///Oth.add(OB,BorderLayout.CENTER);
//Oth.setContentPane( BorderLayout.CENTER);
Oth.setVisible(true);
并请
查看这个将面板添加到框架的经典示例
面板到框架
[ ^ ]
and please
review this classic example of adding panel to frame
panel to frame
[^]
public class Othello extends javax.swing.JFrame {
OthelloBoard OB;
/** Creates new form Othello */
public Othello() {
initComponents();
OB = new OthelloBoard();
add(OB,BorderLayout.CENTER);
setVisible(true);
}
public static void main(String args[]) {
Othello Oth= new Othello();
Oth.setLocation(250, 150);
}
这篇关于如何将JPanel添加到JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!