Results 1 to 7 of 7

Thread: Java GUI

  1. #1
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default Java GUI

    I'm teaching myself Java and need some help
    This is new to me, and I'm having a hard time getting started with a program

    I'm trying to create a GUI that allows me to type in feet and the computer calculate the number of inches in a foot.

    Any help would be appreciated

  2. #2
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    Using netbeans 6.9.1 for IDE

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you done so far? This isn't especially challenging, you should start here: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Try seeing what you come up with and post your attempt here with some specific questions.

  4. #4
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default GUI program

    Thanks for responding

    First of all, I'm using netbeans as the Ide. How do I open this file and extend the JFrame?

  5. #5
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by program junkie View Post
    Thanks for responding

    First of all, I'm using netbeans as the Ide. How do I open this file and extend the JFrame?
    I don't know what you mean by "open this file," any details would help.

    But extending a JFrame is easy, example:
    Java Code:
    import javax.swing.*;
    
    public class window extends JFrame {
        window() {
            super("JFrame Extension Example");    // Call this so we get a window title
            setSize(200, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        // This runs the program
        public static void main(String args[]) {
            window wnd = new window();
    
            wnd.setVisible(true);    // So we can see the window
        }
    }
    That's all there is to it.

    Tha
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  6. #6
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default Gui

    I'm trying to write a similar program to this one.

    I want to calculate feet and inches though

    I want the program to have the ActionListener listen to the textfield for buttons being clicked




    import java.awt.*;
    import javax.swing.*;

    public class FahrenheitGUI extends JFrame
    {
    private JLabel tempQuestionLabel = new JLabel( );
    private JTextField tempBox = new JTextField( );
    private JLabel tempAnswerLabel= new JLabel( );;
    private Container contentPane;
    private JButton submitButton = new JButton( );

    public FahrenheitGUI ()
    {
    createGUI();
    } // ends Welcome constructor

    private void createGUI ( )
    {
    // get content pane and set the layout to null
    contentPane = getContentPane();
    contentPane.setBackground(Color.white);
    contentPane.setLayout(null);

    // set up the question label
    tempQuestionLabel.setText ("Enter inches:");
    tempQuestionLabel.setLocation(0,0);
    tempQuestionLabel.setSize(200,30);
    contentPane.add(tempQuestionLabel);

    // set up the textfield for user entry
    tempBox.setText("0");
    tempBox.setLocation(210,0 );
    tempBox.setSize(100,30 );
    contentPane.add(tempBox);

    // set up the submit button
    submitButton.setText("Submit");
    submitButton.setLocation(50,100);
    submitButton.setSize(100,30);
    contentPane.add(submitButton);

    // set up the label where answer will be displayed
    tempAnswerLabel.setText (" ");
    tempAnswerLabel.setLocation(0,200);
    tempAnswerLabel.setSize(300,30);
    contentPane.add(tempAnswerLabel);

    // set properties of window
    setTitle ("Fahrenheit Program");
    setSize(400,400);
    setVisible(true);

    }// ends creatGUI method.

    public static void main ( String [] args )
    {
    FahrenheitGUI application = new FahrenheitGUI( );
    application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }// ends main


    }// ends program

  7. #7
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    First, you need to add this statement to your imports
    Java Code:
    import java.awt.event.*;
    It has all the event functionality.

    Now, you can easily implement an action listener like this:
    Java Code:
    anyButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            // Put your code where for what you want to when you press a button
        }
    });


    Also, when posting code here, please use the [code] tags.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

Posting Permissions

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