Question
please help me Question 1: Simple GUI-Based Input/Output with JOptionPane Most applications you use on a daily basis use windows or dialog boxes (also called
please help me
Question 1: Simple GUI-Based Input/Output with JOptionPane
Most applications you use on a daily basis use windows or dialog boxes (also called dialogs) to interact with the user. Dialog boxes are windows in which programs display important messages to the user or obtain information from the user. Javas JOptionPane class (package javax.swing) provides prebuilt dialog boxes for both input and output. These are displayed by invoking static JOptionPane methods.
import javax.swing.JOptionPane; // program uses JOptionPane public class Addition { public static void main( String[] args ) { // obtain user input from JOptionPane input dialogs String firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); String secondNumber = JOptionPane.showInputDialog( "Enter second integer" );
// convert String inputs to int values for use in a calculation int number1 = Integer.parseInt( firstNumber ); int number2 = Integer.parseInt( secondNumber ); int sum = number1 + number2; // add numbers // display result in a JOptionPane message dialog JOptionPane.showMessageDialog( null, "The sum is " + sum, "Sum of Two Integers", JOptionPane.PLAIN_MESSAGE ); } // end method main } // end class Addition |
To Do:
Write the above program that presents a simple addition application that uses two input dialogs to obtain integers from the user and a message dialog to display the sum of the integers the user enters.
Propose a new class called Division where the output is number1/number2 NB: The class should prevent the division by zero. If a user type 0 for the number 2 the class invite him to introduce a non-null number.
Question 2:
The goal here is to create a graphic user interface of student information. When you enter the name and the grade of one student (figure 1.a), and the OK pushbutton is clicked, a message dialog is opened with the student information (figure 1.b).
(b)
Figure 1: Graphic user interface of student information
To do so, we consider Student class with the coresponding variables and methods (Lab3) in the studentproject package as follows.
.
Student.java |
package studentproject; public class Student { private String name; private double grade;
public void setName(String n){ name = n; } public String getName(){ return name; } public void setGrade(double g){ grade = g; } public double getGrade(){ return grade; } } |
Also, we define in the same project a subclass of JFrame, named StudentFrame that we need to complete.
To Do:
By considering the following requirements, complete the class StudentFrame defined as below and send the execution proof.
Requirements |
Since we want a frame to process button click event, we must implement the ActionListener interface. |
StudentFrame class declares all the components to use. |
In the constructor, we create all the component's objects. |
Layout managers arrange the components in a container. |
The components concerning the name and the grade are placed first on a panel of JPanel type with GridLayout Manager layout. |
The button is palced on another panel with FlowLayout Manager layout. |
The two panels are next added to the container. |
When the OK button is clicked, the ActionPerformed method of the ActionLister interface is called. In this method, we want to show the student information in the message dialog. |
We can retrieve the text associated to the name amd grade components by calling its getText method. |
StudentFrame.java |
package studentproject;
import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.*; import javax.swing.*;
public class StudentFrame extends JFrame implements .. {
private JLabel labelName, labelGrade; private JTextField textName, textGrade; private JButton ok; private GridLayout layout; // // layout object private JPanel panel,buttonPanel; // frame container private Container container;
// Constructor adds components to JFrame public StudentFrame() {
( "Student information" ); container = getContentPane(); container.setLayout(new BorderLayout());
labelName = new ( "Student name: " ); textName = new ("" );
layout = new GridLayout( 2, 2 ); // 2 pixel gaps panel = new (layout ); panel.add(); panel.add();
labelGrade = new ( "Grade: " ); textGrade= new JTextField ("" ); panel.add(labelGrade); panel.add(); ok = new ("OK"); ok.addActionListener(); buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add();
container.add(panel, BorderLayout.CENTER); container.add(, BorderLayout.SOUTH);
} public void actionPerformed(ActionEvent e){ Student s = new Student();
s.setName(.. getText()); s.setGrade(Double.parseDouble(textGrade. )); String x = s.getName() + " " + s.getGrade(); JOptionPane.showMessageDialog( null, "" + );
} } |
In the main class, we create an object of StudentFrame (class above).
LabelFrames setDefaultCloseOperation method with constant JFrame.EXIT_ON_CLOSE as the argument indicates that the program should terminate when the window is closed by the user.
To set frame size, we use the setSize() method.
To display frame, we use the setVisible() method.
StudentProject.java |
public class StudentProject {
public static void main(String[] args) { StudentFrame labelFrame = new (); labelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); labelFrame.setSize( 250, 120 ); labelFrame.setVisible(); }
} |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started