问题描述
我正在尝试将我的文本从 JTextfields 输入到 Email_GUI 类中的对象中。然后它将被传递到我的 User 类中的构造函数中。我遇到的问题是,当我在文本字段中输入信息时,会传递 Null 或除空格之外的任何内容。
公共类 Email_GUI 扩展 JFrame {
// Fields
// Labels
private JLabel FNameL = new JLabel ("First Name:");
private JLabel LNameL = new JLabel ("Last Name:");
private JLabel DepL = new JLabel ("Department:");
private JLabel PassL = new JLabel ("Password (at least 8-10 characters):");
private JLabel CenterB = new JLabel("Make Selection",SwingConstants.CENTER);
private JLabel Idlabel = new JLabel ("ID");
//Jbutton
private JButton CreateB = new JButton ("Create");
private JButton CreateUB = new JButton ("Create User");
private JButton ViewUB = new JButton ("Search Account");
private JButton CancelB = new JButton ("Cancel");
//JTextFIeld
private JTextField FNameTF = new JTextField (20);
private JTextField LNameTF = new JTextField (20);
private JTextField DepTF = new JTextField (20);
private JTextField PasstF = new JTextField (20);
private JTextField IDTF = new JTextField (20);
private String FirstName = FNameTF.getText().trim();
private String LastName = LNameTF.getText().trim();
private String Department = DepTF.getText().trim();
private String Password = PasstF.getText().trim();
private User Userinformation = new User (FirstName,LastName,Department,Password);
// GUI
public Email_GUI () {
super("Email Account");
this.setSize(700,350);
this.setLocationRelativeto(null);
this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
setVisible(true);
Container mainContainer = this.getContentPane();
mainContainer.setBackground(Color.LIGHT_GRAY);
mainContainer.setLayout(new BorderLayout(8,6));
this.getRootPane().setBorder(BorderFactory.createMatteBorder(4,4,Color.LIGHT_GRAY));
MatteBorder Border = BorderFactory.createMatteBorder(1,1,Color.black);
TitledBorder TBorder = BorderFactory.createTitledBorder(Border,"Navigation Menu");
Border EBorder = BorderFactory.createEmptyBorder(6,6,6);
CompoundBorder CBorder = BorderFactory.createCompoundBorder(TBorder,EBorder);
//Panel Left
JPanel LeftPanel = new JPanel();
LeftPanel.setPreferredSize(new Dimension(150,150));
LeftPanel.setBorder(CBorder);
LeftPanel.add(Box.createRigidArea(new Dimension(3,0)));
LeftPanel.setLayout(new BoxLayout(LeftPanel,BoxLayout.PAGE_AXIS));
LeftPanel.setBackground(Color.WHITE);
LeftPanel.add(CreateUB);
CreateUB.setMaximumSize(new Dimension(200,35));
CreateUB.setFocusable(false);
LeftPanel.add(Box.createVerticalStrut(5));
LeftPanel.add(ViewUB);
ViewUB.setMaximumSize(new Dimension(350,35));
ViewUB.setFocusable(false);
mainContainer.add(LeftPanel,BorderLayout.WEST);
//Panel Middle
CenterB.setopaque(true);
CenterB.setBorder(new LineBorder(Color.BLACK,1));
mainContainer.add(CenterB);
CenterB.setBackground(Color.LIGHT_GRAY);
CenterB.setVisible(true);
// Listeners and events for processesing a request for the GUI
CreateUB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
CreateUB.setEnabled(false);
CenterB.setVisible(false);
Border LBorder = BorderFactory.createLineBorder(Color.BLACK,1);
Border EBorder = BorderFactory.createEmptyBorder(30,30,30);
CompoundBorder CBorder = BorderFactory.createCompoundBorder(LBorder,EBorder);
JPanel MiddlePanel = new JPanel();
MiddlePanel.setBorder(CBorder);
MiddlePanel.setLayout(new GridLayout(8,2,3,3));
MiddlePanel.setBackground(Color.WHITE);
MiddlePanel.setVisible(true);
MiddlePanel.add(FNameL);
MiddlePanel.add(FNameTF);
FNameTF.setMaximumSize(new Dimension(100,20));
MiddlePanel.add(LNameL);
MiddlePanel.add(LNameTF);
LNameTF.setMaximumSize(new Dimension(100,20));
MiddlePanel.add(DepL);
MiddlePanel.add(DepTF);
DepTF.setMaximumSize(new Dimension(100,20));
MiddlePanel.add(PassL);
MiddlePanel.add(PasstF);
PasstF.setMaximumSize(new Dimension(100,20));
MiddlePanel.add(Box.createHorizontalBox());
MiddlePanel.add(Box.createHorizontalBox());
MiddlePanel.add(CreateB);
MiddlePanel.add(CancelB);
MiddlePanel.add(Box.createVerticalStrut(60));
mainContainer.add(MiddlePanel,BorderLayout.CENTER);
CancelB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
CreateUB.setEnabled(true);
MiddlePanel.setVisible(false);
CenterB.setVisible(true);
}
});
CreateB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
if(Userinformation.UserRecord.containsValue(Userinformation)){
JOptionPane.showMessageDialog(null,"User alread exists ","Warning",JOptionPane.PLAIN_MESSAGE );
}
else{
Userinformation.StoreUserInfo();
JOptionPane.showMessageDialog(null,"User added! ","",JOptionPane.PLAIN_MESSAGE );
}
}
catch(NumberFormatException ex){
}
}
});
}
});
公共类用户{
//Fields
private String FirstName;
private String LastName;
private String Department;
private String Password;
private int UserID;
private static final AtomicInteger IDCount = new AtomicInteger(0);
//Constructor that processe user information
public User (String FName,String LName,String Depart,String Pass){
this.FirstName = FName;
this.LastName = LName;
this.Department = Depart;
this.Password = Pass;
}
public HashMap<Integer,String> UserRecord = new HashMap<Integer,String>();
public void setFirstName(String FirstName){
this.FirstName = FirstName;
}
public void setLastName(String LastName){
this.LastName = LastName;
}
public void setDepartment(String Department){
this.Department = Department;
}
public void setPassword(String Password){
this.Password = Password;
}
// get methods
public String getFirstName(){
return FirstName;
}
public String getLastName(){
return LastName;
}
public String getDepartment(){
return Department;
}
public String getpassword(){
return Password;
}
public void StoreUserInfo (){
UserID = IDCount.incrementAndGet();
this.UserRecord.put(UserID,getFirstName()+getLastName()+getDepartment()+getpassword());
System.out.println(Arrays.asList(UserRecord));
}
}
CLick Here for Output of Code Screenshot
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)