首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正在尝试创建将显示GUI幻灯片的自执行jar文件

正在尝试创建将显示GUI幻灯片的自执行jar文件
EN

Stack Overflow用户
提问于 2015-12-15 05:47:50
回答 1查看 38关注 0票数 0

我用java写了一个程序,它使用GUI来显示图像和显示引文的幻灯片。我在mac上的终端中使用命令行。我正在尝试创建一个可自执行的jar文件,它将在其所在的任何计算机上运行此程序。我已经和我的CS教授见过很多次了,但他遇到的问题是加载包含jar文件中所有图片的文件夹。我们已经创建了manifest.mf文件和jar文件,但是它没有运行。我一直在使用this website作为参考

我看了很多关于它的东西,但问题是我对它了解不多,大多数人都是用hello world这样的简单程序来创建这些东西的。这方面的任何帮助都会非常感谢,因为我在这个程序上花了很多时间,如果我不能在其他计算机上运行它,那么它对我来说几乎是无用的。这些图像位于一个名为Pictures的文件夹中,该文件夹位于eclipse项目的bin文件夹中。

上传图像的代码如下所示

代码语言:javascript
复制
for(int i = 0; i < pic.length-1; i++)
    {
        pic[i] = new ImageIcon(this.getClass().getResource("/Pictures/" + i +".JPG")); // Fills array with pictures

    }

我的其余代码看起来像这样

代码语言:javascript
复制
@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 ");
}

}

EN

回答 1

Stack Overflow用户

发布于 2015-12-15 06:01:23

我不知道你有什么问题,因为你的描述不是很清楚。

您可能将图像作为对本地文件的引用进行传递,而不是将它们添加到Jar文件中。

如果你在你的电脑上有一个游戏的快捷方式,并且你试图将这个快捷方式复制到另一台电脑上,这是完全相同的。这个游戏不会起作用。

您需要将图像添加到您的jar包中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34277210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档