首页上一页 1 下一页尾页 1 条记录 1/1页
Java抽奖软件,图片不能显示
发表在Java图书答疑
2014-12-30
是否精华
是
否
版块置顶:
是
否
以下是源代码:
import java.awt.BorderLayout;
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 = 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;
// 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:\\images");
File[] files = dir.listFiles();
String imageName = null;
String temp = null;
int j = 0;
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("gpeg"))
{
continue;
}
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
images[j] = icon;
imageNames[j] = imageName.substring(7,imageName.lastIndexOf("."));
j++;
}
REAL_NUM_IMAGES = j;
// 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();
// 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("开始/停止"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
resultPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("结果"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
// Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("图片显示"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
// Add moon phases combo box to select panel and image label to
// displayPanel.
selectPanel.setLayout(new GridLayout(3,3));
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(phaseChoice);
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
resultPanel.setLayout(new BorderLayout());
displayPanel.add(phaseIconLabel);
resultPanel.add(phaseResult);
// 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;
}
else{
run = true;
new Thread(){
public void run(){
while(run){
int a =(int)( Math.random()*REAL_NUM_IMAGES);
phaseIconLabel.setIcon(images[a]);
phaseResult.setText(imageNames[a]);
try{
Thread.sleep(100);
}
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("摇号抽奖");
// 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.BorderLayout;
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 = 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;
// 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:\\images");
File[] files = dir.listFiles();
String imageName = null;
String temp = null;
int j = 0;
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("gpeg"))
{
continue;
}
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
images[j] = icon;
imageNames[j] = imageName.substring(7,imageName.lastIndexOf("."));
j++;
}
REAL_NUM_IMAGES = j;
// 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();
// 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("开始/停止"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
resultPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("结果"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
// Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("图片显示"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
// Add moon phases combo box to select panel and image label to
// displayPanel.
selectPanel.setLayout(new GridLayout(3,3));
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(phaseChoice);
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
resultPanel.setLayout(new BorderLayout());
displayPanel.add(phaseIconLabel);
resultPanel.add(phaseResult);
// 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;
}
else{
run = true;
new Thread(){
public void run(){
while(run){
int a =(int)( Math.random()*REAL_NUM_IMAGES);
phaseIconLabel.setIcon(images[a]);
phaseResult.setText(imageNames[a]);
try{
Thread.sleep(100);
}
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("摇号抽奖");
// 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);
}
}