抽奖器基本做好了,现在有个问题就是我在images文件夹里放了一些图片,为.jpg/.png/.gif格式的,现在我加进去一张.bmp格式的图片想做抽奖器默认显示图片,相当于封面,我的想法是把它放在image[0]的位置,但是总是失败,代码如下,求高手解决。import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;import java.util.*;import java.io.FileReader;
import java.io.File;// This example demonstrates the use of JButton, JTextField
// and JLabel.
public class LunarPhases implements ActionListener {
final static int NUM_IMAGES = 1000; final static int START_INDEX = 1;
int REAL_NUM_IMAGES = 0; ImageIcon[] images = new ImageIcon[NUM_IMAGES];
String[] imageNames = new String[NUM_IMAGES]; JPanel mainPanel, selectPanel, displayPanel, resultPanel; JButton phaseChoice = null; JLabel phaseIconLabel = null, phaseResult = null, phaseBlank1 = null, phaseBlank2 = null; // Constructor
public LunarPhases() {
// Create the phase selection and display panels.
selectPanel = new JPanel();
displayPanel = new JPanel();
resultPanel = new JPanel(); // Add various widgets to the sub panels.
addWidgets(); // Create the main panel to contain the two sub panels.
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1, 3, 5, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Add the select and display panels to the main panel.
mainPanel.add(selectPanel);
mainPanel.add(displayPanel);
mainPanel.add(resultPanel);
} // Create and the widgets to select and display the phases of the moon.
private void addWidgets() {
// Get the images and put them into an array of ImageIcon.
File dir = new File("H:\\java\\bin\\images");
File[] files = dir.listFiles();
String imageName = null;
String temp = null;
int j = 1;
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
imageName = "images/" + files[i].getName();
}
temp = imageName.substring(imageName.lastIndexOf(".") + 1, imageName.length()); if(!temp.equals("gif")&&!temp.equals("jpg")&&!temp.equals("png")&&!temp.equals("bmp"))
{
continue;
}
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
if(temp.equals("bmp"))
{
images[0] = icon;
continue;
}
images[j] = icon;
imageNames[j] = imageName.substring(7,imageName.lastIndexOf("."));
j++;
}
REAL_NUM_IMAGES = j - 1; // Create label for displaying moon phase images and put a border around
// it.
phaseIconLabel = new JLabel();
phaseIconLabel.setHorizontalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalTextPosition(JLabel.CENTER);
phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER);
phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 10, 0), phaseIconLabel
.getBorder()));
phaseResult = new JLabel();
phaseBlank1 = new JLabel();
phaseBlank2 = new JLabel();
// Create combo box with lunar phase choices.
phaseChoice = new JButton("开始/停止"); // Display the first image.
phaseIconLabel.setIcon(images[START_INDEX]);
phaseIconLabel.setText(""); // Add border around the select panel.
selectPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Select Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
resultPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Result Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); // Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Display Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); // Add moon phases combo box to select panel and image label to
// displayPanel.
selectPanel.add(phaseChoice);
displayPanel.add(phaseIconLabel);
resultPanel.add(phaseBlank1);
resultPanel.add(phaseResult);
resultPanel.add(phaseBlank2); // Listen to events from combo box.
phaseChoice.addActionListener(this);
} boolean run = false;
// Implementation of ActionListener interface.
public void actionPerformed(ActionEvent event) {
if(run){
run = false;
phaseBlank1.setText("恭喜");
phaseBlank2.setText("中奖啦!");
}
else{
run = true; new Thread(){
public void run(){
while(run){
int a =(int)( Math.random()*REAL_NUM_IMAGES) + 1;
System.out.println(a);
phaseIconLabel.setIcon(images[a]);
phaseBlank1.setText("");
phaseResult.setText(imageNames[a]);
phaseBlank2.setText("");
try{
Thread.sleep(1000);
}
catch(Exception e){}
}
}
}.start();
} } // main method
public static void main(String[] args) {
// create a new instance of LunarPhases
LunarPhases phases = new LunarPhases(); // Create a frame and container for the panels.
JFrame lunarPhasesFrame = new JFrame("Lunar Phases"); // Set the look and feel.
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
} lunarPhasesFrame.setContentPane(phases.mainPanel); // Exit when the window is closed.
lunarPhasesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Show the converter.
lunarPhasesFrame.pack();
lunarPhasesFrame.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;import java.util.*;import java.io.FileReader;
import java.io.File;// This example demonstrates the use of JButton, JTextField
// and JLabel.
public class LunarPhases implements ActionListener {
final static int NUM_IMAGES = 1000; final static int START_INDEX = 1;
int REAL_NUM_IMAGES = 0; ImageIcon[] images = new ImageIcon[NUM_IMAGES];
String[] imageNames = new String[NUM_IMAGES]; JPanel mainPanel, selectPanel, displayPanel, resultPanel; JButton phaseChoice = null; JLabel phaseIconLabel = null, phaseResult = null, phaseBlank1 = null, phaseBlank2 = null; // Constructor
public LunarPhases() {
// Create the phase selection and display panels.
selectPanel = new JPanel();
displayPanel = new JPanel();
resultPanel = new JPanel(); // Add various widgets to the sub panels.
addWidgets(); // Create the main panel to contain the two sub panels.
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1, 3, 5, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Add the select and display panels to the main panel.
mainPanel.add(selectPanel);
mainPanel.add(displayPanel);
mainPanel.add(resultPanel);
} // Create and the widgets to select and display the phases of the moon.
private void addWidgets() {
// Get the images and put them into an array of ImageIcon.
File dir = new File("H:\\java\\bin\\images");
File[] files = dir.listFiles();
String imageName = null;
String temp = null;
int j = 1;
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
imageName = "images/" + files[i].getName();
}
temp = imageName.substring(imageName.lastIndexOf(".") + 1, imageName.length()); if(!temp.equals("gif")&&!temp.equals("jpg")&&!temp.equals("png")&&!temp.equals("bmp"))
{
continue;
}
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
if(temp.equals("bmp"))
{
images[0] = icon;
continue;
}
images[j] = icon;
imageNames[j] = imageName.substring(7,imageName.lastIndexOf("."));
j++;
}
REAL_NUM_IMAGES = j - 1; // Create label for displaying moon phase images and put a border around
// it.
phaseIconLabel = new JLabel();
phaseIconLabel.setHorizontalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalTextPosition(JLabel.CENTER);
phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER);
phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 10, 0), phaseIconLabel
.getBorder()));
phaseResult = new JLabel();
phaseBlank1 = new JLabel();
phaseBlank2 = new JLabel();
// Create combo box with lunar phase choices.
phaseChoice = new JButton("开始/停止"); // Display the first image.
phaseIconLabel.setIcon(images[START_INDEX]);
phaseIconLabel.setText(""); // Add border around the select panel.
selectPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Select Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
resultPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Result Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); // Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Display Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); // Add moon phases combo box to select panel and image label to
// displayPanel.
selectPanel.add(phaseChoice);
displayPanel.add(phaseIconLabel);
resultPanel.add(phaseBlank1);
resultPanel.add(phaseResult);
resultPanel.add(phaseBlank2); // Listen to events from combo box.
phaseChoice.addActionListener(this);
} boolean run = false;
// Implementation of ActionListener interface.
public void actionPerformed(ActionEvent event) {
if(run){
run = false;
phaseBlank1.setText("恭喜");
phaseBlank2.setText("中奖啦!");
}
else{
run = true; new Thread(){
public void run(){
while(run){
int a =(int)( Math.random()*REAL_NUM_IMAGES) + 1;
System.out.println(a);
phaseIconLabel.setIcon(images[a]);
phaseBlank1.setText("");
phaseResult.setText(imageNames[a]);
phaseBlank2.setText("");
try{
Thread.sleep(1000);
}
catch(Exception e){}
}
}
}.start();
} } // main method
public static void main(String[] args) {
// create a new instance of LunarPhases
LunarPhases phases = new LunarPhases(); // Create a frame and container for the panels.
JFrame lunarPhasesFrame = new JFrame("Lunar Phases"); // Set the look and feel.
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
} lunarPhasesFrame.setContentPane(phases.mainPanel); // Exit when the window is closed.
lunarPhasesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Show the converter.
lunarPhasesFrame.pack();
lunarPhasesFrame.setVisible(true);
}
}
BMP格式的图片不显示有2个原因:
1. BMP格式需要JDK1.5+才支持, 如果楼主使用的是1.4的JDK的话, 需要转换图片格式
2. 在JDK1.5+的环境中, 代码改一下即可顺序运行: URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);改为:URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon;
try {
BufferedImage bi = ImageIO.read(iconURL);
if(bi == null) continue;
icon = new ImageIcon(bi);
} catch (IOException e) {
continue;
}
ImageIcon icon = new ImageIcon(imageName);
直接new ImageIcon(iconURL)不能显示BMP图片
改为new ImageIcon(ImageIO.read(iconURL))即可显示BMP图片事实上我的截图中的红色图片就是用画图工具做的一个BMP图片
你看到了, 可以正常显示!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;// This example demonstrates the use of JButton, JTextField
// and JLabel.
public class LunarPhases implements ActionListener {
final static int NUM_IMAGES = 1000; final static int START_INDEX = 0; int REAL_NUM_IMAGES = 0; ImageIcon[] images = new ImageIcon[NUM_IMAGES]; String[] imageNames = new String[NUM_IMAGES]; JPanel mainPanel, selectPanel, displayPanel, resultPanel; JButton phaseChoice = null; JLabel phaseIconLabel = null, phaseResult = null, phaseBlank1 = null,
phaseBlank2 = null; // Constructor
public LunarPhases() {
// Create the phase selection and display panels.
selectPanel = new JPanel();
displayPanel = new JPanel();
resultPanel = new JPanel(); // Add various widgets to the sub panels.
addWidgets(); // Create the main panel to contain the two sub panels.
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1, 3, 5, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Add the select and display panels to the main panel.
mainPanel.add(selectPanel);
mainPanel.add(displayPanel);
mainPanel.add(resultPanel);
} // Create and the widgets to select and display the phases of the moon.
private void addWidgets() {
// Get the images and put them into an array of ImageIcon.
File dir = new File("E:\\workspace\\Test\\bin\\images");
File[] files = dir.listFiles();
String imageName = null;
String temp = null;
int j = 1;
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
imageName = "images/" + files[i].getName();
}
temp = imageName.substring(imageName.lastIndexOf(".") + 1,
imageName.length()); if (!temp.equals("gif") && !temp.equals("jpg")
&& !temp.equals("png") && !temp.equals("bmp")) {
continue;
}
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon;
try {
BufferedImage bi = ImageIO.read(iconURL);
if(bi == null) continue;
icon = new ImageIcon(bi);
} catch (IOException e) {
continue;
}
if (temp.equals("bmp")) {
images[0] = icon;
continue;
}
images[j] = icon;
imageNames[j] = imageName.substring(7, imageName.lastIndexOf("."));
j++;
}
REAL_NUM_IMAGES = j - 1; // Create label for displaying moon phase images and put a border around
// it.
phaseIconLabel = new JLabel();
phaseIconLabel.setHorizontalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalTextPosition(JLabel.CENTER);
phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER);
phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 10, 0), phaseIconLabel
.getBorder())); phaseResult = new JLabel();
phaseBlank1 = new JLabel();
phaseBlank2 = new JLabel(); // Create combo box with lunar phase choices.
phaseChoice = new JButton("开始/停止"); // Display the first image.
phaseIconLabel.setIcon(images[START_INDEX]);
phaseIconLabel.setText(""); // Add border around the select panel.
selectPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Select Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); resultPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Result Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); // Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Display Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); // Add moon phases combo box to select panel and image label to
// displayPanel.
selectPanel.add(phaseChoice);
displayPanel.add(phaseIconLabel);
resultPanel.add(phaseBlank1);
resultPanel.add(phaseResult);
resultPanel.add(phaseBlank2); // Listen to events from combo box.
phaseChoice.addActionListener(this);
} boolean run = false; // Implementation of ActionListener interface.
public void actionPerformed(ActionEvent event) {
if (run) {
run = false;
phaseBlank1.setText("恭喜");
phaseBlank2.setText("中奖啦!");
} else {
run = true; new Thread() {
public void run() {
while (run) {
int a = (int) (Math.random() * REAL_NUM_IMAGES) + 1;
System.out.println(a);
phaseIconLabel.setIcon(images[a]);
phaseBlank1.setText("");
phaseResult.setText(imageNames[a]);
phaseBlank2.setText("");
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}.start();
} } // main method
public static void main(String[] args) {
// create a new instance of LunarPhases
LunarPhases phases = new LunarPhases(); // Create a frame and container for the panels.
JFrame lunarPhasesFrame = new JFrame("Lunar Phases"); // Set the look and feel.
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
} lunarPhasesFrame.setContentPane(phases.mainPanel); // Exit when the window is closed.
lunarPhasesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Show the converter.
lunarPhasesFrame.pack();
lunarPhasesFrame.setVisible(true);
}
}
Exception in thread "main" java.lang.NullPointerException
at LunarPhases.addWidgets(LunarPhases.java:67)
at LunarPhases.<init>(LunarPhases.java:46)
at LunarPhases.main(LunarPhases.java:183)
求解决。
File dir = new File("H:\\java\\bin\\images");
URL iconURL = ClassLoader.getSystemResource(imageName);
这是有冲突的, 第1行是绝对路径, 而第2行是相对路径
也就是说如果你的工程是在H:\java\下才可以运行程序
否则会因为找不到图片而报NullPointerException稍后我再改下吧
运行的时候应确保与LunarPhases.class同目录下有images目录
该目录下应有.jpg,.png或.gif的抽奖图片以及.bmp的初始图片
bin
│ LunarPhases$1.class
│ LunarPhases.class
└─images
user_01.jpg
user_02.jpg
user_03.jpg
user_04.jpg
red.bmpimport java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;// This example demonstrates the use of JButton, JTextField
// and JLabel.
public class LunarPhases implements ActionListener {
final static int NUM_IMAGES = 1000; final static int START_INDEX = 0; int REAL_NUM_IMAGES = 0; ImageIcon[] images = new ImageIcon[NUM_IMAGES]; String[] imageNames = new String[NUM_IMAGES]; JPanel mainPanel, selectPanel, displayPanel, resultPanel; JButton phaseChoice = null; JLabel phaseIconLabel = null, phaseResult = null, phaseBlank1 = null,
phaseBlank2 = null; // Constructor
public LunarPhases() {
// Create the phase selection and display panels.
selectPanel = new JPanel();
displayPanel = new JPanel();
resultPanel = new JPanel(); // Add various widgets to the sub panels.
addWidgets(); // Create the main panel to contain the two sub panels.
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1, 3, 5, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Add the select and display panels to the main panel.
mainPanel.add(selectPanel);
mainPanel.add(displayPanel);
mainPanel.add(resultPanel);
} // Create and the widgets to select and display the phases of the moon.
private void addWidgets() {
// Get the images and put them into an array of ImageIcon. // 改为相对路径
// File dir = new File("H:\\java\\bin\\images");
URL path = ClassLoader.getSystemResource("images");
if(path == null) { // 避免因找不到图片目录而出现空指针异常
StringBuffer msg = new StringBuffer("没有找到图片目录: ")
.append(new File(ClassLoader.getSystemResource("").getPath()))
.append(File.separator).append("images").append(File.separator);
System.out.println(msg);
System.exit(0);
return;
}
File dir = new File(path.getFile());
File[] files = dir.listFiles();
int j = 1;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
continue; // 目录则不处理
}
// 文件名
String fileName = files[i].getName();
// 文件名后缀(转换为小写,否则不能兼容大写后缀名)
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1)
.toLowerCase(); if (!suffix.equals("gif") && !suffix.equals("jpg")
&& !suffix.equals("png") && !suffix.equals("bmp")) {
continue;
}
ImageIcon icon;
try {
// 可直接使用files[i], 无需构造URL
// URL iconURL = ClassLoader.getSystemResource(imageName);
BufferedImage bi = ImageIO.read(files[i]);
if(bi == null) continue;
icon = new ImageIcon(bi);
} catch (IOException e) {
continue;
}
if (suffix.equals("bmp")) {
images[0] = icon;
continue;
}
images[j] = icon;
imageNames[j] = fileName.substring(0, fileName.lastIndexOf("."));
j++;
}
REAL_NUM_IMAGES = j - 1; // Create label for displaying moon phase images and put a border around
// it.
phaseIconLabel = new JLabel();
phaseIconLabel.setHorizontalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalTextPosition(JLabel.CENTER);
phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER);
phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 10, 0), phaseIconLabel
.getBorder())); phaseResult = new JLabel();
phaseBlank1 = new JLabel();
phaseBlank2 = new JLabel(); // Create combo box with lunar phase choices.
phaseChoice = new JButton("开始/停止"); // Display the first image.
phaseIconLabel.setIcon(images[START_INDEX]);
phaseIconLabel.setText(""); // Add border around the select panel.
selectPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Select Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); resultPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Result Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); // Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Display Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5))); // Add moon phases combo box to select panel and image label to
// displayPanel.
selectPanel.add(phaseChoice);
displayPanel.add(phaseIconLabel);
resultPanel.add(phaseBlank1);
resultPanel.add(phaseResult);
resultPanel.add(phaseBlank2); // Listen to events from combo box.
phaseChoice.addActionListener(this);
} boolean run = false; // Implementation of ActionListener interface.
public void actionPerformed(ActionEvent event) {
if (run) {
run = false;
phaseBlank1.setText("恭喜");
phaseBlank2.setText("中奖啦!");
} else {
run = true; new Thread() {
public void run() {
while (run) {
int a = (int) (Math.random() * REAL_NUM_IMAGES) + 1;
System.out.println(a);
phaseIconLabel.setIcon(images[a]);
phaseBlank1.setText("");
phaseResult.setText(imageNames[a]);
phaseBlank2.setText("");
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}.start();
} } // main method
public static void main(String[] args) {
// create a new instance of LunarPhases
LunarPhases phases = new LunarPhases(); // Create a frame and container for the panels.
JFrame lunarPhasesFrame = new JFrame("Lunar Phases"); // Set the look and feel.
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
} lunarPhasesFrame.setContentPane(phases.mainPanel); // Exit when the window is closed.
lunarPhasesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Show the converter.
lunarPhasesFrame.pack();
lunarPhasesFrame.setVisible(true);
}
}
我的电脑是Vista Home Basic系统,UAC开着,JDK1.6.0_20,运行成功的那台电脑是Windows7 旗舰版,JDK1.6.0_03,他的用户帐号控制级别时默认的有对计算机产生更改的动作时发出通知。求解决。