使用字符串数组将文件名分配给ImageIcons (Java)



嗨,我有问题时,试图使用数组读取图像文件名被分配给图像图标的数组。它编译得很好,但当我启动应用程序的图像似乎没有出现。有人能看出我在这里做的有什么问题吗?如果你觉得有必要对节目中的其他内容提出批评,我愿意接受建议。提前感谢!

public class PokerStats extends JFrame
{
    private static final String [] names = {"First Name","Surname","Nickname",
                                            "Address1","Address2","Address3",
                                            "Nationality","Hands Played",
                                            "Hands Won","Total $ Won","Age"};
    private static final String [] imageNames = {"continue1.png","continue2.png",
                                                 "store1.png","store2.png",
                                                 "reset1.png","reset2.png",
                                                 "exit1.png","exit2.png"};

    private static final JLabel [] labels = new JLabel [names.length];
    private static final JTextField [] textFields = new JTextField [names.length];
    private static final JButton [] buttons = new JButton [4];
    private static final ImageIcon [] icons = new ImageIcon [imageNames.length];
    private static final Image img = new ImageIcon("poker.jpg").getImage();
    private static final Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    private static final Font f3= new Font("Bell MT",Font.PLAIN,16);
    private static final JButton button1 = new JButton (icons[3]);
    private static final JButton button2 = new JButton (icons[5]);
    private static final JButton button3 = new JButton(icons[6]);
    private static final JButton button4 = new JButton(icons[0]);
    public PokerStats() //creates a default constructor
    {
        setLayout
        (
            new FlowLayout()
        );
    }
    public PokerStats (String title) // single argument constructor
    {
        super(title); //calls on the superclass "JFrame" and sets the frame title
                      //as the string passed in from the main method
        JPanel background = new JPanel() //creates a JPanel called background
         {
            public void paintComponent(Graphics g) //overrides Jpanel Graphics to set a custom background
            {
               setPreferredSize(size);
               setSize(size);
               setLayout(null);
               g.drawImage(img, 0, 0, null);
            }
         };
            for(int count=0; count<names.length; count++) // loop used to create labels and textFields
             {
                //creates JLables and sets their attributes
                labels[count] = new JLabel(names[count]);
                labels[count].setVisible(false);
                labels[count].setForeground(Color.WHITE);
                labels[count].setFont(f3);
                background.add(labels[count]);
                //creates JTextFields and adds them to the panel
                textFields[count] = new JTextField("");
                textFields[count].setVisible(false);
                background.add(textFields[count]);
            }
            for(int count=0; count<imageNames.length; count++) // loop used to create labels and textFields
             {
                        //creates JLables and sets their attributes
                        icons[count] = new ImageIcon(imageNames[count]);
             }

        background.setLayout (null);

        button1.setToolTipText("Submit Details");
        button1.setRolloverIcon(icons[2]);
        button1.setVisible(false);
        button1.setBackground(Color.BLACK);
        button1.setBorder(null);
        button2.setToolTipText("Refresh Fields");
        button2.setVisible(false);
        button2.setRolloverIcon(icons[4]);
        button2.setBackground(Color.BLACK);
        button2.setBorder(null);
        button2.setOpaque(true);
        button3.setToolTipText("Exit Program");
        button3.setRolloverIcon(icons[7]);
        button3.setVisible(true);
        button3.setBorder(null);
        button3.setBackground(Color.BLACK);
        button4.setToolTipText("Continue");
        button4.setVisible(true);
        button4.setOpaque(true);
        button4.setBackground(Color.BLACK);
        button4.setBorder(null);
        button4.setRolloverIcon(icons[1]);
        button1.setBounds(540,440,95,40);
        button2.setBounds(650,440,95,40);
        button3.setBounds(955,577,59,59);
        button4.setBounds(420,490,210,70);
        labels[0].setBounds(220,240,110,20);
        labels[1].setBounds(220,280,110,20);
        labels[2].setBounds(220,320,110,20);
        labels[3].setBounds(220,360,110,20);
        labels[4].setBounds(220,400,110,20);
        labels[5].setBounds(220,440,110,20);
        labels[6].setBounds(540,240,110,20);
        labels[7].setBounds(540,280,110,20);
        labels[8].setBounds(540,320,110,20);
        labels[9].setBounds(540,360,110,20);
        labels[10].setBounds(540,400,110,20);
        textFields[0].setBounds(320,240,180,20);
        textFields[1].setBounds(320,280,180,20);
        textFields[2].setBounds(320,320,180,20);
        textFields[3].setBounds(320,360,180,20);
        textFields[4].setBounds(320,400,180,20);
        textFields[5].setBounds(320,440,180,20);
        textFields[6].setBounds(660,240,150,20);
        textFields[7].setBounds(660,280,110,20);
        textFields[8].setBounds(660,320,110,20);
        textFields[9].setBounds(660,360,110,20);
        textFields[10].setBounds(590,400,40,20);
        background.add(button1);
        background.add(button2);
        background.add(button3);
        background.add(button4);
        getContentPane().add(background);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     button1.addActionListener
     (
        new ActionListener () //anonymous inner class
        {
            public void actionPerformed (ActionEvent e)
            {
                String output;
                output = textFields[0].getText() + "   " + textFields[1].getText() + "   " + textFields[2].getText() + "   " +
                         textFields[3].getText() + "   " + textFields[4].getText() + "   " + textFields[5].getText() + "   " +
                         textFields[6].getText() + "   " + textFields[7].getText() + "   " + textFields[8].getText() + "   " +
                         textFields[9].getText() + "   " + textFields[10].getText();
                try
                 {
                      BufferedWriter out = new BufferedWriter(new FileWriter("PlayerData.txt", true));
                        out.write(output);
                        out.newLine();
                        out.close();
                        JOptionPane.showMessageDialog(null, "Your Details Have Been Stored Sucessfully");
                        resetFields();
                 }
                catch(IOException e2) // Exception Handler
                 {
                        JOptionPane.showMessageDialog(null, "There has been an error writing to file");
                 }
            }//ends ActionEvent
        } //ends ActionListener
     ); //ends anonymous inner class
    button2.addActionListener
    (
        new ActionListener() //anonymous inner class
        {
            public void actionPerformed(ActionEvent e)
            {
                resetFields(); //calls resetFields method to clear TextFields
            }
        }
     ); //ends anonymous inner class
    button3.addActionListener
     (
         new ActionListener () //anonymous inner class
         {
            public void actionPerformed (ActionEvent e)
            {
                System.exit(0);
                setVisible(false);
            }
         }
     );//ends anonymous inner class
    button4.addActionListener
     (
         new ActionListener() //anonymous inner class
         {
            public void actionPerformed(ActionEvent e)
            {
                for(int count=0; count<names.length; count++)
                 {
                            labels[count].setVisible(true);
                            textFields[count].setVisible(true);
                 }
                button1.setVisible(true);
                button2.setVisible(true);
                button4.setVisible(false);
            }
         }
     ); //ends anonymous inner class

   }//end single argument constructor

    public void resetFields () //a method used to reset the textFields to an empty state
    {
        for(int count=0; count<names.length; count++)
            {
                textFields[count].setText(""); //all textFields are cleared through the for loop
            }
    }//ends resetFields method
}//end of PokerStats class

您是否尝试使用图像的完整路径?问题很可能是您提供的单个文件名("continue1.png","continue2.png"等)不是类的可用资源。如果完整路径有效,那么您可能可以找到正确的相对路径("images/continue1.png"等)。

目前的期望是,这些图像是在相同的构建文件夹中,你的类,这是不正常的。如果您正在构建一个war文件(或其他类型的存档),而不仅仅是使用简单的java执行,请记住,图像可能不在您期望的位置,您应该检查构建的war文件。

相关内容

  • 没有找到相关文章

最新更新