使保存按钮在java中工作

使保存按钮在java中工作,java,swing,user-interface,Java,Swing,User Interface,我需要构建一个GUI,允许用户为新的患者表单输入自己的基本详细信息,并将这些信息保存到.txt文件中。这些数据应该可以在以后查看,但我无法让“保存”按钮工作 //Import packages import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.FileNotFoundException; import java.io.PrintWriter; //主类 公共类患者详情{ /

我需要构建一个GUI,允许用户为新的患者表单输入自己的基本详细信息,并将这些信息保存到.txt文件中。这些数据应该可以在以后查看,但我无法让“保存”按钮工作

    //Import packages

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
//主类

公共类患者详情{

//Declare variables
public JFrame frame1;
public JPanel panel;
public JButton btnSave, btnExit;
public JLabel lblFirstName, lblSurname, lblGender, lblDOB, lblSmoker, lblMedHistory, lblFSlash1, lblFSlash2, lblImage;
public JTextField txtFirstName, txtSurname, txtGender,  txtSmoker, txtMedHistory, txtDOB1, txtDOB2, txtDOB3;
public Insets insets;
public JTextArea textArea = new JTextArea();



public static void main (String args[]){
    new PatientDetailsWindow();  
}

public PatientDetailsWindow(){
    createFrame();
    createLabels();
    createTextFields();
    createButtons();

}

//Create the frame
public void createFrame(){
    frame1 = new JFrame ("Personal details");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize (600,600);

    panel = new JPanel();
    panel.setBackground(Color.gray);
    panel.setBounds(0, 0, 600, 600);
    panel.setLayout(null);

    frame1.add(panel);
    frame1.setVisible(true);

}

//Creating Labels
public void createLabels(){
    lblFirstName = new JLabel ("First Name: ");
    lblFirstName.setBounds(50, 50, 500, 20);
    lblFirstName.setForeground(Color.blue);
    panel.add (lblFirstName);

    lblSurname = new JLabel ("Surname: ");
    lblSurname.setBounds(50, 100, 500, 20);
    lblSurname.setForeground(Color.blue);
    panel.add (lblSurname);

    lblGender = new JLabel ("Gender: ");
    lblGender.setBounds(50, 150, 500, 20);
    lblGender.setForeground(Color.blue);
    panel.add (lblGender);


    lblDOB = new JLabel ("Date of Birth: "); 
    lblDOB.setBounds(50, 200, 500, 20);
    lblDOB.setForeground(Color.blue);
    panel.add (lblDOB);

    lblFSlash1 = new JLabel ("/"); 
    lblFSlash1.setBounds(440, 200, 20, 20);
    lblFSlash1.setForeground(Color.blue);
    panel.add (lblFSlash1);

    lblFSlash2 = new JLabel ("/"); 
    lblFSlash2.setBounds(490, 200, 40, 20);
    lblFSlash2.setForeground(Color.blue);
    panel.add (lblFSlash2);

    lblSmoker = new JLabel ("Are you a smoker? ");
    lblSmoker.setBounds(50, 250, 500, 20);
    lblSmoker.setForeground(Color.blue);
    panel.add (lblSmoker);

    lblMedHistory = new JLabel ("Any other previous medical history? ");
    lblMedHistory.setBounds(50, 300, 500, 20);
    lblMedHistory.setForeground(Color.blue);
    panel.add (lblMedHistory);


    /*ImageIcon image= new ImageIcon("heartandstethoscope.jpg");
    JLabel lblImage = new JLabel(image);
    panel.add(lblImage);
     */
}

//Creating Text Fields
public void createTextFields(){
    txtFirstName = new JTextField (10);
    txtFirstName.setBounds(400, 50, 100, 20);
    txtFirstName.setForeground(Color.blue);
    panel.add (txtFirstName);

    txtSurname = new JTextField  (10);
    txtSurname.setBounds(400, 100, 100, 20);
    txtSurname.setForeground(Color.blue);
    panel.add (txtSurname);

    txtGender = new JTextField  (10);
    txtGender.setBounds(400, 150, 100, 20);
    txtGender.setForeground(Color.blue);
    panel.add (txtGender);

    txtDOB1 = new JTextField  (2);
    txtDOB1.setBounds(400, 200, 40, 20);
    txtDOB1.setForeground(Color.blue);
    panel.add (txtDOB1);

    txtDOB2 = new JTextField  (2);
    txtDOB2.setBounds(450, 200, 40, 20);
    txtDOB2.setForeground(Color.blue);
    panel.add (txtDOB2);

    txtDOB3 = new JTextField  (4);
    txtDOB3.setBounds(500, 200, 80, 20);
    txtDOB3.setForeground(Color.blue);
    panel.add (txtDOB3);

    txtSmoker = new JTextField (3);
    txtSmoker.setBounds(400, 250, 100, 20);
    txtSmoker.setForeground(Color.blue);
    panel.add (txtSmoker);

    txtMedHistory = new JTextField (300);
    txtMedHistory.setBounds(400, 300, 100, 60);
    txtMedHistory.setForeground(Color.blue);
    panel.add (txtMedHistory);

    JScrollPane areaScrollPane = new JScrollPane(txtMedHistory);
    areaScrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    textArea.setBounds(400, 300, 100, 80);
    JScrollPane scroll = new JScrollPane (textArea);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);


    textArea.setLineWrap(true);
    textArea.setEditable(true);
    textArea.setVisible(true);
    panel.add(textArea);
}

//Creating buttons
public void createButtons(){
    btnSave = new JButton ("Save");
    btnSave.setBounds(130, 350, 100, 20);
    btnSave.setForeground(Color.blue);
    btnSave.addActionListener(new SaveHandler());
    panel.add (btnSave);
    btnSave.setVisible(true);

    btnExit = new JButton ("Exit");
    btnExit.setBounds(240, 350, 100, 20);
    btnExit.setForeground(Color.blue);
    btnExit.addActionListener(new ExitHandler());
    panel.add (btnExit);
    btnExit.setVisible(true);
}

class ExitHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        int n = JOptionPane.showConfirmDialog(frame1, 
                "Are you sure you want to exit?", "Exit?",JOptionPane.YES_NO_OPTION);
        if(n == JOptionPane.YES_OPTION){
            System.exit(0);
            System.out.println("EXIT SUCCESSFUL");
        }
    }
}

class SaveHandler implements ActionListener{

    public void actionPerformed(ActionEvent e){
        try(  PrintWriter out = new PrintWriter( "DetailsofPatient.txt" )){
            out.println(txtFirstName.getText());
            out.println(txtGender.getText());
            out.println(txtDOB1.getText());
            out.println(txtDOB2.getText());
            out.println(txtDOB3.getText());
            out.println(txtSmoker.getText());
            out.println(txtMedHistory.getText());

        } catch (FileNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        System.out.println("Save successful");
    }
}

}

好的,对于初学者来说,您没有在“保存”按钮中添加ActionListener,所以您需要像这样添加它

 btnSave = new JButton ("Save");
    btnSave.setBounds(130, 350, 100, 20);
    btnSave.setForeground(Color.blue);
    btnSave.addActionListener(new SaveHandler());
    panel.add (btnSave);
    btnSave.setVisible(true);
另外,在
SaveHandler
中,您的
PrintWriter
实际上没有打印任何内容

您的代码:out.println()不会写任何东西

假设您想要编写txtFirstName的内容,您需要这样做

out.println(txtFirstName.getText())

编辑:在ExitHandler类的末尾还需要另一个右大括号

class ExitHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        int n = JOptionPane.showConfirmDialog(frame1, 
                "Are you sure you want to exit?", "Exit?",JOptionPane.YES_NO_OPTION);
        if(n == JOptionPane.YES_OPTION){
            System.exit(0);
            System.out.println("EXIT SUCCESSFUL");
        }
    }
}

然后,您还需要删除程序末尾的右括号。

好的,对于初学者来说,您没有将ActionListener添加到“保存”按钮中,因此需要像这样添加它

 btnSave = new JButton ("Save");
    btnSave.setBounds(130, 350, 100, 20);
    btnSave.setForeground(Color.blue);
    btnSave.addActionListener(new SaveHandler());
    panel.add (btnSave);
    btnSave.setVisible(true);
另外,在
SaveHandler
中,您的
PrintWriter
实际上没有打印任何内容

您的代码:out.println()不会写任何东西

假设您想要编写txtFirstName的内容,您需要这样做

out.println(txtFirstName.getText())

编辑:在ExitHandler类的末尾还需要另一个右大括号

class ExitHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        int n = JOptionPane.showConfirmDialog(frame1, 
                "Are you sure you want to exit?", "Exit?",JOptionPane.YES_NO_OPTION);
        if(n == JOptionPane.YES_OPTION){
            System.exit(0);
            System.out.println("EXIT SUCCESSFUL");
        }
    }
}

然后,您还需要删除程序末尾的右括号。

保存按钮有什么不起作用?例如,文件没有更改?或者抛出异常?当您说它不工作时,会发生什么?是否有错误,或者什么都没有发生,或者发生了意外情况?它只是不会将任何内容保存到文件中,没有错误消息但它不会保存?保存按钮有什么不起作用?例如,文件没有更改?或者抛出异常?当您说它不工作时,会发生什么?是否有错误,或者什么也没有发生,或者发生了意外情况?它只是不会将任何内容保存到文件中,没有错误消息但它不会保存?我正要发布。我确实尝试过,但是SaveHandler有错误消息“无法解析为类型”://Creating buttons public void createButtons(){btnSave=new JButton(“Save”);btnSave.setBounds(130350100,20);btnSave.setForeground(Color.blue);btnSave.addActionListener(new SaveHandler());panel.add(btnSave);btnSave.setVisible(true);非常感谢您的帮助!@Austin非常感谢!但我没有看到输入的信息实际保存到.txt文件中?您认为错误可能在以下部分吗?`class SaveHandler implements ActionListener{public void actionPerformed(ActionEvent e){try(PrintWriter out=new PrintWriter(“DetailsofPatient.txt”){out.println();}catch(FileNotFoundException e2){//TODO自动生成的catch块e2.printStackTrace();}System.out.println(“保存”);}`@OlleHawes您仍然没有让您的PrintWriter实际编写任何内容。请从此点重新阅读我的帖子:
假设您想编写txtFirstName的内容,您需要执行
我正要发布。我尝试了,但是SaveHandler有错误消息“无法解析为类型”://创建按钮public void createButtons(){btnSave=new JButton(“Save”);btnSave.setBounds(130350100,20);btnSave.setForeground(Color.blue);btnSave.addActionListener(new SaveHandler());panel.add(btnSave);btnSave.setVisible(true);非常感谢您的帮助!@Austin非常感谢!但我没有看到输入的信息实际保存到.txt文件中?您认为错误可能在以下部分吗?`class SaveHandler implements ActionListener{public void actionPerformed(ActionEvent e){try(PrintWriter out=new PrintWriter(“DetailsofPatient.txt”){out.println();}catch(FileNotFoundException e2){//TODO自动生成的catch块e2.printStackTrace();}System.out.println(“保存”);}`@OlleHawes您仍然没有让您的PrintWriter实际编写任何内容。从这一点上重新阅读我的帖子:
假设您想编写txtFirstName的内容,您需要执行