Java:ImageIcon“value”的If语句
我有一个带有文本字段、图像标签和按钮的小程序。当我按下按钮时,它显示一个标准图像 但是,如果textfield的输入等于某个字符串,我想更改图片,例如car 当我使用它时,我得到一个错误,我已经将“foto”定义为一个变量:Java:ImageIcon“value”的If语句,java,image,netbeans,imageicon,Java,Image,Netbeans,Imageicon,我有一个带有文本字段、图像标签和按钮的小程序。当我按下按钮时,它显示一个标准图像 但是,如果textfield的输入等于某个字符串,我想更改图片,例如car 当我使用它时,我得到一个错误,我已经将“foto”定义为一个变量: if ("car".equals(input)) { ImageIcon foto = ImageIcon("C:......png"); 这是代码中最重要的部分: private void buttonActionPerformed(java.awt.even
if ("car".equals(input)) {
ImageIcon foto = ImageIcon("C:......png");
这是代码中最重要的部分:
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
String input = textfield.getText();
ImageIcon foto = new ImageIcon("C:.......png");
label.setIcon(foto);
这是完整的代码:
主要类别:
package test4;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test4 extends JFrame
{
public static void main(String[] args) {
test5 frame = new test5();
frame.setVisible(true);
}
}
摇摆类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test4;
import javax.swing.ImageIcon;
/**
*
* @author
*/
public class test5 extends javax.swing.JFrame {
/**
* Creates new form test5
*/
public test5() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
textfield = new javax.swing.JTextField();
label = new javax.swing.JLabel();
button = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
button.setText("Show");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(121, 121, 121)
.addComponent(textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(100, 100, 100)
.addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(147, 147, 147)
.addComponent(button)))
.addContainerGap(124, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(button)
.addContainerGap(63, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
String input = textfield.getText();
ImageIcon foto = new ImageIcon("C:\\Users\\Jordii\\Pictures\\music.png");
label.setIcon(foto);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(test5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(test5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(test5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(test5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test5().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton button;
private javax.swing.JLabel label;
private javax.swing.JTextField textfield;
// End of variables declaration
}
所以我的问题是:根据文本字段的输入,是否有其他方式显示图片
例如:如果输入为“car”,则显示汽车的图片。如果输入为“house”,则显示房屋的图片。否则,显示自行车的图片。看,你的代码不是很漂亮。因此,要在代码中进行操作,您需要了解它的功能 除此之外,请回答您的问题: 如果你在说。。。已经定义了,那么。。。已定义。 就这么简单。 此示例将向您展示其工作原理:
package test.project.main;
public class Main {
public static void main(String[] args) {
int a = 10;
String a = "10";
}
}
如果您试图运行上述代码,编译器将警告您,并说您已经定义了一个名为a的变量。如果仍继续运行代码,将引发运行时异常:
主线程java.lang中出现异常。错误:未解决的编译问题:
在test.project.main.main.mainMain.java中复制局部变量a:7
现在回到您的foto示例
在这里,您可以看到foto已经定义,编译器将警告您这一点。如果您运行代码,并且如果If语句为true,因为输入等于“car”,那么您将得到一个运行时异常,请注意:如果您有编译器错误,您可能会在运行时得到一条未解析的编译错误消息
您可以通过在相同的方法中避免重复的名称来修复它,如foto和foto1等。
更好的方法是使一个变量foto,并在运行时设置它。下面的示例将解释其工作原理:
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
String input = textfield.getText();
ImageIcon foto;
if ("car".equals(input))
foto = new ImageIcon("C:......png");
else if ("plane".equals(input))
foto = new ImageIcon("C:......png");
else if ("bike".equals(input))
foto = new ImageIcon("C:......png");
else
foto = new ImageIcon("C:......png");
label.setIcon(foto);
}
关于错误消息,您不了解什么?你知道什么是声明吗;检查你的代码,看看你是否没有用相同的方法使用FOTOI我理解错误信息,但我的问题是是否有其他方法“更改”图片,具体取决于文本字段中的输入。因此,如果输入为“car”,则会出现一张汽车图片。或者,如果输入为“house”,则会显示一个房子的图片。在给出输入之前,标签可能为空,以便按下按钮后显示图片。我知道由于错误,这种方法不起作用,但我想知道是否还有其他方法可以起作用。
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
String input = textfield.getText();
ImageIcon foto;
if ("car".equals(input))
foto = new ImageIcon("C:......png");
else if ("plane".equals(input))
foto = new ImageIcon("C:......png");
else if ("bike".equals(input))
foto = new ImageIcon("C:......png");
else
foto = new ImageIcon("C:......png");
label.setIcon(foto);
}