Ramirez_RD

Samson_Ramirez - Form

Jun 14th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Samson_Form extends JFrame implements ActionListener {
  5.     private Container con = getContentPane();
  6.    
  7.     FlowLayout fl = new FlowLayout();
  8.     Font bFont = new Font("Times New Roman", Font.BOLD, 20);
  9.     JLabel lb1 = new JLabel("Enter Your Fullname: ");
  10.     JTextField tf1 = new JTextField(18);
  11.     JLabel lb2 = new JLabel("Enter Your Age: ");
  12.     JTextField tf2 = new JTextField(18);
  13.     JLabel lb3 = new JLabel("Enter Your Address: ");
  14.     JTextField tf3 = new JTextField(18);
  15.     JButton bt = new JButton("Send");
  16.     JButton bt2 = new JButton("Clear");
  17.     JLabel lb4 = new JLabel();
  18.    
  19.     public Samson_Form(){
  20.         super("Samson - Information Form");
  21.         setSize(600, 350);
  22.         con.setLayout(fl);
  23.         con.setBackground(Color.GRAY);    
  24.         lb1.setFont(bFont);
  25.         lb2.setFont(bFont);
  26.         lb3.setFont(bFont);
  27.         lb4.setFont(bFont);
  28.         add(lb1);
  29.         add(tf1);
  30.         add(lb2);
  31.         add(tf2);
  32.         add(lb3);
  33.         add(tf3);
  34.         add(bt);
  35.         add(bt2);
  36.         add(lb4);
  37.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.         lb1.setPreferredSize(new Dimension(250,50));
  39.         tf1.setPreferredSize(new Dimension(325,30));
  40.         lb2.setPreferredSize(new Dimension(250,50));
  41.         tf2.setPreferredSize(new Dimension(325,30));
  42.         lb3.setPreferredSize(new Dimension(250,50));
  43.         tf3.setPreferredSize(new Dimension(325,30));
  44.         bt.setPreferredSize(new Dimension(200, 40));
  45.         bt.setHorizontalAlignment(JButton.CENTER);
  46.         bt2.setPreferredSize(new Dimension(200, 40));
  47.         bt2.setHorizontalAlignment(JButton.CENTER);
  48.         lb4.setPreferredSize(new Dimension(375,50));
  49.         lb4.setHorizontalAlignment(JLabel.CENTER);
  50.         lb4.setForeground(Color.BLACK);
  51.         bt.setBackground(Color.BLACK);
  52.         bt.setForeground(Color.GREEN);
  53.         bt2.setBackground(Color.BLACK);
  54.         bt2.setForeground(Color.RED);
  55.         bt.setToolTipText("Click to Submit");
  56.         bt2.setToolTipText("Click to Clear the Textfield");
  57.         bt.addActionListener(this);
  58.         bt2.addActionListener(new ActionListener(){
  59.            public void actionPerformed(ActionEvent e){
  60.                tf1.setText("");
  61.                tf2.setText("");
  62.                tf3.setText("");
  63.                lb4.setText("");
  64.            }
  65.         });
  66.     }
  67.     public void actionPerformed(ActionEvent e){
  68.         if(tf1.getText().isEmpty()||tf2.getText().isEmpty()||tf3.getText().isEmpty()){
  69.             JOptionPane.showMessageDialog(Samson_Form.this, "No Data Input.");
  70.         }else{
  71.             try{
  72.                 String name1 = tf1.getText();
  73.                 String age1 = tf2.getText();
  74.                 String add1 = tf3.getText();    
  75.                 int age = Integer.parseInt(tf2.getText());
  76.        
  77.                 String message = "Hi!, "+name1+".";
  78.                 String message2 = "Age: "+age1+" years old.";
  79.                 String message3 = "Address: "+add1+".";
  80.                 String message4A = "Congratulation, You Can Vote.";
  81.                 String message4B = "Sorry, You Can't Vote.";    
  82.                 String msg;
  83.                 if(age > 17){
  84.                     msg = ""+message+"\n"+message2+"\n"+message3+"\n"+message4A;
  85.                 }else{
  86.                     msg = ""+message+"\n"+message2+"\n"+message3+"\n"+message4B;
  87.                 }
  88.                 lb4.setText("Information Submitted. . .");
  89.                 JOptionPane.showMessageDialog(Samson_Form.this, msg);
  90.             }catch(NumberFormatException er){
  91.                 System.out.println(er);
  92.                 lb4.setText("Input Data Error on Age.");
  93.                 JOptionPane.showMessageDialog(Samson_Form.this, "You must Enter a Number for Age.");
  94.             }
  95.         }
  96.     }
  97.     public static void main(String[] args){
  98.         Samson_Form fr = new Samson_Form();
  99.         fr.setResizable(false);
  100.         fr.setVisible(true);
  101.     }
  102. }
Add Comment
Please, Sign In to add comment