十万火急啊,明天交作业啊,有关背景图片不显示的,拜托各路神仙了
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
class MyButton extends Button{
Container con;
MyButton(){
this.con=con;
setVisible(true);
setSize(20,15);
}
}
class MyPanel extends JPanel implements MouseListener{
GridLayout grid;
int i,j;
int number=1;
Panel panel1,panel2;
Color a;
Color c;
Label label;
YourPanel yourpanel;
MyButton mybutton[][];
MyPanel(){
yourpanel=new YourPanel();
label=new Label("Have a try,maybe you just have one chance,however,that's game!",Label.CENTER);
label.setSize(450,120);
c=new Color(3,131,199);
label.setBackground(c);
Font font=new Font("default",Font.PLAIN+Font.ITALIC,15);
label.setFont(font);
panel1=new Panel();
panel2=new panel();
panel2.add(label);
grid=new GridLayout(6,9);
panel1.setLayout(grid);
mybutton=new MyButton[6][9];
for(i=0;i<6;i++)
for(j=0;j<9;j++){
mybutton[i][j]=new MyButton();
panel1.add(mybutton[i][j]);
mybutton[i][j].addMouseListener(this);
}
add(panel2);
add(panel1);
a=new Color(217,19,128);
setSize(225,90);
setVisible(true);
}
public void mousePressed(MouseEvent e){
if(number==26) {
remove(panel1);
remove(panel2);
add(yourpanel);
}
if(mybutton[0][1]==e.getSource())
{if(mybutton[0][1].getBackground()!=a)
{mybutton[0][1].setBackground(a);
++number;
}
}
else if(mybutton[0][2]==e.getSource())
{if(mybutton[0][2].getBackground()!=a)
{mybutton[0][2].setBackground(a);
++number;
}
}
else if(mybutton[0][5]==e.getSource())
{if(mybutton[0][5].getBackground()!=a)
{mybutton[0][5].setBackground(a);
++number;
}
}
else if(mybutton[0][8]==e.getSource())
{if(mybutton[0][8].getBackground()!=a)
{mybutton[0][8].setBackground(a);
++number;
}
}
else if(mybutton[1][0]==e.getSource())
{if(mybutton[1][0].getBackground()!=a)
{mybutton[1][0].setBackground(a);
++number;
}
}
else if(mybutton[1][4]==e.getSource())
{if(mybutton[1][4].getBackground()!=a)
{mybutton[1][4].setBackground(a);
++number;
}
}
else if(mybutton[1][6]==e.getSource())
{if(mybutton[1][6].getBackground()!=a)
{mybutton[1][6].setBackground(a);
++number;
}
}
else if(mybutton[1][8]==e.getSource())
{if(mybutton[1][8].getBackground()!=a)
{mybutton[1][8].setBackground(a);
++number;
}
}
else if(mybutton[2][0]==e.getSource())
{if(mybutton[2][0].getBackground()!=a)
{mybutton[2][0].setBackground(a);
++number;
}
}
else if(mybutton[2][4]==e.getSource())
{if(mybutton[2][4].getBackground()!=a)
{mybutton[2][4].setBackground(a);
++number;
}
}
else if(mybutton[2][6]==e.getSource())
{if(mybutton[2][6].getBackground()!=a)
{mybutton[2][6].setBackground(a);
++number;
}
}
else if(mybutton[2][8]==e.getSource())
{if(mybutton[2][8].getBackground()!=a)
{mybutton[2][8].setBackground(a);
++number;
}
}
else if(mybutton[3][0]==e.getSource())
{if(mybutton[3][0].getBackground()!=a)
{mybutton[3][0].setBackground(a);
++number;
}
}
else if(mybutton[3][4]==e.getSource())
{if(mybutton[3][4].getBackground()!=a)
{mybutton[3][4].setBackground(a);
++number;
}
}
else if(mybutton[3][5]==e.getSource())
{if(mybutton[3][5].getBackground()!=a)
{mybutton[3][5].setBackground(a);
++number;
}
}
else if(mybutton[3][6]==e.getSource())
{if(mybutton[3][6].getBackground()!=a)
{mybutton[3][6].setBackground(a);
++number;
}
}
else if(mybutton[3][8]==e.getSource())
{if(mybutton[3][8].getBackground()!=a)
{mybutton[3][8].setBackground(a);
++number;
}
}
else if(mybutton[4][0]==e.getSource())
{if(mybutton[4][0].getBackground()!=a)
{mybutton[4][0].setBackground(a);
++number;
}
}
else if(mybutton[4][4]==e.getSource())
{if(mybutton[4][4].getBackground()!=a)
{mybutton[4][4].setBackground(a);
++number;
}
}
else if(mybutton[4][6]==e.getSource())
{if(mybutton[4][6].getBackground()!=a)
{mybutton[4][6].setBackground(a);
++number;
}
}
else if(mybutton[4][8]==e.getSource())
{if(mybutton[4][8].getBackground()!=a)
{mybutton[4][8].setBackground(a);
++number;
}
}
else if(mybutton[5][1]==e.getSource())
{if(mybutton[5][1].getBackground()!=a)
{mybutton[5][1].setBackground(a);
++number;
}
}
else if(mybutton[5][2]==e.getSource())
{if(mybutton[5][2].getBackground()!=a)
{mybutton[5][2].setBackground(a);
++number;
}
}
else if(mybutton[5][4]==e.getSource())
{if(mybutton[5][4].getBackground()!=a)
{mybutton[5][4].setBackground(a);
++number;
}
}
else if(mybutton[5][6]==e.getSource())
{if(mybutton[5][6].getBackground()!=a)
{mybutton[5][6].setBackground(a);
++number;
}
}
else if(mybutton[5][8]==e.getSource())
{if(mybutton[5][8].getBackground()!=a)
{mybutton[5][8].setBackground(a);
++number;
}
}
else {
System.exit(2);}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(e.getClickCount()>=2)
{System.exit(1);
}
}
}
class YourPanel extends JPanel{
YourPanel() {
setLayout(null);
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon yourpanel = new ImageIcon("D:\\1.png");
g.drawImage(yourpanel.getImage(), 200, 250, 300, 400, this);
}
}
public class MyFrame extends JFrame{
MyPanel mypanel;
Label label;
MyFrame(String s){
super(s);
mypanel=new MyPanel();
setBounds(200,100,450,380);
add(mypanel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
new MyFrame("Je t'amine");
}
}
大哥大姐们,我又来了,为何我设置的背景图片不显示啊,图片麻烦大神自己定义个,
----------------解决方案--------------------------------------------------------
神仙为了瞪分而来
----------------解决方案--------------------------------------------------------
呵呵!!
----------------解决方案--------------------------------------------------------
最好断点调试下
----------------解决方案--------------------------------------------------------
看迷了眼...
-----------------------
斗破乾坤 http://www.5399.com/dpqk/
----------------解决方案--------------------------------------------------------
你的color直接 写setBackground(Color.red);
这样应该就行了
----------------解决方案--------------------------------------------------------