Results 1 to 1 of 1
- 10-31-2011, 12:55 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
Create a Class that instantiates the GUI interface question
Hi everyone! Looking for some help for my java homework if possible. I am writing code for a miles per gallon calculator. I am finished with the program and it works but there are some specifics in the assignment that I am unsure how to implement into my own code. Here are the directions I am stuck on-
Create a class named RunMPGGui.java that will instantiate a MPGGui object. In addition to creating the MPGGui object, the class should do the following:
1 Set the window width to 225 and its height to 200.
2 Set the window to display in the center of the screen.
3 Make the window visible.
Java Code:import javax.swing.*; import java.awt.event.*; import java.text.DecimalFormat; public class MPGgui extends JFrame { private JPanel panel; private JLabel mpgCalc; private JLabel milesTrav; private JLabel galUsed; private JTextField mileTextField; private JTextField galTextField; private JButton calcButton; private JButton quitButton; private final int WINDOW_WIDTH = 225; private final int WINDOW_HEIGHT = 200; public MPGgui() { setTitle("MPG Calculator"); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buildPanel(); add(panel); setVisible(true); } private void buildPanel() { mpgCalc = new JLabel("MPG Calculator"); milesTrav = new JLabel("Enter miles travelled"); galUsed = new JLabel("Enter gallons used "); mileTextField = new JTextField(6); galTextField = new JTextField(6); calcButton = new JButton("Calculate MPG"); calcButton.addActionListener(new CalcButtonListener()); quitButton = new JButton("Quit"); quitButton.addActionListener(new QuitButtonListener()); panel = new JPanel(); panel.add(mpgCalc); panel.add(milesTrav); panel.add(mileTextField); panel.add(galUsed); panel.add(galTextField); panel.add(calcButton); panel.add(quitButton); } private class CalcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { double miles; double gallons; double mpg; miles = Double.parseDouble(mileTextField.getText()); gallons = Double.parseDouble(galTextField.getText()); mpg = miles/gallons; DecimalFormat formatter = new DecimalFormat("#0.00"); JOptionPane.showMessageDialog(null, "Distance: " + formatter.format(miles) + "\n Miles per Gallon: " + formatter.format(mpg)); } } private class QuitButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } }
Java Code:public class RunMPGGui { public static void main(String[] args) { MPGgui mg = new MPGgui(); } }
Similar Threads
-
create an object of interface
By yma16 in forum New To JavaReplies: 6Last Post: 04-16-2011, 05:28 AM -
Create extra GUI interface
By africanhacker in forum New To JavaReplies: 7Last Post: 03-27-2011, 11:05 AM -
Interface Question
By superman1938 in forum Advanced JavaReplies: 1Last Post: 12-14-2010, 05:28 AM -
Create interface from my code
By Lyricid in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 06:39 PM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 08:04 AM
Bookmarks