Using String arrays to assign file names to ImageIcons (Java)

itachi4us

New Member
Hi i'm having problems when trying to use an array to read in image file names to be assigned to an array of Image Icons. It compiles fine but when i launch the application the images don't seems to be appearing. Can anyone see a problem with what i am doing here? If you feel the need to critique anything else in the program i'm open to suggestions. Thanks in advance!\[code\]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\[/code\]
 
Back
Top