Question

In: Computer Science

Give an example of a program in java that creates a GUI with at least one...

Give an example of a program in java that creates a GUI with at least one button and several textfields. Some of the textfields should be for input and others for output. Make the output textfields uneditable. When the button is clicked, the input fields should be read, some calculation performed and the result displayed in the output textfield(s).

Solutions

Expert Solution

Code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class CalcVolCube extends JFrame implements ActionListener {

   /**

   *

   */

   private static final long serialVersionUID = 1L;

   GridLayout layout = new GridLayout(9, 0);

   JLabel len = new JLabel(" Length:");

   JLabel wid = new JLabel(" Width:");

   JLabel hgt = new JLabel(" Height:");

   JLabel ansl = new JLabel(" Volume of Cube: ");

   JTextField t1 = new JTextField(30);

   JTextField t2 = new JTextField(30);

   JTextField t3 = new JTextField(30);

   JTextField ansf = new JTextField(30);

   JButton calc = new JButton(" Calculate ");

   Float ans;

   /**

   *

   */

   public CalcVolCube() {

       super("Cube Volume Calculator");

       setSize(300, 300);

       add(len);

       add(t1);

       add(wid);

       add(t2);

       add(hgt);

       add(t3);

       add(ansl);

       add(ansf);

       add(calc);

       ansf.setEditable(false);

       setLayout(layout);

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       calc.addActionListener(this);

       setVisible(true);

   }

   /*

   * (non-Javadoc)

   *

   * @see

   * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)

   */

   public void actionPerformed(ActionEvent e) {

       String n1 = t1.getText();

       String n2 = t2.getText();

       String n3 = t3.getText();

       Float num1 = Float.parseFloat(n1);

       Float num2 = Float.parseFloat(n2);

       Float num3 = Float.parseFloat(n3);

       Object clicked = e.getSource();

       if (calc == clicked) {

           ansf.setText(String.valueOf(num1*num2*num3));

       }

       ansf.setEditable(false);

   }

   /**

   * @param args

   */

   public static void main(String[] args) {

       CalcVolCube calc = new CalcVolCube();

       calc.setVisible(true);

   }

}

Output:



Related Solutions

Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and deposit. Test the methods in the main function.
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. Note: Use dialog boxes for communicating with the user. Remember to use the JOptionPane class which is the graphical user interface (GUI) and Comments that are...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. Note: Use dialog boxes for communicating with the user. Remember to use the JOptionPane class which is the graphical user interface (GUI) and Comments that are...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class •...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class • uses proper design techniques including reading UML Class Diagrams • reads input from the keyboard using a Scanner Object and its methods • uses selection (if and if else) statements • uses iteration (while, do while or for) statements • uses String comparison methods. • follows standard acceptable programming practices. • handles integer overflow errors Prep Readings: Zybooks chapter 1 - 6 1. General...
Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
Need a program in java that creates a random addition math quiz The program should ask...
Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT