如何在JFrame中显示新图像?

时间:2014-02-04 09:25:36

标签: java image swing imageicon

我有这段代码:

 import java.lang.*;
 import javax.swing.*;
 import java.awt.event.*;
 import java.awt.*;
 import java.lang.*;
 import java.io.*;
 import java.util.*;
 import java.text.*;
 import java.*;


 public class LottoGUI extends JFrame
                  implements ActionListener

 {
 Random rand = new Random();
 int randomNum = rand.nextInt((4 - 1) + 1) + 1;

String mine="",mine2="";
String help;


  JTextArea test = new JTextArea(0,0);

  JTextArea test2 = new JTextArea(0,0);

  JTextArea test4 = new JTextArea(0,0);

  JTextArea test3 = new JTextArea(0,0);

  JTextArea test5 = new JTextArea(10,30);
   JScrollPane test55 =new JScrollPane(test5); 

  /*
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    JLabel MyImage = new JLabel(new ImageIcon("image"+randomNum+".png"));
  */



JLabel lab  =    new JLabel(mine);
JLabel lab2 =    new JLabel(mine2);
JLabel lab3 =    new JLabel("Type 1 for a new seat.");

JTextField labf = new JTextField(10);
JTextField lab2f = new JTextField(10);
JTextField lab3f = new JTextField(10);

JButton labb =    new JButton("OK!");
JButton lab2b=    new JButton("Clear!");
JButton lab3b=    new JButton("Again!");


JLabel MyImage2 = new JLabel(new ImageIcon("smea"+randomNum+".gif"));
JLabel MyImage = new JLabel(new ImageIcon("lottery.png"));
int col1,col2,col3;


public LottoGUI(String one, String two,String three)
{


    Container C= getContentPane();
    C.setLayout(new FlowLayout());

    Font font = new Font("Impact", Font.ITALIC +Font.BOLD, 30);
        test.setFont(font);
         col1=ColorGen();
         col2=ColorGen();
         col3=ColorGen();
        test.setForeground(new Color(col1,col2,col3));
        test.setBackground(new Color(255-col1,255-col2,255-col3));
        test.setText(one);
        test.setEditable(false);
    Font font2 = new Font("Verdana", Font.BOLD, 12);
        test2.setFont(font2);
         col1=ColorGen();
         col2=ColorGen();
         col3=ColorGen();
        test2.setForeground(new Color(col1,col2,col3));
        test2.setBackground(new Color(255-col1,255-col2,255-col3));
        test2.setText(two);
        test2.setEditable(false);
    Font font3 = new Font("Sans", Font.BOLD, 12);
        test3.setFont(font3);
         col1=ColorGen();
         col2=ColorGen();
         col3=ColorGen();
        test3.setForeground(new Color(col1,col2,col3));
        test3.setBackground(new Color(255-col1,255-col2,255-col3));
    Font font4 = new Font("Calibri", Font.BOLD, 12);
        test4.setFont(font3);
         col1=ColorGen();
         col2=ColorGen();
         col3=ColorGen();
        test4.setEditable(false);
        test4.setText(three);
        test4.setForeground(new Color(col1,col2,col3));
        test4.setBackground(new Color(255-col1,255-col2,255-col3));



    labf.setEditable(true);
         col1=ColorGen();
         col2=ColorGen();
         col3=ColorGen();
        labf.setBackground(new Color(col1,col2,col3));
        labf.setFont(new Font("Verdana", Font.ITALIC,24));
        labf.setForeground(new Color(255-col1,255-col2,255-col3));
    lab2f.setEditable(true);
         col1=ColorGen();
         col2=ColorGen();
         col3=ColorGen();
        lab2f.setBackground(new Color(col1,col2,col3));
        lab2f.setFont(new Font("Verdana", Font.ITALIC,24));
        lab2f.setForeground(new Color(255-col1,255-col2,255-col3));

    //labb
         col1=ColorGen();
         col2=ColorGen();
         col3=ColorGen();
        labb.setBackground(Color.DARK_GRAY);
        labb.setFont(new Font("Verdana", Font.ITALIC,24));
        labb.setForeground(Color.white);
    lab2f.setEditable(true);
         col1=ColorGen();
         col2=ColorGen();
         col3=ColorGen();
        lab2f.setBackground(new Color(col1,col2,col3));
        lab2f.setFont(new Font("Verdana", Font.ITALIC,24));
        lab2f.setForeground(new Color(255-col1,255-col2,255-col3));



    C.add(MyImage);
    C.add(test);
    C.add(test2);
    C.add(test3);
    C.add(labf);
    C.add(test4);
    C.add(lab2f);
    C.add(test55);
    //C.add(lab);

    //C.add(lab2);

    C.add(labb);
    C.add(lab2b);


    C.setBackground(new Color(ColorGen(),ColorGen(),ColorGen()));


    labb.addActionListener(this);
    lab2b.addActionListener(this);
    labf.addActionListener(this);


    setLocation(100,10);
    setResizable(false);
    /*setIconImage(
    Toolkit.getDefaultToolkit().getImage("Duke.jpg"));*/
    setSize(550,900);
    setVisible(true);
    setTitle("Ultra Lotto!!!");
}

int a1[]= new int[6];
int aMaster[]= new int[6];

int b=0,c=0,d=0,E=0;
int x=0,x1=0;
int y=0;
int coun = 0;
String Holp="",Help="",Hilp="";

public void actionPerformed(ActionEvent e)
 {  

if(e.getSource()==lab2b)
{
    labf.setText("");
    lab2f.setText("");
    test5.setText("");
}

if(e.getSource()==lab2f||e.getSource()==labb)
{
    //test2.setText("");
    //test3.setText("");
    //test4.setText("");
    c=Integer.parseInt(lab2f.getText());
    b=Integer.parseInt(labf.getText());
    help="";
    switch(b)
        {

        case 1:
            coun=1;
            Holp ="6-42 Lotto:\n";


    //Winning number!
        for(x=0; x<aMaster.length; x++)
                {
                    if(x==0)
                    {
                    aMaster[x]= AllGen(1,42);
                    }

                    if(x>0)
                    {
                    aMaster[x]= AllGen(1,42);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(aMaster[x] == aMaster[x-x1])
                        x--;
                        }
                    }
                }
        help += Holp+"Winning Combination :" +"\n" +aMaster[0]+"    " +aMaster[1]+"    "+aMaster[2]+"    "+aMaster[3]+"    "+aMaster[4]+"    "+aMaster[5]+"\n";

    //Start Player!

            for(E=1; E<=c;E++)
            {

                for(x=0; x<a1.length; x++)
                {
                    if(x==0)
                    {
                    a1[x]= AllGen(1,42);
                    }

                    if(x>0)
                    {
                    a1[x]= AllGen(1,42);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(a1[x] == a1[x-x1])
                        x--;
                        }
                    }
                }
            d++;    

            help += "   Player "+d +"\n" +"   "+a1[0]+"    " +a1[1]+"    "+a1[2]+"    "+a1[3]+"    "+a1[4]+"    "+a1[5]+"\n";


            //check!

            for(x=0; x<aMaster.length; x++)
                {
                        for(x1=0; x1<aMaster.length; x1++)
                    {
                        if(a1[x1]==aMaster[x])
                        {
                            ++y;
                        }
                    }
                }

            if(y==6)
            {
                Hilp += "Player " +d +" wins!!!\n";
            }

            y=0;

        }
            break;

            case 2:
                 coun=2;
            Holp ="Mega Lotto:\n";


    //Winning number!
        for(x=0; x<aMaster.length; x++)
                {
                    if(x==0)
                    {
                    aMaster[x]= AllGen(1,45);
                    }

                    if(x>0)
                    {
                    aMaster[x]= AllGen(1,45);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(aMaster[x] == aMaster[x-x1])
                        x--;
                        }
                    }
                }
        help += Holp+"Winning Combination :" +"\n" +aMaster[0]+"    " +aMaster[1]+"    "+aMaster[2]+"    "+aMaster[3]+"    "+aMaster[4]+"    "+aMaster[5]+"\n";

    //Start Player!

            for(E=1; E<=c;E++)
            {

                for(x=0; x<a1.length; x++)
                {
                    if(x==0)
                    {
                    a1[x]= AllGen(1,45);
                    }

                    if(x>0)
                    {
                    a1[x]= AllGen(1,45);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(a1[x] == a1[x-x1])
                        x--;
                        }
                    }
                }
            d++;    

            help += "   Player "+d +"\n" +"   "+a1[0]+"    " +a1[1]+"    "+a1[2]+"    "+a1[3]+"    "+a1[4]+"    "+a1[5]+"\n";


            //check!

            for(x=0; x<aMaster.length; x++)
                {
                        for(x1=0; x1<aMaster.length; x1++)
                    {
                        if(a1[x1]==aMaster[x])
                        {
                            ++y;
                        }
                    }
                }

            if(y==6)
            {
                Hilp += "Player " +d +" wins!!!\n";
            }

            y=0;

        }
            break;  

        case 3:
            coun=3;
            Holp ="Super Lotto:\n";


    //Winning number!
        for(x=0; x<aMaster.length; x++)
                {
                    if(x==0)
                    {
                    aMaster[x]= AllGen(1,49);
                    }

                    if(x>0)
                    {
                    aMaster[x]= AllGen(1,49);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(aMaster[x] == aMaster[x-x1])
                        x--;
                        }
                    }
                }
        help += Holp+"Winning Combination :" +"\n" +aMaster[0]+"    " +aMaster[1]+"    "+aMaster[2]+"    "+aMaster[3]+"    "+aMaster[4]+"    "+aMaster[5]+"\n";

    //Start Player!

            for(E=1; E<=c;E++)
            {

                for(x=0; x<a1.length; x++)
                {
                    if(x==0)
                    {
                    a1[x]= AllGen(1,49);
                    }

                    if(x>0)
                    {
                    a1[x]= AllGen(1,49);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(a1[x] == a1[x-x1])
                        x--;
                        }
                    }
                }
            d++;    

            help += "   Player "+d +"\n" +"   "+a1[0]+"    " +a1[1]+"    "+a1[2]+"    "+a1[3]+"    "+a1[4]+"    "+a1[5]+"\n";


            //check!

            for(x=0; x<aMaster.length; x++)
                {
                        for(x1=0; x1<aMaster.length; x1++)
                    {
                        if(a1[x1]==aMaster[x])
                        {
                            ++y;
                        }
                    }
                }

            if(y==6)
            {
                Hilp += "Player " +d +" wins!!!\n";
            }

            y=0;

        }
            break;
        case 4:
            int coun=4;
            Holp ="Grand Lotto:\n";


    //Winning number!
        for(x=0; x<aMaster.length; x++)
                {
                    if(x==0)
                    {
                    aMaster[x]= AllGen(1,55);
                    }

                    if(x>0)
                    {
                    aMaster[x]= AllGen(1,55);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(aMaster[x] == aMaster[x-x1])
                        x--;
                        }
                    }
                }
        help += Holp+"Winning Combination :" +"\n" +aMaster[0]+"    " +aMaster[1]+"    "+aMaster[2]+"    "+aMaster[3]+"    "+aMaster[4]+"    "+aMaster[5]+"\n";

    //Start Player!

            for(E=1; E<=c;E++)
            {

                for(x=0; x<a1.length; x++)
                {
                    if(x==0)
                    {
                    a1[x]= AllGen(1,55);
                    }

                    if(x>0)
                    {
                    a1[x]= AllGen(1,55);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(a1[x] == a1[x-x1])
                        x--;
                        }
                    }
                }
            d++;    

            help += "   Player "+d +"\n" +"   "+a1[0]+"    " +a1[1]+"    "+a1[2]+"    "+a1[3]+"    "+a1[4]+"    "+a1[5]+"\n";


            //check!

            for(x=0; x<aMaster.length; x++)
                {
                        for(x1=0; x1<aMaster.length; x1++)
                    {
                        if(a1[x1]==aMaster[x])
                        {
                            ++y;
                        }
                    }
                }

            if(y==6)
            {
                Hilp += "Player " +d +" wins!!!\n";
            }

            y=0;

        }
            break;

    case 5:
        coun=5;
    Holp ="Easy Lotto:\n";

    //Winning number!
        for(x=0; x<aMaster.length; x++)
                {
                    if(x==0)
                    {
                    aMaster[x]= AllGen(1,6);
                    }

                    if(x>0)
                    {
                    aMaster[x]= AllGen(1,6);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(aMaster[x] == aMaster[x-x1])
                        x--;
                        }
                    }
                }
        help += Holp+"Winning Combination :" +"\n" +aMaster[0]+"    " +aMaster[1]+"    "+aMaster[2]+"    "+aMaster[3]+"    "+aMaster[4]+"    "+aMaster[5]+"\n";

    //Start Player!

            for(E=1; E<=c;E++)
            {

                for(x=0; x<a1.length; x++)
                {
                    if(x==0)
                    {
                    a1[x]= AllGen(1,6);
                    }

                    if(x>0)
                    {
                    a1[x]= AllGen(1,6);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(a1[x] == a1[x-x1])
                        x--;
                        }
                    }
                }
            d++;    

            help += "   Player "+d +"\n" +"   "+a1[0]+"    " +a1[1]+"    "+a1[2]+"    "+a1[3]+"    "+a1[4]+"    "+a1[5]+"\n";


            //check!

            for(x=0; x<aMaster.length; x++)
                {
                        for(x1=0; x1<aMaster.length; x1++)
                    {
                        if(a1[x1]==aMaster[x])
                        {
                            ++y;
                        }
                    }
                }

            if(y==6)
            {
                Hilp += "Player " +d +" wins!!!\n";
            }

            y=0;

        }
            break;

        case 6:
            coun=6;
    Holp ="1-10 Easy Lotto:\n";

    //Winning number!
        for(x=0; x<aMaster.length; x++)
                {
                    if(x==0)
                    {
                    aMaster[x]= AllGen(1,10);
                    }

                    if(x>0)
                    {
                    aMaster[x]= AllGen(1,10);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(aMaster[x] == aMaster[x-x1])
                        x--;
                        }
                    }
                }
        help += Holp+"Winning Combination :" +"\n" +aMaster[0]+"    " +aMaster[1]+"    "+aMaster[2]+"    "+aMaster[3]+"    "+aMaster[4]+"    "+aMaster[5]+"\n";

    //Start Player!

            for(E=1; E<=c;E++)
            {

                for(x=0; x<a1.length; x++)
                {
                    if(x==0)
                    {
                    a1[x]= AllGen(1,10);
                    }

                    if(x>0)
                    {
                    a1[x]= AllGen(1,10);
                        for(x1=1; x1<=x; x1++)
                        {
                        if(a1[x] == a1[x-x1])
                        x--;
                        }
                    }
                }
            d++;    

            help += "   Player "+d +"\n" +"   "+a1[0]+"    " +a1[1]+"    "+a1[2]+"    "+a1[3]+"    "+a1[4]+"    "+a1[5]+"\n";


            //check!

            for(x=0; x<aMaster.length; x++)
                {
                        for(x1=0; x1<aMaster.length; x1++)
                    {
                        if(a1[x1]==aMaster[x])
                        {
                            y++;
                        }
                    }
                }

            if(y==6)
            {
                Hilp += "Player " +d +" wins!!!\n";
            }

            y=0;

        }
            break;

        default:
        help = "Please As I SAID TYPE ONLY 1-6!!!";
        }

MyImage.setText("lotto1.png");


test5.setText(help +"\n" +Hilp +"\n");
b=0;c=0;d=0;E=0;
x=0;x1=0;
y=0;
Holp="";Help="";Hilp="";

MyArea(help +"\n" +Hilp +"\n",
10,10,
ColorGen(),ColorGen(),ColorGen(),
ColorGen(),ColorGen(),ColorGen());

}

 }

 protected ImageIcon createImageIcon(String path,String description) 
 {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) 
{
    return new ImageIcon(imgURL, description);
} 
else 
{
    System.err.println("Couldn't find file: " + path);
    return null;
     }
 }

public static JScrollPane MyArea(String walalang,int x1,int y1,int c1,int c2,int             c3,int c4,int c5,int c6)
{   
JTextArea omni= new JTextArea(x1,y1);
JScrollPane Omni= new JScrollPane(omni);
Font font = new Font("Verdana", Font.BOLD, 12);
omni.setFont(font);
omni.setForeground(new Color(c1,c2,c3));
omni.setBackground(new Color(c4,c5,c6));
omni.setText(walalang);

return Omni;

} 

public static int ColorGen()
{
int colorgen=(int)(0+Math.random()*256);
return colorgen;
}

public static int AllGen(int x, int y)
{
int allgen=(int)(x+Math.random()*y);
return allgen;
}       

public static int randomMessage()
{
int xol=0, xola=0;
xol = AllGen(1,5);
switch(xol)
{
    case 1:
    xola = JOptionPane.WARNING_MESSAGE;
    break;
    case 2:
    xola = JOptionPane.INFORMATION_MESSAGE;
    break;
    case 3:
    xola = JOptionPane.ERROR_MESSAGE;
    break;
    case 4:
    xola = JOptionPane.QUESTION_MESSAGE;
    break;
    case 5:
    xola = JOptionPane.PLAIN_MESSAGE;
    break;
}

return xola;
}   

public static void main(String args[])
{
String halp="",hilp="",holp="",hulp="";
halp="  Welcome to the LOTTERY!!!  ";
hilp="Type [1] for 6-42\nType [2] for Mega\nType [3] for Super\nType [4] for Grand\nType [5] for 1-6 Easy Lotto\nType [6] for 1-10 Easy Lotto";
holp="How many players?                          ";

LottoGUI me=new LottoGUI(halp,hilp,holp);
//LottoGUI me2=new LottoGUI("","","");
me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//me2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
}

}

如您所见,这是一个“短期”的彩票计划。

有2个按钮,确定清除按钮,确定按钮使文本显示在JTextArea中,其中包含彩票,清除清除输入和输出的所有区域。

当我点击确定按钮时,如何让名为 MyImage 的JLabel改变它的图像?(比如说lotto1.png会变成lotto2.png)我试过setText()它只在原始图像旁边添加了文本,我再次尝试setIcon(),它没有用。我也尝试过输入新的ImageIcon(“lotto2.png”)仍然无法正常工作。谢谢!

3 个答案:

答案 0 :(得分:0)

编码风格和格式很糟糕,http://sscce.org/需要30行而不是750行(你可能在创建这样的例子时自己找到了解决方案)。

然而,行

MyImage.setIcon(new ImageIcon("lotto1.png"));
应该插入

而不是MyImage.setText("lotto1.png");

如果这没有帮助:提供http://sscce.org/

答案 1 :(得分:0)

使用Jlabel在JFrame上显示图像。在JButton ActionPerformedEvent

上写下以下一行
 myjLabel.setIcon(new javax.swing.ImageIcon("images/one.png"));

谢谢..

答案 2 :(得分:0)

稍微偏离主题

您应该通过网址将图片加载为embedded resources。可以使用getClass().getResource()获取URL,并将相对类路径传递给.getResource()。像这样的东西

ImageIcon icon = new ImageIcon(LottoGUI.class.getResource("/resources/image.png"));

其中image.png位于resources(或您选择的任何一个)src

ProjectRoot
          src
             resources
                     image.png

构建时,resources将加载到类路径中。将字符串传递给ImageIcon使其在文件系统上搜索路径。认为这可能适用于您的开发环境,您会发现它在部署时不起作用。最好在你学习艰难的方法之前立即使用它(从类路径加载)并且必须重构一个大项目。


附注

  • 不要设置框架的大小。取而代之的是pack(),将框架设置为最小尺寸,并尊重所有首选尺寸。如果您想要空白空间,请使用EmptyBorderMatteBorder