Ramirez_RD

Samson_Ramirez-JFrame.Event

May 13th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Samson_Swing extends JFrame implements ActionListener{  
  5.     private Container con = getContentPane();
  6.    
  7.     FlowLayout fl = new FlowLayout();
  8.     Font bFont = new Font("Arial", 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.     JLabel lb4 = new JLabel();
  17.     JLabel lb5 = new JLabel();
  18.     JLabel lb6 = new JLabel();
  19.     JLabel lb7 = new JLabel();    
  20. public Samson_Swing() {  
  21.     super("Message");
  22.         setSize(600, 500);
  23.         con.setLayout(fl);
  24.         con.setBackground(Color.GRAY);    
  25.         lb1.setFont(bFont);
  26.         lb2.setFont(bFont);
  27.         lb3.setFont(bFont);
  28.         lb4.setFont(bFont);
  29.         lb5.setFont(bFont);
  30.         lb6.setFont(bFont);
  31.         lb7.setFont(bFont);  
  32.         add(lb1);
  33.         add(tf1);
  34.         add(lb2);
  35.         add(tf2);
  36.         add(lb3);
  37.         add(tf3);
  38.         add(bt);
  39.         add(lb4);
  40.         add(lb5);
  41.         add(lb6);
  42.         add(lb7);
  43.        
  44.      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45.      lb1.setPreferredSize(new Dimension(250,50));
  46.      tf1.setPreferredSize(new Dimension(325,30));
  47.      lb2.setPreferredSize(new Dimension(250,50));
  48.      tf2.setPreferredSize(new Dimension(325,30));
  49.      lb3.setPreferredSize(new Dimension(250,50));
  50.      tf3.setPreferredSize(new Dimension(325,30));
  51.      bt.setPreferredSize(new Dimension(200, 40));
  52.      bt.setHorizontalAlignment(JButton.CENTER);
  53.      lb4.setPreferredSize(new Dimension(375,50));
  54.      lb5.setPreferredSize(new Dimension(375,50));
  55.      lb6.setPreferredSize(new Dimension(375,50));
  56.      lb7.setPreferredSize(new Dimension(375,50));
  57.      lb4.setHorizontalAlignment(JLabel.CENTER);
  58.      lb5.setHorizontalAlignment(JLabel.CENTER);
  59.      lb6.setHorizontalAlignment(JLabel.CENTER);
  60.      lb7.setHorizontalAlignment(JLabel.CENTER);
  61.      lb4.setForeground(Color.BLACK);
  62.      lb5.setForeground(Color.BLACK);
  63.      lb6.setForeground(Color.BLACK);
  64.      lb7.setForeground(Color.BLACK);  
  65.      bt.setBackground(Color.BLACK);
  66.      bt.setForeground(Color.YELLOW);
  67.      bt.setToolTipText("Click to Submit");
  68.      bt.addActionListener(this);
  69.  }
  70.    public void actionPerformed(ActionEvent e) {
  71.        String name1 = tf1.getText();
  72.        String age1 = tf2.getText();
  73.        String add1 = tf3.getText();    
  74.        int age = Integer.parseInt(tf2.getText());
  75.        
  76.        String message = "Hi!, "+name1+".";
  77.        String message2 = "Age: "+age1+".";
  78.        String message3 = "Address: "+add1+".";
  79.        String message4A = "You Can Vote.";
  80.        String message4B = "You Can't Vote.";    
  81.        lb4.setText(message);
  82.        lb5.setText(message2);
  83.        lb6.setText(message3);
  84.        if(age > 17){
  85.            lb7.setText(message4A);
  86.        }else{
  87.            lb7.setText(message4B);
  88.        }
  89.     }
  90.    public static void main(String []args){
  91.         Samson_Swing fr = new Samson_Swing();
  92.         fr.setVisible(true);
  93.    }
  94. }
Add Comment
Please, Sign In to add comment