初学JAVA ,求大神指点。JPanel里嵌套了一个JPanel,为什么会重复出现一个JPanel,
答案:1 悬赏:10 手机版
解决时间 2021-04-13 13:21
- 提问者网友:骑士
- 2021-04-13 01:55
初学JAVA ,求大神指点。JPanel里嵌套了一个JPanel,为什么会重复出现一个JPanel,
最佳答案
- 五星知识达人网友:怀裏藏嬌
- 2021-04-13 02:53
import javax.swing.*;
import java.awt.*;
class GamePanel extends JPanel{
Image img = null;
JPanel jp = new JPanel();
public JPanel getjp()
{ return jp;
}
public GamePanel() {
//绿色组件JPanel,
jp.setBounds(490,200,140,200);
jp.setBackground(Color.green); //绿色背景
jp.setLayout(null);
// jp.setOpaque(false);
//添加JPanel
add(jp);
setLayout(null);
setVisible(true);
setBackground(Color.blue); //蓝色背景
setPreferredSize(new Dimension(640,482));
//获取背景图片
img = getToolkit().getImage("img/back.png");
}
public void paint(Graphics g) {
//写paint组件能显示用paint能显示背景图
g.drawImage(img,0,0,this);
paintComponents(g);
}
}
public class testPanel{
//程序入口
public static void main(String[] args) {
JFrame frame = new JFrame();
GamePanel gp = new GamePanel();
frame.add(gp.getjp());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
class GamePanel extends JPanel{
Image img = null;
JPanel jp = new JPanel();
public JPanel getjp()
{ return jp;
}
public GamePanel() {
//绿色组件JPanel,
jp.setBounds(490,200,140,200);
jp.setBackground(Color.green); //绿色背景
jp.setLayout(null);
// jp.setOpaque(false);
//添加JPanel
add(jp);
setLayout(null);
setVisible(true);
setBackground(Color.blue); //蓝色背景
setPreferredSize(new Dimension(640,482));
//获取背景图片
img = getToolkit().getImage("img/back.png");
}
public void paint(Graphics g) {
//写paint组件能显示用paint能显示背景图
g.drawImage(img,0,0,this);
paintComponents(g);
}
}
public class testPanel{
//程序入口
public static void main(String[] args) {
JFrame frame = new JFrame();
GamePanel gp = new GamePanel();
frame.add(gp.getjp());
frame.pack();
frame.setVisible(true);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯