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

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 need help centering the label and making the "Temperature", "Distance", and "Exit" buttons the same size. import java.

Post by answerhappygod »

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(); } }}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply