Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Samson_Form extends JFrame implements ActionListener {
- private Container con = getContentPane();
- FlowLayout fl = new FlowLayout();
- Font bFont = new Font("Times New Roman", Font.BOLD, 20);
- JLabel lb1 = new JLabel("Enter Your Fullname: ");
- JTextField tf1 = new JTextField(18);
- JLabel lb2 = new JLabel("Enter Your Age: ");
- JTextField tf2 = new JTextField(18);
- JLabel lb3 = new JLabel("Enter Your Address: ");
- JTextField tf3 = new JTextField(18);
- JButton bt = new JButton("Send");
- JButton bt2 = new JButton("Clear");
- JLabel lb4 = new JLabel();
- public Samson_Form(){
- super("Samson - Information Form");
- setSize(600, 350);
- con.setLayout(fl);
- con.setBackground(Color.GRAY);
- lb1.setFont(bFont);
- lb2.setFont(bFont);
- lb3.setFont(bFont);
- lb4.setFont(bFont);
- add(lb1);
- add(tf1);
- add(lb2);
- add(tf2);
- add(lb3);
- add(tf3);
- add(bt);
- add(bt2);
- add(lb4);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- lb1.setPreferredSize(new Dimension(250,50));
- tf1.setPreferredSize(new Dimension(325,30));
- lb2.setPreferredSize(new Dimension(250,50));
- tf2.setPreferredSize(new Dimension(325,30));
- lb3.setPreferredSize(new Dimension(250,50));
- tf3.setPreferredSize(new Dimension(325,30));
- bt.setPreferredSize(new Dimension(200, 40));
- bt.setHorizontalAlignment(JButton.CENTER);
- bt2.setPreferredSize(new Dimension(200, 40));
- bt2.setHorizontalAlignment(JButton.CENTER);
- lb4.setPreferredSize(new Dimension(375,50));
- lb4.setHorizontalAlignment(JLabel.CENTER);
- lb4.setForeground(Color.BLACK);
- bt.setBackground(Color.BLACK);
- bt.setForeground(Color.GREEN);
- bt2.setBackground(Color.BLACK);
- bt2.setForeground(Color.RED);
- bt.setToolTipText("Click to Submit");
- bt2.setToolTipText("Click to Clear the Textfield");
- bt.addActionListener(this);
- bt2.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- tf1.setText("");
- tf2.setText("");
- tf3.setText("");
- lb4.setText("");
- }
- });
- }
- public void actionPerformed(ActionEvent e){
- if(tf1.getText().isEmpty()||tf2.getText().isEmpty()||tf3.getText().isEmpty()){
- JOptionPane.showMessageDialog(Samson_Form.this, "No Data Input.");
- }else{
- try{
- String name1 = tf1.getText();
- String age1 = tf2.getText();
- String add1 = tf3.getText();
- int age = Integer.parseInt(tf2.getText());
- String message = "Hi!, "+name1+".";
- String message2 = "Age: "+age1+" years old.";
- String message3 = "Address: "+add1+".";
- String message4A = "Congratulation, You Can Vote.";
- String message4B = "Sorry, You Can't Vote.";
- String msg;
- if(age > 17){
- msg = ""+message+"\n"+message2+"\n"+message3+"\n"+message4A;
- }else{
- msg = ""+message+"\n"+message2+"\n"+message3+"\n"+message4B;
- }
- lb4.setText("Information Submitted. . .");
- JOptionPane.showMessageDialog(Samson_Form.this, msg);
- }catch(NumberFormatException er){
- System.out.println(er);
- lb4.setText("Input Data Error on Age.");
- JOptionPane.showMessageDialog(Samson_Form.this, "You must Enter a Number for Age.");
- }
- }
- }
- public static void main(String[] args){
- Samson_Form fr = new Samson_Form();
- fr.setResizable(false);
- fr.setVisible(true);
- }
- }
Add Comment
Please, Sign In to add comment