Results 1 to 7 of 7

Thread: Creating a GUI

  1. #1
    HoustonsOwn88 is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Creating a GUI

    So i have this assignment. We just started working on GUI's and im totally lost. Can someone help me with this?

    this is basically what i have to create



    but with these guidelines/rules

    Create application with three classes: TimeOfCallPanel, RateCategoryPanel and Demo_LongDistanceCharges

    In RateCategoryPanel you will declare constants and radio buttons; write constructor and GetRate method, which return chosen rate.

    In TimeOfCallPanel you will declare text field, wrist the constructor and getCharges method

    In Demo_LongDistanceCharges you will declare both panels and all buttons, write appropriate constructor, write buildButtonPanel method, use inner classes for calculating charges and for exiting program. Do not forget to include main method here, which will create an instance of the Demo_LongDistanceCharges class.

    anyone who's good at java help me out with this?

  2. #2
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Creating a GUI

    I think your guidelines are fairly self explanatory.
    Within Demo_LongDistanceCharges you will declare and add the two JPanels to the JFrame. Look at the different layout managers available, the default is borderLayout. First panel contains radio buttons, remember they will be mutually exclusive, you are provided with a way to group these to make sure this is the case. Next panel contains a JTextField. I assume you enter the amount of minutes, and when calculate is pressed it returns the amount charged back into the text field, or into a new frame that opens? Either way is easy, just use a method to calculate the price using the price referring to the radio button pressed and the text field entry. If you look in the java API. It has all the methods you need to manipulate the components of your GUI. Go an try to implement this and come back with any problems you encounter.

  3. #3
    HoustonsOwn88 is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Creating a GUI

    ok this is what i have. but it wont run. can someone help why?


    import javax.swing.*;



    import java.awt.event.*;


    /**

    The MetricConverterWindow class lets the user enter a

    distance in kilometers. Radio buttons can be selected to

    convert the kilometers to miles, feet, or inches.

    */



    public class LongDistanceCharges {



    private JPanel panel; // A holding panel

    private JLabel minutesLabel; // A lable for the minutes

    private JTextField minutesTextField; // To hold user input of minutes

    private JRadioButton daytimeButton; // To select datyime

    private JRadioButton eveningButton; // To select evening

    private JRadioButton peakButton; // To select peak

    private ButtonGroup radioButtonGroup; // To group radio buttons

    private final int WINDOW_WIDTH = 400; // Window width

    private final int WINDOW_HEIGHT = 100; // Window height



    /**

    Constructor

    */



    public LongDistanceCharges()

    {

    // Set the title.

    setTitle("Long Distance Calls");



    // Set the size of the window.

    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);



    // Specify an action for the close button.

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    // Build the panel and add it to the frame.

    buildPanel();



    // Add the panel to the frame's content pane.

    add(panel);



    // Display the window.

    setVisible(true);

    }



    private void setVisible(boolean b) {





    }



    private void add(JPanel panel2) {





    }



    private void setDefaultCloseOperation(int exitOnClose) {





    }



    private void setSize(int wINDOWWIDTH, int wINDOWHEIGHT) {





    }



    private void setTitle(String string) {





    }



    /**

    The buildPanel method adds a label, text field, and

    and three buttons to a panel.

    */


    private void buildPanel() {



    // Create the label, text field, and radio buttons.

    minutesLabel = new JLabel("Enter the minutes used");

    minutesTextField = new JTextField(10);

    daytimeButton = new JRadioButton("Daytime Rate");

    eveningButton = new JRadioButton("Evening Rate");

    peakButton = new JRadioButton("Peak Rate");



    // Group the radio buttons.

    radioButtonGroup = new ButtonGroup();

    radioButtonGroup.add(daytimeButton);

    radioButtonGroup.add(eveningButton);

    radioButtonGroup.add(peakButton);



    // Add action listeners to the radio buttons.

    daytimeButton.addActionListener(new RadioButtonListener());

    eveningButton.addActionListener(new RadioButtonListener());

    peakButton.addActionListener(new RadioButtonListener());



    // Create a panel and add the components to it.

    panel = new JPanel();

    panel.add(minutesLabel);

    panel.add(minutesTextField);

    panel.add(daytimeButton);

    panel.add(eveningButton);

    panel.add(peakButton);

    }



    /**

    Private inner class that handles the event when

    the user clicks one of the radio buttons.

    */



    private class RadioButtonListener implements ActionListener

    {

    public void actionPerformed(ActionEvent e)

    {

    String input; // To hold the user's input

    String rateTo = ""; // The rate that is payed

    double result = 0.0; // To hold the conversion



    // Get the kilometers entered.

    input = minutesTextField.getText();



    // Determine which radio button was clicked.

    if (e.getSource() == daytimeButton)

    {

    // Multiply by daytime rate.

    rateTo = " dollars.";

    result = Double.parseDouble(input) * 0.07;

    }

    else if (e.getSource() == eveningButton)

    {

    // Multiply by eveing rate.

    rateTo = " dollars.";
    result = Double.parseDouble(input) * 0.12;

    }

    else if (e.getSource() == peakButton)

    {

    // Multiply by peak rate

    rateTo = " inches.";

    result = Double.parseDouble(input) * 0.05;

    }



    // Display the final price

    JOptionPane.showMessageDialog(null, input +

    " The price of your minutes used are as follows; " + result + rateTo);



    }



    /**

    The main method creates an instance of the

    Longdistancecharges class, displaying its window.

    */



    public void main(String[] args)
    {


    LongDistanceCharges mcw =

    new LongDistanceCharges();

    }
    }
    }

  4. #4
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Creating a GUI

    Your class should extend JFrame, which it doesn't seem to do at the moment. Then in your main method when you create a new JFrame instead of new JFrame() you will insert the name of your class.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,090
    Rep Power
    20

    Default Re: Creating a GUI

    Please use [code] tags [/code] when posting code.
    I for one rarely read unformatted code.
    You also seem to have an awful lot of whitespace (blank lines) in there...

    I will say, don't extend JFrame. That is considered bad practice.

    If you haven't got a JFrame then in your LongDistance (whatever it's called class) you need to create one.
    But I'll wait for readable code before I go into more detail.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    HoustonsOwn88 is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Creating a GUI

    Java Code:
    import javax.swing.*;
    
    import java.awt.event.*;
    
    
    /**
    The MetricConverterWindow class lets the user enter a
    distance in kilometers. Radio buttons can be selected to
    convert the kilometers to miles, feet, or inches.
    
    */
    
    
    
    public class LongDistanceCharges {
    private JPanel panel; // A holding panel
    private JLabel minutesLabel; // A lable for the minutes
    private JTextField minutesTextField; // To hold user input of minutes
    private JRadioButton daytimeButton; // To select datyime
    private JRadioButton eveningButton; // To select evening
    private JRadioButton peakButton; // To select peak
    private ButtonGroup radioButtonGroup; // To group radio buttons
    private final int WINDOW_WIDTH = 400; // Window width
    private final int WINDOW_HEIGHT = 100; // Window height
    
    
    
    /**
    Constructor
    */
    
    
    
    public LongDistanceCharges()
    {
    // Set the title.
    setTitle("Long Distance Calls");
    
    // Set the size of the window.
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    
    
    
    // Specify an action for the close button.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    
    // Build the panel and add it to the frame.
    buildPanel();
    
    
    
    // Add the panel to the frame's content pane.
    add(panel);
    
    
    
    // Display the window.
    setVisible(true);
    }
    
    
    
    private void setVisible(boolean b) {
    
    
    
    
    
    }
    
    
    
    private void add(JPanel panel2) {
    }
    
    private void setDefaultCloseOperation(int exitOnClose) {
    }
    private void setSize(int wINDOWWIDTH, int wINDOWHEIGHT) {
    
    }
    private void setTitle(String string) {
    }
    
    
    
    /**
    
    The buildPanel method adds a label, text field, and
    and three buttons to a panel.
    
    */
    private void buildPanel() {
    // Create the label, text field, and radio buttons.
    minutesLabel = new JLabel("Enter the minutes used");
    minutesTextField = new JTextField(10);
    daytimeButton = new JRadioButton("Daytime Rate");
    eveningButton = new JRadioButton("Evening Rate");
    peakButton = new JRadioButton("Peak Rate");
    // Group the radio buttons.
    radioButtonGroup = new ButtonGroup();
    radioButtonGroup.add(daytimeButton);
    radioButtonGroup.add(eveningButton);
    radioButtonGroup.add(peakButton);
    
    
    // Add action listeners to the radio buttons.
    daytimeButton.addActionListener(new RadioButtonListener());
    eveningButton.addActionListener(new RadioButtonListener());
    peakButton.addActionListener(new RadioButtonListener());
    
    // Create a panel and add the components to it.
    panel = new JPanel();
    panel.add(minutesLabel);
    panel.add(minutesTextField);
    panel.add(daytimeButton);
    panel.add(eveningButton);
    panel.add(peakButton);
    }
    
    
    
    /**
    
    Private inner class that handles the event when
    the user clicks one of the radio buttons.
    
    */
    
    private class RadioButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    
    {
    
    String input; // To hold the user's input
    String rateTo = ""; // The rate that is payed
    double result = 0.0; // To hold the conversion
    
    
    // Get the kilometers entered.
    input = minutesTextField.getText();
    
    // Determine which radio button was clicked.
    
    if (e.getSource() == daytimeButton)
    {
    // Multiply by daytime rate.
    
    rateTo = " dollars.";
    
    result = Double.parseDouble(input) * 0.07;
    
    }
    
    else if (e.getSource() == eveningButton)
    
    {
    
    // Multiply by eveing rate.
    rateTo = " dollars.";
    result = Double.parseDouble(input) * 0.12;
    
    }
    
    else if (e.getSource() == peakButton)
    
    {
    
    // Multiply by peak rate
    rateTo = " inches.";
    result = Double.parseDouble(input) * 0.05;
    
    }
    
    
    
    // Display the final price
    
    JOptionPane.showMessageDialog(null, input +
    " The price of your minutes used are as follows; " + result + rateTo);
    
    
    
    }
    
    
    
    /**
    
    The main method creates an instance of the
    
    Longdistancecharges class, displaying its window.
    
    */
    
    
    
    public void main(String[] args)
    {
    
    
    LongDistanceCharges mcw =
    
    new LongDistanceCharges();
    
    }
    }
    }
    sorry i didnt know it needed a format. here it is in code now. as for the previous comment im a little confused on what you meant?
    Last edited by HoustonsOwn88; 11-16-2012 at 12:38 AM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,090
    Rep Power
    20

    Default Re: Creating a GUI

    A desktop Swing application needs a JFrame (or some similar top level component).
    That's what gets displayed.
    All you're doing at the moment is creating a JPanel.
    This is the HelloWorld app from the Swing tutorials...note the JFrame code.
    Your JPanel would be added to the content pane, instead of the JLabel in that example.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 01:50 PM
  2. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  3. Creating an EXE help
    By funnygames in forum New To Java
    Replies: 5
    Last Post: 09-27-2010, 07:53 PM
  4. Creating jar
    By nitinverma in forum AWT / Swing
    Replies: 1
    Last Post: 07-12-2010, 11:07 AM
  5. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-25-2009, 11:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •