logo

Analysis/Design: Case Study

   

Added on  2022-08-21

7 Pages1161 Words12 Views
Analysis/Design:
To implement this GUI program first, it is necessary to identify the components which are
required to implement a weight convertor. In this program, there are different types of JFrame
components used such as JButton, JPanel, JLable, JTextField and JTextArea. In this program,
JButtons are used to perform different actions of the program whenever the button is pushed.
JPanel is used to group all the buttons of the program because JPanel is a lightweight container
of javax.swing.Similarly, JTextField and JTextArea are used for user input and output.
JTextField is used for input, and the output of the program is appended into the JTextArea.
To implement a weight converter program, there are two different.java files used.
Conversion.java class is used to implement user interface and all other calculation is performed
in this class. testConversion.java is the main class where conversion.java class is called and
conversion.java class performs their task to produced an optimal output with an appropriate user
interface.
Coding:
Conversion.java:
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Conversion extends JFrame implements ActionListener{
JFrame f;
JTextField t;
String text;
static JTextArea textarea;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bconvert,bdel,bclr,bdec;
static double result=0;
Conversion()
{
f=new JFrame("Weight Convertor");
JPanel p=new JPanel();
p.setBounds(20,95,160,200);
p.setBackground(Color.lightGray);
textarea=new JTextArea("Hello Welcome to my Conversion
Calculator.!\n");
textarea.setBounds(200,50, 300,250);
f.add(textarea);

t=new JTextField();
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdec=new JButton(".");
bconvert=new JButton("Convert");
bdel=new JButton("<-");
JLabel lb=new JLabel("Weight in Pounds");
t.setBounds(20,50,165,30);
lb.setBounds(50,10,170,30);
b7.setBounds(20,100,50,40);
b8.setBounds(80,100,50,40);
b9.setBounds(140,100,50,40);
b4.setBounds(20,150,50,40);
b5.setBounds(80,150,50,40);
b6.setBounds(140,150,50,40);
b1.setBounds(20,200,50,40);
b2.setBounds(80,200,50,40);
b3.setBounds(140,200,50,40);
bdec.setBounds(20,250,50,40);
b0.setBounds(80,250,50,40);
bdel.setBounds(140,250,50,40);
bconvert.setBounds(65,300,80,40);
f.add(t);
f.add(lb);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b1);

p.add(b2);
p.add(b3);
p.add(bdec);
p.add(b0);
p.add(bdel);
p.add(bconvert);
f.add(p);
f.setLayout(null);
f.setVisible(true);
f.setSize(600,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bdec.addActionListener(this);
bconvert.addActionListener(this);
bdel.addActionListener(this);
t.addActionListener(this);
textarea.setEditable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText(t.getText().concat("1"));
if(e.getSource()==b2)
t.setText(t.getText().concat("2"));
if(e.getSource()==b3)

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Object-oriented Software Development
|9
|1222
|17