使保存按钮在java中工作

时间:2016-04-27 19:16:14

标签: 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;

//主要课程

公共类PatientDetailsWindow {

//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");
    }
}

}

1 个答案:

答案 0 :(得分:4)

对于初学者来说,你没有在你的保存按钮中添加一个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");
        }
    }
}

然后您还需要在程序结束时删除右括号。