我在java中编写了一个使用GUI的程序,显示图像和显示引号的幻灯片。我在mac上使用终端中的命令行。我正在尝试创建一个自我执行的jar文件,它将在任何计算机上运行该程序。我遇到了我的CS教授很多时间,但他遇到的问题是加载包含jar文件中所有图片的文件夹。我们已经创建了manifest.mf文件,我们已经创建了jar文件但它没有运行。我一直在使用this website作为参考
我已经看了很多这方面的问题,但问题是我对此并不了解,大多数人用hello world这样的简单程序创建这些东西。任何有关这方面的帮助都将非常感激,因为我花了很多时间在这个程序上,如果我不能在其他计算机上运行它,那对我来说几乎没用。这些图像位于名为Pictures的文件夹中,该文件夹位于eclipse项目的bin文件夹中。
我上传图片的代码如下所示
for(int i = 0; i < pic.length-1; i++)
{
pic[i] = new ImageIcon(this.getClass().getResource("/Pictures/" + i +".JPG")); // Fills array with pictures
}
我的其余代码看起来像
@SuppressWarnings("serial")
public class AubsGUI extends JFrame
{
int size = 20;
JLabel label;
JButton prevPic, prevQuote,changeQuote;
JTextArea text, text2;
ImageIcon pic[] = new ImageIcon[size];
JPanel panel;
int count = 0;
int i = 0;
int oldIndex = 0;
static ArrayList<String> quotes = new ArrayList<String>();
public AubsGUI() // constructor
{
panel = new JPanel(); // Creates a JPanel
panel.setLayout(null);
panel.setBackground(randomColorForBackground()); // Sets the background of that panel to a random color
add(panel); // Adds panel to screen
text2 = new JTextArea("Click the image for a new one");
text2.setBounds(630,10,300,300);
panel.add(text2);
text2.setFont(new Font("FatFrank",Font.BOLD,16));
panel.setBackground(randomColorForBackground());
text2.setForeground(randomColorForFont());
text2.setOpaque(false);
text2.setEditable(false);
for(int i = 0; i < pic.length-1; i++)
{
pic[i] = new ImageIcon(this.getClass().getResource("/Pictures/" + i +".JPG")); // Fills array with pictures
}
label = new JLabel(); // Creates a JLabel
label.setIcon(picture()); // Sets picture to the label
label.setBounds(10,10,600,700);
panel.add(label); // Adds the label to the panel
changePic e = new changePic();
label.addMouseListener(e);
panel.addMouseListener(e);
prevPic = new JButton("Click for Previous Picture");
prevPic.setBounds(1000,0,185,30);
prevPic.addActionListener(new previousPic());
prevPic.setForeground(randomColorForBackground());
panel.add(prevPic);
getQuotes();
text = new JTextArea(quotes.get(getIndex()));
text.setBounds(650,100,200,200);
panel.add(text);
panel.setBackground(randomColorForBackground());
text.setFont(new Font("FatFrank",Font.BOLD,16));
text.setForeground(randomColorForFont());
text.setOpaque(false);
text.setEditable(false);
changeQuote = new JButton("Click For New Quote");
changeQuote.setBounds(1000,20,185,30);
changeQuote.addActionListener(new changeQuote());
changeQuote.setForeground(randomColorForBackground());
panel.add(changeQuote);
prevQuote = new JButton("Click for Previous Quote");
}
public ImageIcon picture()
{
if (i >= pic.length)
{
i = 0;
}
if(count > 1)
{
oldIndex = i--;
}
Random flipCoin = new Random();
i = flipCoin.nextInt(pic.length-1); // Makes i a random # between 0 & 20
ImageIcon icon = pic[i]; // Sets icon to be a random picture in the array
Image image = icon.getImage(); // Transforms icon from an ImageIcon to an Image so it can be resized
Image tempImg = image.getScaledInstance( 600, 700, java.awt.Image.SCALE_SMOOTH ) ; // Resizes the image
icon = new ImageIcon(tempImg); // Transforms the Image back to an ImageIcon
return icon; // Returns the random picture
}
public ImageIcon picAt()
{
ImageIcon icon = pic[oldIndex];
Image image = icon.getImage();
Image tempImg = image.getScaledInstance( 600, 700, java.awt.Image.SCALE_SMOOTH );
icon = new ImageIcon(tempImg);
return icon;
}
public void getQuotes()
{
URL url = getClass().getResource("Quotes/Quotes.txt");
try{
Scanner scan = new Scanner(new FileInputStream(url.getPath()));
while(scan.hasNextLine())
{
quotes.add(scan.nextLine());
}
scan.close();
}
catch(Exception ex)
{
System.out.println("File not found");
}
}
public Color randomColorForFont()
{
int r = (int)(Math.random() *256);
int g = (int)(Math.random() *256);
int b = (int)(Math.random() *256);
return (new Color(r, g, b).brighter());
}
public Color randomColorForBackground()
{
int r = (int)(Math.random() *256);
int g = (int)(Math.random() *256);
int b = (int)(Math.random() *256);
return (new Color(r, g, b).darker());
}
public int getIndex()
{
int index;
index = (int)(Math.random()*quotes.size());
return index;
}
public class changePic implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
text2.setForeground(randomColorForFont());
label.setIcon(picture());
panel.setBackground(randomColorForBackground());
count++;
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e){}
}
public class changeQuote implements ActionListener
{
public void actionPerformed(ActionEvent q)
{
text.setText(quotes.get(getIndex())); // Sets the quote in text area
text.setFont(new Font("FatFrank",Font.BOLD,16));
text.setForeground(randomColorForFont()); // Sets the fonts to white
text.setLineWrap(true); // Lets the string be broken up if it's to long
text.setWrapStyleWord(true); // Won't let text.setLineWrap(true) break up the words
text.setOpaque(false);
text.setEditable(false); // Won't let the text area be edited
changeQuote.setForeground(randomColorForBackground());
panel.setBackground(randomColorForBackground());
}
}
public class previousPic implements ActionListener
{
public void actionPerformed(ActionEvent p)
{
if(count > 0)
{
prevPic.setForeground(randomColorForBackground());
label.setIcon(picAt());
panel.setBackground(randomColorForBackground());
}
}
}
public static void main (String args [])
{
AubsGUI aubs = new AubsGUI(); // creates an object aubs from class AubsGUi
aubs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // allows window to close and program to end
aubs.setSize(new Dimension(1200, 1000));
aubs.setVisible(true); // allows you to see window while program runs
aubs.setTitle("title ");
}
}
答案 0 :(得分:0)
我不确定你的问题是什么,因为你的描述不是很清楚。
可能您将图像作为对本地文件的引用传递,而不是将它们添加到Jar文件中。
如果您的PC中有游戏的快捷方式,并且您尝试将该快捷方式复制到另一台计算机,则完全相同。游戏不起作用。
您需要将图像添加到jar包中。