gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:46:53 26 4
gpt4 key购买 nike

您好,我在尝试使用数组读取要分配给图像图标数组的图像文件名时遇到问题。它编译得很好,但是当我启动应用程序时,图像似乎没有出现。任何人都可以看到我在这里所做的事情有问题吗?如果您觉得有必要批评该计划中的其他任何内容,我愿意接受建议。提前致谢!

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 文件。

关于java - 使用字符串数组将文件名分配给 ImageIcons (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15687874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com