我正在尝试创建货币转换程序,但输出无法通过JLabel工作。我有这个程序,所以有6个JCheckbox,每个代表一个不同的货币。当用户按下“转换”按钮时,任何选定的JCheckbox都会将其特定方法的结果(在不同的类中,我认为不应该发布)输出到JLabel中。出于某种原因,我有一个“JLabel类型中的方法setText(String)不适用于参数(JCheckbox)。”这是我的GUI类:
import java.awt.EventQueue;
public class ConverterGUI {
private JFrame frame;
public static JTextField textField;
public static JLabel label1, label2, label3, label4, label5, label6;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ConverterGUI window = new ConverterGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ConverterGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
final JCheckBox euros = new JCheckBox("Euros");
euros.setBounds(50, 50, 97, 23);
frame.getContentPane().add(euros);
final JCheckBox pounds = new JCheckBox("Pounds");
pounds.setBounds(50, 76, 97, 23);
frame.getContentPane().add(pounds);
final JCheckBox pesos = new JCheckBox("Mexican Pesos");
pesos.setBounds(50, 102, 97, 23);
frame.getContentPane().add(pesos);
final JCheckBox canadians = new JCheckBox("Canadian Dollar");
canadians.setBounds(50, 128, 107, 23);
frame.getContentPane().add(canadians);
final JCheckBox yen = new JCheckBox("Yen (Japan)");
yen.setBounds(50, 154, 97, 23);
frame.getContentPane().add(yen);
final JCheckBox yuan = new JCheckBox("Yuan (China)");
yuan.setBounds(50, 180, 97, 23);
frame.getContentPane().add(yuan);
JLabel lblInputAmericanDollars = new JLabel("Input American Dollars:");
lblInputAmericanDollars.setBounds(116, 11, 117, 14);
frame.getContentPane().add(lblInputAmericanDollars);
JLabel lblSelectCurrencies = new JLabel("Select currencies:");
lblSelectCurrencies.setBounds(40, 29, 107, 14);
frame.getContentPane().add(lblSelectCurrencies);
textField = new JTextField();
textField.setBounds(231, 8, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
final JLabel label1 = new JLabel("");
label1.setBounds(163, 54, 46, 14);
frame.getContentPane().add(label1);
final JLabel label2 = new JLabel("");
label2.setBounds(163, 80, 46, 14);
frame.getContentPane().add(label2);
final JLabel label3 = new JLabel("");
label3.setBounds(163, 106, 46, 14);
frame.getContentPane().add(label3);
final JLabel label4 = new JLabel("");
label4.setBounds(163, 132, 46, 14);
frame.getContentPane().add(label4);
final JLabel label5 = new JLabel("");
label5.setBounds(163, 158, 46, 14);
frame.getContentPane().add(label5);
final JLabel label6 = new JLabel("");
label6.setBounds(163, 184, 46, 14);
frame.getContentPane().add(label6);
JButton btnConvert = new JButton("Convert");
btnConvert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(euros.isSelected() == true){
Converter.euros();
label1.setText(euros);
}
if(pounds.isSelected() == true){
Converter.pounds();
label2.setText(pounds);
}
if(pesos.isSelected() == true){
Converter.pesos();
label3.setText(pesos);
}
if(canadians.isSelected() == true){
Converter.canadians();
label4.setText(canadians);
}
if(yen.isSelected() == true){
Converter.yen();
label5.setText(yen);
}
if(yuan.isSelected() == true){
Converter.yuan();
label6.setText(yuan);
}
}
});
btnConvert.setBounds(170, 228, 89, 23);
frame.getContentPane().add(btnConvert);
}
}
答案 0 :(得分:0)
嗯,当然不是。 setText将String作为参数,而不是JCheckBox,这是您尝试传递的。
试试这个:
if(yuan.isSelected() == true){
Converter.yuan();
label6.setText("yuan");
}
更好,将是:
if(yuan.isSelected()){
Converter.yuan();
label6.setText("yuan");
}
'== true'不是必需的,因为isSelected()本身已经返回一个布尔表达式。
答案 1 :(得分:0)
您应该从Converter Class的相关方法返回String然后 使用像这样的代码
if(yuan.isSelected() == true){
label6.setText(Converter.yuan());
}