[英]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()
其設置為最小尺寸,同時考慮所有首選尺寸。 如果需要空白空間,請使用EmptyBorder
或MatteBorder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.