0% found this document useful (0 votes)
21 views5 pages

File Gui

This document contains the code for a GUI application created in Java that allows users to input member details such as name, smoking status, and staff status. It displays the input fields and buttons in different panels within the GUI frame. When the save button is clicked, it saves the member object containing the input details to a file. The clearFields method resets the input fields after saving.

Uploaded by

Hether Stone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

File Gui

This document contains the code for a GUI application created in Java that allows users to input member details such as name, smoking status, and staff status. It displays the input fields and buttons in different panels within the GUI frame. When the save button is clicked, it saves the member object containing the input details to a file. The clearFields method resets the input fields after saving.

Uploaded by

Hether Stone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package za.ac.cput.assignment6;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
* this gui is to show members of cput whom smoke and don't and also to check if
they are staff members
or not.
* @author LuphiweSikupela 216060133
*/
public class fileGui extends JFrame implements ActionListener {

private String title;


private String firstname;
private String lastname;
private boolean smoker;
private boolean nonsmoker;
private boolean staff;

private FileWriter fw;


private FileReader fr;

private JPanel panelNorth;


private JPanel panelCenter, panelSmoker;
private JPanel panelSouth;

private JLabel lblLogo;


private JLabel lblHeading;
private JLabel lblPadding0, lblPadding01, lblPadding1, lblPadding2,
lblPadding3, lblPadding4, lblPadding5;

private JLabel lblRecordCount;

private JLabel lblTitle;


private JComboBox cboTitle;

private JLabel lblFirstName;


private JTextField txtFirstName;

private JLabel lblLastName;


private JTextField txtLastName;
private JLabel lblErrorLastName;

private JLabel lblSmoker;


private JRadioButton radNonSmoking;
private JRadioButton radSmoking;
private ButtonGroup smokerGroup;

private JLabel lblStaff;


private JCheckBox chkStaff;

private JButton btnSave;


private JButton btnExit;

private Font ft1, ft2, ft3, ft4;


private Members members;

public fileGui() {

super("Assigment6-Staff and Nonstaff Smokers/Non Smokers");

panelNorth = new JPanel();


panelCenter = new JPanel();
panelSmoker = new JPanel();
panelSouth = new JPanel();

lblLogo = new JLabel();


ImageIcon imageIcon = new ImageIcon(new
ImageIcon("smoking").getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT));
lblLogo.setIcon(imageIcon);
lblHeading = new JLabel("Members Of CPUT");

lblTitle = new JLabel("Title: ");


cboTitle = new JComboBox(new String[]{"Adv", "Dr", "Miss", "Mr", "Mrs",
"Prof"});
lblFirstName = new JLabel("First Name: ");
txtFirstName = new JTextField();
lblLastName = new JLabel("Last Name: ");
txtLastName = new JTextField();

lblSmoker = new JLabel("Smoker: ");


radNonSmoking = new JRadioButton("Non-smoking");
radSmoking = new JRadioButton("Smoking");
smokerGroup = new ButtonGroup();

lblStaff = new JLabel("Staff member: ");


chkStaff = new JCheckBox();

lblPadding0 = new JLabel("");


lblPadding01 = new JLabel("");
lblPadding1 = new JLabel("");
lblPadding2 = new JLabel("");
lblPadding3 = new JLabel("");
lblPadding4 = new JLabel("");
lblPadding5 = new JLabel("");

btnSave = new JButton("Save to file");


btnExit = new JButton("Exit");

ft1 = new Font("Arial", Font.BOLD, 32);


ft2 = new Font("Arial", Font.PLAIN, 22);
ft3 = new Font("Arial", Font.PLAIN, 24);
ft4 = new Font("Arial", Font.ITALIC, 16);

public void setGUI() {

// Setting all gui components on the frame


panelNorth.setLayout(new FlowLayout());
panelCenter.setLayout(new GridLayout(7, 3));
panelSmoker.setLayout(new GridLayout(1, 2));
panelSouth.setLayout(new GridLayout(1, 3));

panelNorth.add(lblLogo);
panelNorth.add(lblHeading);
lblHeading.setFont(ft1);
lblHeading.setForeground(Color.white);
panelNorth.setBackground(new Color(0, 106, 255));

lblTitle.setFont(ft2);
lblTitle.setHorizontalAlignment(JLabel.RIGHT);
cboTitle.setFont(ft2);
panelCenter.add(lblTitle);
panelCenter.add(cboTitle);
panelCenter.add(lblPadding1);

lblFirstName.setFont(ft2);
lblFirstName.setHorizontalAlignment(JLabel.RIGHT);
txtFirstName.setFont(ft2);
panelCenter.add(lblFirstName);
panelCenter.add(txtFirstName);
panelCenter.add(lblPadding2);

lblLastName.setFont(ft2);
lblLastName.setHorizontalAlignment(JLabel.RIGHT);
txtLastName.setFont(ft2);
panelCenter.add(lblLastName);
panelCenter.add(txtLastName);
panelCenter.add(lblPadding3);

lblSmoker.setFont(ft2);
lblSmoker.setHorizontalAlignment(JLabel.RIGHT);
radNonSmoking.setFont(ft2);
radNonSmoking.setHorizontalAlignment(JRadioButton.CENTER);
radNonSmoking.setBackground(new Color(12, 145, 255));
radSmoking.setFont(ft2);
radSmoking.setHorizontalAlignment(JRadioButton.LEFT);
radSmoking.setBackground(new Color(12, 145, 255));

smokerGroup.add(radSmoking);
smokerGroup.add(radNonSmoking);
panelCenter.add(lblSmoker);
panelSmoker.add(radSmoking);
panelSmoker.add(radNonSmoking);
panelSmoker.setBackground(new Color(36, 145, 255));
panelCenter.add(panelSmoker);
panelCenter.add(lblPadding4);

lblStaff.setFont(ft2);
lblStaff.setHorizontalAlignment(JLabel.RIGHT);
chkStaff.setFont(ft2);
chkStaff.setHorizontalAlignment(JRadioButton.LEFT);
chkStaff.setBackground(new Color(36, 145, 255));

panelCenter.add(lblStaff);
panelCenter.add(chkStaff);
panelCenter.add(lblPadding5);
panelCenter.setBackground(new Color(36, 145, 255));

btnSave.setFont(ft3);
btnExit.setFont(ft3);

panelSouth.add(btnSave);
panelSouth.add(btnExit);

this.add(panelNorth, BorderLayout.NORTH);
this.add(panelCenter, BorderLayout.CENTER);
this.add(panelSouth, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

btnSave.addActionListener(this);
btnExit.addActionListener(this);

// this.setBackground(Color.yellow);
this.setSize(600, 600);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);

public void clearFields() {


txtFirstName.setText("");
txtLastName.setText("");
smokerGroup.clearSelection();
chkStaff.setSelected(false);

@Override
public void actionPerformed(ActionEvent ae) {

if (ae.getActionCommand().equals(btnSave.getActionCommand())) {

members = new Members();


members.setTitle((String) cboTitle.getSelectedItem());
members.setFirstName(txtFirstName.getText());

members.setLastName(txtLastName.getText());
if (radSmoking.isSelected()) {
members.setSmoker(true);
} else if (radNonSmoking.isSelected()) {
members.setSmoker(false);
}
if (chkStaff.isSelected()) {
members.setStaff(true);
{
members.setStaff(false);

members.writeToFile();
clearFields();

JOptionPane.showMessageDialog(null, "File written successfully!");

if (ae.getActionCommand().equals(btnExit.getActionCommand())) {

System.exit(0);
}
}

public static void main(String[] args) {

new fileGui().setGUI();

// TODO code application logic here


}
}

You might also like