在 JOptionPane 中显示输出时出错



我是Java新手,如果我尝试在JOptionPane中显示我的输出,我会收到错误,请帮助。 我收到一个错误,说

javax.swing.JTextField[,217,117,150x20,layout = javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX= 0.0,align Y = 0.0,border =javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@fb7f7e,flags=296,maximumSize=,minimumSize= 某事。

我只是从某个地方引用了代码,因为我们的格式相同,而且我无法显示输出。


这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Text1 {
static Container contain;
static JFrame f;
static JPanel p;
static JLabel l,lblName,lblEmail,lblMovie,lblRole,lblSalary,lblGross;
static JTextField fName,fEmail,fMovie,fRole,fSalary,fGross;
static JButton submit;
Handler h = new Handler();
public static void main(String[] args) {
new Text1();                
}
public Text1(){
f = new JFrame("Movies");
f.setLayout(null);
contain = f.getContentPane();
p = new JPanel();
p.setLayout(null);
p.setBounds(0,0,700,700);
contain.add(p);
l = new JLabel("<html>Write a program to accept the name, email address, latest movie, role, salary, box office gross. Display all the inputs and the bonus and total income.The total income came from the salary and the bonus which is the 10% of the box office gross.</html>");
l.setBounds(10,5,670,100);
p.add(l);
//--Name
lblName = new JLabel("Name: ");
lblName.setBounds(78,52,89,150);
p.add(lblName);
//--Email
lblEmail = new JLabel("Email: ");
lblEmail.setBounds(78,84,89,150);
p.add(lblEmail);
//--Movie
lblMovie = new JLabel("Latest Movie: ");
lblMovie.setBounds(78,116,130,150);
p.add(lblMovie);
//--Role
lblRole = new JLabel("Role: ");
lblRole.setBounds(78,148,89,150);
p.add(lblRole);
//--Salary
lblSalary = new JLabel("Salary: ");
lblSalary.setBounds(78,180,89,150);
p.add(lblSalary);
//--Gross
lblGross = new JLabel("Gross: ");
lblGross.setBounds(78,212,89,150);
p.add(lblGross);
//TextFields :3
fName = new JTextField();
fName.setBounds(217,117,150,20);
p.add(fName);
fEmail = new JTextField();
fEmail.setBounds(217,147,150,20);
p.add(fEmail);
fMovie = new JTextField();
fMovie.setBounds(217,180,150,20);
p.add(fMovie);
fRole = new JTextField();
fRole.setBounds(217,213,150,20);
p.add(fRole);
fSalary = new JTextField();
fSalary.setBounds(217,244,150,20);
p.add(fSalary);
fGross = new JTextField();
fGross.setBounds(217,275,150,20);
p.add(fGross);
//Buttonerino
submit = new JButton("Submit");
submit.setBounds(78,310,80,20);
submit.addActionListener(h);
p.add(submit);

f.setSize(710,710);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent e){
String name = fName.getText();
String email = fEmail.getText();
String movie = fMovie.getText();
String role = fRole.getText();
String salary = fSalary.getText();
String gross = fGross.getText();
JOptionPane.showMessageDialog(null,"Name: " + fName +"n Email: " +fEmail);
}   
}
}

fName更改为name,将fEmail更改为email

JOptionPane.showMessageDialog(null, "Name: " + name + "n Email: " + email);

你应该使用name而不是fName,使用email而不是fEmail。

private class Handler implements ActionListener{
public void actionPerformed(ActionEvent e){
String name = fName.getText();
String email = fEmail.getText();
JOptionPane.showMessageDialog(null,"Name: " + name +"n Email: " +email);
}   
}

相关内容

最新更新