初学JAVA ,求大神指点。JPanel里嵌套了一个JPanel,为什么会重复出现一个JPanel,
packageasd;importjavax.swing.*;publicclasstestPanel{//程序入口publicstaticvoidmain(String...
package asd;
import javax.swing.*;
public class testPanel{
//程序入口
public static void main(String[] args) {
JFrame frame = new JFrame();
GamePanel gp = new GamePanel();
frame.add(gp);
frame.pack();
frame.setVisible(true);
}
}
package asd;
import java.awt.*;
import javax.swing.*;
public class GamePanel extends JPanel{
Image img = null;
public GamePanel() {
//这是的绿色的子组件JPanel,
JPanel jp = new 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);
}
} 展开
import javax.swing.*;
public class testPanel{
//程序入口
public static void main(String[] args) {
JFrame frame = new JFrame();
GamePanel gp = new GamePanel();
frame.add(gp);
frame.pack();
frame.setVisible(true);
}
}
package asd;
import java.awt.*;
import javax.swing.*;
public class GamePanel extends JPanel{
Image img = null;
public GamePanel() {
//这是的绿色的子组件JPanel,
JPanel jp = new 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);
}
} 展开
1个回答
展开全部
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询