Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Sample Program With GUI

Uploaded by

tempestv944
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Sample Program With GUI

Uploaded by

tempestv944
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Sample Program with GUI

Code:

public Reviewer() { //JFrame Name is Reviewer


setTitle("Student Application");//Title of my JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 559, 399);
contentPane = new JPanel();
contentPane.setBackground(Color.LIGHT_GRAY);
contentPane.setForeground(new Color(205, 133, 63));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

//Declaration of Labels with its corresponding properties


//Full Name Label
JLabel lblName = new JLabel("Full Name:");
lblName.setHorizontalAlignment(SwingConstants.RIGHT);
lblName.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblName.setBounds(10, 34, 88, 25);
contentPane.add(lblName);

//Address Label
JLabel lblAdd = new JLabel("Address:");
lblAdd.setHorizontalAlignment(SwingConstants.RIGHT);
lblAdd.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblAdd.setBounds(10, 69, 88, 25);
contentPane.add(lblAdd);

//Age Label
JLabel lblAge = new JLabel("Age:");
lblAge.setHorizontalAlignment(SwingConstants.RIGHT);
lblAge.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblAge.setBounds(10, 104, 88, 25);
contentPane.add(lblAge);

//Gender Label
JLabel lblGender = new JLabel("Gender:");
lblGender.setHorizontalAlignment(SwingConstants.RIGHT);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblGender.setBounds(10, 139, 88, 25);
contentPane.add(lblGender);

//Program Choice Label


JLabel lblProgChoice = new JLabel("Program Choice:");
lblProgChoice.setHorizontalAlignment(SwingConstants.RIGHT);
lblProgChoice.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblProgChoice.setBounds(10, 174, 149, 25);
contentPane.add(lblProgChoice);

//Declaration of Text Fields with its corresponding properties


//Name Text Field
txtName = new JTextField();
txtName.setFont(new Font("Tahoma", Font.PLAIN, 16));
txtName.setBounds(102, 34, 405, 25);
contentPane.add(txtName);
txtName.setColumns(10);

//Address Text Field


txtAddress = new JTextField();
txtAddress.setFont(new Font("Tahoma", Font.PLAIN, 16));
txtAddress.setColumns(10);
txtAddress.setBounds(102, 69, 405, 25);
contentPane.add(txtAddress);
//Age Text Field
txtAge = new JTextField();
txtAge.setFont(new Font("Tahoma", Font.PLAIN, 16));
txtAge.setColumns(10);
txtAge.setBounds(102, 104, 80, 25);
contentPane.add(txtAge);

//Declaration of Radio Button with its corresponding properties


//Male Radio Button
JRadioButton rdbMale = new JRadioButton("Male");
rdbMale.setBackground(Color.LIGHT_GRAY);
rdbMale.setFont(new Font("Tahoma", Font.PLAIN, 16));
rdbMale.setBounds(115, 141, 80, 21);
contentPane.add(rdbMale);

//Female Radio Button


JRadioButton rdbFemale = new JRadioButton("Female");
rdbFemale.setBackground(Color.LIGHT_GRAY);
rdbFemale.setFont(new Font("Tahoma", Font.PLAIN, 16));
rdbFemale.setBounds(198, 141, 103, 21);
contentPane.add(rdbFemale);

//Grouping the rdb to select only one option


ButtonGroup grpGender = new ButtonGroup();
grpGender.add(rdbFemale);
grpGender.add(rdbMale);

//1-D Array for the list of option in combo box


String availableProg [] = {"BS in Information Technology",
"BS in Computer Science",
"BS in Accountancy",
"BS in Elementary Education",
"BS in Accountancy"};

//Declaration of Combo box with its corresponding properties


//availableProg is placed inside the parentheses to store the elements to become the
cmb items
JComboBox cmbProgram = new JComboBox(availableProg);
cmbProgram.setFont(new Font("Tahoma", Font.PLAIN, 16));
cmbProgram.setBounds(169, 174, 338, 25);
contentPane.add(cmbProgram);

//Declaration of Text Area with its corresponding properties


JTextArea txtDisplay = new JTextArea();
txtDisplay.setLineWrap(true);
txtDisplay.setEditable(false); //to disable editable
txtDisplay.setFont(new Font("Monospaced", Font.PLAIN, 20));
txtDisplay.setBounds(42, 259, 470, 77);
contentPane.add(txtDisplay);

//Declaration of Button with its corresponding properties


//Buttons are placed on the last part to avoid error when we call the other components
JButton btnApply = new JButton("APPLY");

//codes to be executed once the button was clicked


btnApply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

//declaration of variables for user input


String appName, add, age, program, gender;
int appNumber; //the program will generate application number

//initializing the value of appName, add, and age from the Text Field
appName = txtName.getText();
add = txtAddress.getText();
age = txtAge.getText();

//initializing the value of gender from the selected radio button


//ternary operator is used to identify the gender
//ternary condition?True Output:False Output;
gender = (rdbMale.isSelected() == true)? "Male" : "Female";

//initializing the value of program from the selected item on the combo box
//(String) is used to convert the selected item from Object to String
program = (String)cmbProgram.getSelectedItem();

//initializing the value of appNumber from random number 1000-9999


//(int) to convert the random number from double to integer
//Math.random() function for random number
//1000 is the starting , 9999 is the ending
appNumber = ((int)(Math.random()*(1000-9999+1)+9999));

//to show message dialog for the user to confirm the information
JOptionPane.showMessageDialog(contentPane, "Confirm your input"
+ "\nName: " + appName
+ "\nAddress: " + add
+ "\nAge: " + age
+ "\nProgram Choice: " + program);

//for example BSIT and BSCS exceed the number of applicants


//conditional statement is used
//getSelectedIndex will return the selected item inside the cmb
//it will also represent the items(array elements)
if(cmbProgram.getSelectedIndex() == 0) {//BSIT
//will display No Slot Available on the text area
txtDisplay.setText("No Slot Available");
}
else if(cmbProgram.getSelectedIndex() == 1) {//BSCS
txtDisplay.setText("No Slot Available");
}
else{
//will display the ff. on the text area
//2024 + appNumber will return 2024 then the random generated number
txtDisplay.setText("Application Successful! \nApplicant Number: " + 2024 +
appNumber);
}
}
});
btnApply.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnApply.setBounds(221, 220, 115, 29);
contentPane.add(btnApply);
}

Sample Output:

You might also like