Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Basic Java GUI swing, Lab Reports of Java Programming

This can help you to think how to make a basic java swing

Typology: Lab Reports

2020/2021

Uploaded on 04/01/2022

zurbito-jacque-landrito
zurbito-jacque-landrito 🇵🇭

5 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 + ".");
pf2

Partial preview of the text

Download Basic Java GUI swing and more Lab Reports Java Programming in PDF only on Docsity!

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) { } }