我是新手,请耐心等待
我想要什么
我有一个JPanel,上面有一个JButton,一个JLabel和一个JTextArea。按下 JButton 后,必须在 JLabel 中打印图像(以及 JTextArea 中的一些文本)。这个特定的图像(和文本)是由if-else语句决定的。if-else 的条件基于整数变量 R。
基本上,这是一个类似于我试图做的问答式调查。我使用 R 记录用户的答案。当用户单击某个选项时,R 的值将更新。
它对文本工作正常,但对图像无效。
对于文本,我使用字符串变量您的手机。如果最后 R 的值是 eg 120,那么你的手机会更新为一个字符串,例如。Xperia Z.
我想要一个类似的变量,我可以用于图像,以便在用户单击JButton时显示Xperia Z的图片。
R 的总值用于 if-else 语句。
结构
我启动这样的变量
int R=0;
String yourphone;
ImageIcon imageresult;
我的JPanel,旨在显示结果
final JPanel result = new JPanel();
result.setBackground(Color.BLACK);
getContentPane().add(result, "name_17130054294139");
result.setLayout(null);
final JTextArea txtrphoneresult = new JTextArea();
txtrphoneresult.setBackground(Color.BLACK);
txtrphoneresult.setForeground(Color.YELLOW);
txtrphoneresult.setFont(new Font("Tahoma", Font.PLAIN, 14));
txtrphoneresult.setBounds(448, 515, 469, 121);
result.add(txtrphoneresult);
JLabel resultlabel = new JLabel(imageresult);
resultlabel.setBounds(292, 122, 782, 346);
result.add(resultlabel);
JButton btnShowResult = new JButton("Show Result");
btnShowResult.setFont(new Font("Tahoma", Font.PLAIN, 10));
btnShowResult.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(R==1726)
{
yourphone = "Samsung Galaxy S4rnHTC OnernSony Xperia Z";
ImageIcon imageresult = new ImageIcon("galaxy_one_XperiaZ.jpg");
}
else if(R==5002)
{
yourphone = "Sony Xperia Z1rnSamsung Galaxy Note 3";
ImageIcon imageresult = new ImageIcon("Note3_sonyZ1.jpg");
}
else
{
yourphone = "No Results";
}
txtrphoneresult.setText(yourphone);
}
});
btnShowResult.setBounds(618, 48, 130, 32);
result.add(btnShowResult);
问题
图像根本不显示。如果有任何其他可能的方法可以实现这一点,请指导。
首先,您在if-else
中创建imageresult
,它并非对所有方法都可见。而且您不会将图像添加到JLabel
.
将结果标签设为 clas 成员或final
变量。以以下方式更改代码:
public void actionPerformed(ActionEvent e) {
ImageIcon imageresult = null;
if(R==1726)
{
yourphone = "Samsung Galaxy S4rnHTC OnernSony Xperia Z";
imageresult = new ImageIcon("galaxy_one_XperiaZ.jpg");
}
else if(R==5002)
{
yourphone = "Sony Xperia Z1rnSamsung Galaxy Note 3";
imageresult = new ImageIcon("Note3_sonyZ1.jpg");
}
else
{
yourphone = "No Results";
}
resultlabel.setIcon(imageresult)
txtrphoneresult.setText(yourphone);
}
if
语句之前声明 ImageIcon imageresult 并调用
resultlabel.setIcon(imageresult)
txtrphoneresult.setText(yourphone);
后
顺便说一句:不要使用 null layout/setBounds()。阅读有关布局的信息并选择合适的布局。
设置标签的图标。你误会了这条线
resultlabel.setIcon(imageresult);
最终resultLabel
并执行更改操作:
public void actionPerformed(ActionEvent e)
{
ImageIcon imageresult = null;
if(R == 1726)
{
yourphone = "Samsung Galaxy S4rnHTC OnernSony Xperia Z";
imageresult = new ImageIcon("galaxy_one_XperiaZ.jpg");
}
else if(R == 5002)
{
yourphone = "Sony Xperia Z1rnSamsung Galaxy Note 3";
imageresult = new ImageIcon("Note3_sonyZ1.jpg");
}
else
{
yourphone = "No Results";
}
txtrphoneresult.setText(yourphone);
resultlabel.setIcon(imageresult);
}
不会显示,因为您没有将其"提供"给您JLabel
。你可以通过使用 setIcon() 方法来完成。
变量声明也有问题。与PHP和其他一些编程语言不同,在Java中,变量仅存在于声明{}
括号(和子括号)中(尽管有一些例外)。例如:
public void myMethod() {
float b = 5f;
if (true) {
int a = 6;
if (true) {
// a exists, you can do whatever you like with it
// b exists, you can do whatever you like with it
}
// a exists you can do whatever you like with it
// b exists, you can do whatever you like with it
} // a is "destroyed"
// a doesn't exists
// b still exists
} // b is "destroyed"
// Neither a or b exists
您的变量imageresult
声明的情况完全相同。