0% found this document useful (0 votes)
33 views

GUI Design HW 1

This document describes a Java Swing GUI for entering product information. It creates labels and text fields for an ID, name, price, and discount. It adds buttons to add or cancel the entry and displays the entered data or clears the fields accordingly.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

GUI Design HW 1

This document describes a Java Swing GUI for entering product information. It creates labels and text fields for an ID, name, price, and discount. It adds buttons to add or cancel the entry and displays the entered data or clears the fields accordingly.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

GUI Design HW 1

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ProductEntryForm {


public static void main(String[] args) {
// Create the main frame
JFrame frame = new JFrame("Product Entry Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);

// Create a panel to hold the components


JPanel panel = new JPanel(new GridLayout(5, 2, 5, 5));

// Create labels and text fields for ID, name, price, and discount
JLabel idLabel = new JLabel("ID:");
JTextField idTextField = new JTextField(10);

JLabel nameLabel = new JLabel("Name:");


JTextField nameTextField = new JTextField(10);

JLabel priceLabel = new JLabel("Price:");


JTextField priceTextField = new JTextField(10);
JLabel discountLabel = new JLabel("Discount:");
JTextField discountTextField = new JTextField(10);

// Create "Add" and "Cancel" buttons


JButton addButton = new JButton("Add");
JButton cancelButton = new JButton("Cancel");

// Add action listener to the "Add" button


addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id = idTextField.getText();
String name = nameTextField.getText();
String price = priceTextField.getText();
String discount = discountTextField.getText();

// Perform some action with the entered data, like saving it to a database
// For this example, we'll just display the entered data in the console
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Price: " + price);
System.out.println("Discount: " + discount);
}
});
// Add action listener to the "Cancel" button
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Clear all the text fields when "Cancel" is clicked
idTextField.setText("");
nameTextField.setText("");
priceTextField.setText("");
discountTextField.setText("");
}
});

// Add components to the panel


panel.add(idLabel);
panel.add(idTextField);
panel.add(nameLabel);
panel.add(nameTextField);
panel.add(priceLabel);
panel.add(priceTextField);
panel.add(discountLabel);
panel.add(discountTextField);
panel.add(addButton);
panel.add(cancelButton);

// Add the panel to the frame


frame.add(panel);
// Display the frame
frame.setVisible(true);
}
}

You might also like