

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This can help you to think how to make a basic java swing
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


import java.awt.; import javax.swing.; import java.awt.event.*; public class OOP_Exer3 implements ActionListener { public static void main(String [] args) { FlowLayout fl = new FlowLayout(); //create the frame JFrame fr = new JFrame("OOP Exercise 3"); //set frame size fr.setSize(400,200); //create labels JLabel lbl1 = new JLabel("Enter your name: "); JLabel lbl2 = new JLabel(); //create text field JTextField txt1 = new JTextField(25); //create button JButton btn1 = new JButton("Greet Me"); //add components to the frame fr.setLayout(fl); fr.add(lbl1); fr.add(txt1); fr.add(btn1); fr.add(lbl2); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if(txt1.getText().equals("")) { JOptionPane.showMessageDialog(null,"Enter Your Name", "Error", JOptionPane.ERROR_MESSAGE); txt1.requestFocusInWindow(); } else { String myName = txt1.getText(); lbl2.setText("Hi!, " + myName + ".");
//set frame visibility fr.setVisible(true); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent ae) { } }