繁体   English   中英

如何使新图像出现在JFrame中?

[英]How to make a NEW image appear in JFrame?

我有以下代码:

 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中,该JTextArea由彩票组成, Clear清除所有输入和输出区域。

当我单击“ 确定”按钮时,如何使名为MyImage的JLabel更改其图像?(例如lotto1.png将变成lotto2.png)。我尝试了setText(),它仅在原始图像旁边添加了文本,我尝试了setIcon()再次,它没有用。 我还尝试过键入新的ImageIcon(“ lotto2.png”)仍然无法正常工作。 谢谢!

编码样式和格式太可怕了,而http://sscce.org/会花30行而不是750行(创建这样的示例时,您可能会自己找到解决方案)。

但是,线

MyImage.setIcon(new ImageIcon("lotto1.png"));

应该插入而不是MyImage.setText("lotto1.png");

如果这样做没有帮助:提供一个http://sscce.org/

使用Jlabel在JFrame上显示图像。 JButton ActionPerformedEvent以下行中编写

 myjLabel.setIcon(new javax.swing.ImageIcon("images/one.png"));

谢谢..

有点题外话

您应该通过URL将图像作为嵌入式资源加载。 可以使用getClass().getResource()获得URL,并将相对的类路径传递给.getResource() 像这样

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

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

ProjectRoot
          src
             resources
                     image.png

构建时, resources将被加载到类路径中。 将String传递给ImageIcon使其在文件系统上搜索路径。 认为这可能在您的开发环境中可行,那么您会发现在部署时它将无法正常工作。 在学习困难的方法并不得不重构大型项目之前,最好立即使用它(从类路径加载)。


旁注

  • 不要设置框架的大小。 而是使用pack()其设置为最小尺寸,同时考虑所有首选尺寸。 如果需要空白空间,请使用EmptyBorderMatteBorder

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM