I am having trouble centering the label above the buttons, and I would like for the "Exit" button to be under the "Dista

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

I am having trouble centering the label above the buttons, and I would like for the "Exit" button to be under the "Dista

Post by answerhappygod »

I am having trouble centering the label above the buttons, and Iwould like for the "Exit" button to be under the "Distance" and"Temperature" buttons.
package cmis242week6;
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 CMIS242 {
public static class Converter{
// attribute private double input;
// default constructorwith no parameter which sets input //to Double.NaN [Not aNumber] Converter(){ this.input= Double.NaN; }
// overloaded constructorwith input for parameter Converter(doubleinput){ this.input= input; }
// get and setmethods double getInput() { returnthis.input; } public void setInput(doubleinput) { this.input= input; }
// method convert() toreturn input value double convert() { returnthis.input; }
}// end Converter
public static class TemperatureConverterextends Converter{
// constructs that callparent constructor TemperatureConverter(){ super(); }
TemperatureConverter(double input){ super(input); }
/* overridden convert()method to convert Fahrenheit to Celsius *and return value; if theinstance has no input or not a number, *it should returnDouble.NaN */ double convert() { if(super.getInput() == Double.NaN) { return Double.NaN; } else{ return((super.getInput() - 32) *5)/9; } }
}// end TemperatureConverter
public static class DistanceConverter extendsConverter{
// constructors that callparent constructor DistanceConverter(){ super(); }
DistanceConverter(doubleinput){ super(input); }
/* overridden convert()method to convert input (distance in miles) * to distance(km) and returnvalue; if the instance has no input *or not a number, it shouldreturn Double.NaN **/ double convert() { if(super.getInput() == Double.NaN) { return Double.NaN; } else{ return super.getInput() * 1.609; } }
}// end DistanceConverter
public static class GUIConverter{
GUIConverter(){
//implement GUIConverter class JFrameframe = new JFrame("Converter"); frame.setLayout(new FlowLayout()); JPanelpanel = new JPanel(); // setlabel JLabellabel1 = new JLabel(); label1.setText("Please select which converter you woudlike:"); // GUIbuttons JButtondistance = new JButton("Distance"); JButtontemperature = new JButton("Temperature"); JButtonexit = new JButton("Exit");
// addbuttons to Panel panel.add(label1); panel.add(distance); panel.add(temperature); panel.add(exit);
// setsize and layout of frame frame.add(panel); frame.setLayout(new FlowLayout()); frame.setSize(500, 90); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //terminate program when user closes GUI
// addaction listener to buttons; allows user to input value after //Distance Converter button is selected and get convertedresult distance.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e){ // store input String str = JOptionPane.showInputDialog("Input distance in milesto" + " convert to kilometers: "); double miles =Double.parseDouble(str); // conversion to double
// instance creation ofDistanceConverter object using miles DistanceConverter obj =new DistanceConverter(miles); // call convert() ofobject and display result JOptionPane.showMessageDialog(frame, miles + " miles equals "+ obj.convert()+ "kilometers."); } }); // allows user to inputvalue when Temperature button is selected and // returnsconverted result temperature.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e){ // store inputstring String str =JOptionPane.showInputDialog("Input Fahrenheit temp to" + " convert to Celsius: "); double fahren =Double.parseDouble(str); // conversion to double // instance creation ofTemperatureConverter object TemperatureConverterobj = new TemperatureConverter(fahren); JOptionPane.showMessageDialog(frame, fahren + " F equals "+ obj.convert() +"C."); } }); // programterminates when user clicks Exit exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e){ frame.dispose(); //dispose frame } }); }
publicstatic void main(String[] args) {
// call constructor to create GUI new GUIConverter(); } }}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply