Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help centering the label and making the Temperature, Distance, and Exit buttons the same size. import java.awt.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel;

I need help centering the label and making the "Temperature", "Distance", and "Exit" buttons the same size.

import java.awt.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JOptionPane; import java.awt.event.ActionListener; import java.awt.event.ActionEvent;

public class GUI {

public static class Converter{

// attribute private double input;

// default constructor with no parameter which sets input //to Double.NaN [Not a Number] Converter(){ this.input = Double.NaN; }

// overloaded constructor with input for parameter Converter(double input){ this.input = input; }

// get and set methods double getInput() { return this.input; } public void setInput(double input) { this.input = input; }

// method convert() to return input value double convert() { return this.input; }

}// end Converter

public static class TemperatureConverter extends Converter{

// constructs that call parent constructor TemperatureConverter(){ super(); }

TemperatureConverter(double input){ super(input); }

/* overridden convert() method to convert Fahrenheit to Celsius *and return value; if the instance has no input or not a number, *it should return Double.NaN */ double convert() { if(super.getInput() == Double.NaN) { return Double.NaN; } else { return((super.getInput() - 32) * 5)/9; } }

}// end TemperatureConverter

public static class DistanceConverter extends Converter{

// constructors that call parent constructor DistanceConverter(){ super(); }

DistanceConverter(double input){ super(input); }

/* overridden convert() method to convert input (distance in miles) * to distance(km) and return value; if the instance has no input *or not a number, it should return Double.NaN **/ double convert() { if(super.getInput() == Double.NaN) { return Double.NaN; } else { return super.getInput() * 1.609; } }

}// end DistanceConverter

public static class GUIConverter extends Converter{

GUIConverter(){

// implement GUIConverter class JFrame frame = new JFrame("Converter"); JPanel panel = new JPanel(); // set label JLabel label1 = new JLabel("Please select which converter you woud like:"); label1.setHorizontalTextPosition(JLabel.CENTER); // GUI buttons JButton distance = new JButton("Distance"); JButton temperature = new JButton("Temperature"); JButton exit = new JButton("Exit"); GridBagLayout layout = new GridBagLayout(); panel.setLayout(layout); GridBagConstraints window = new GridBagConstraints(); window.insets = new Insets(10, 10, 10, 10); // add constraints on specific buttons window.fill = GridBagConstraints.HORIZONTAL; window.gridx = 0; window.gridy = 0; panel.add(label1, window); window.fill = GridBagConstraints.HORIZONTAL; window.gridx = 0; window.gridy = 1; panel.add(distance, window);

window.gridx = 1; window.gridy = 1; panel.add(temperature, window);

window.gridx = 0; window.gridy = 2; window.fill = GridBagConstraints.HORIZONTAL; window.gridwidth = 2; panel.add(exit, window);

// set size and layout of frame frame.add(panel); frame.setLayout(new GridBagLayout()); frame.setSize(500, 160); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // terminate program when user closes GUI

// add action listener to buttons; allows user to input value after // Distance Converter button is selected and get converted result distance.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // store input String str = JOptionPane.showInputDialog("Input distance in miles to" + " convert to kilometers: "); double miles = Double.parseDouble(str); // conversion to double

// instance creation of DistanceConverter object using miles DistanceConverter obj = new DistanceConverter(miles); // call convert() of object and display result JOptionPane.showMessageDialog(frame, miles + " miles equals " + obj.convert()+ " kilometers."); } }); // allows user to input value when Temperature button is selected and // returns converted result temperature.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // store input string String str = JOptionPane.showInputDialog("Input Fahrenheit temp to" + " convert to Celsius: "); double fahren = Double.parseDouble(str); // conversion to double // instance creation of TemperatureConverter object TemperatureConverter obj = new TemperatureConverter(fahren); JOptionPane.showMessageDialog(frame, fahren + " F equals " + obj.convert() + "C."); } }); // program terminates when user clicks Exit exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); // dispose frame } }); }

public static void main(String[] args) {

// call constructor to create GUI new GUIConverter(); } } }

This is how I need it to look:

image text in transcribed
Converter X Please select which converter you would like: Distance Temperature Exit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions