Results 1 to 8 of 8

Thread: GUI's

  1. #1
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default GUI's

    I have this old code that is all text, it runs completely in the General Output. I was wondering how difficult it would be to change that code into a GUI based program. If it would be fairly simple, how do I do it?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    It depends. If its purely textual information you want to convey, it can be pretty simple. NetBeans has a built in GUI builder that is all drag 'n' drop. I suggest trying it out.

  3. #3
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    I dont have NetBeans. I have a customized JCreator. It came on a CD with Java For Dummies (edition 4). And I wanted GUI for the user input. With text boxes and all.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Its free, I suggest you download it and give it a try:
    Welcome to NetBeans

  5. #5
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    No, I'd prefer JCreator. So I can follow the instructions in Java for Dummies. I might get it when I finish the book. Maybe. Only if I think it's better. But can you (or anyone) answer my original question.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Well, I'll paste in the code for a simple GUI with text input and output in a minute, but I think if you are planning on coding it from scratch it'll be a bit of a learning curve for you.
    Java Code:
    package test;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    /**
     * @author Steffen Gates
     */
    public class GUI {
        public static void main(String[] args) {
            new GUI();
        }
    
        JFrame window;      //The whole window
        JPanel panel;       //A place to put things in the window
        JTextField textIn;  //input
        JLabel label;       //output
        JButton button;     //simple button
    
        public GUI(){
            window = new JFrame();
            window.setSize(640, 150);
            window.setLocationRelativeTo(null);             //Center the window
            panel = new JPanel();
            textIn = new JTextField();
            textIn.setColumns(50);                          //50 char's wide
            label = new JLabel("Lets Print Something!");    //default output
            button = new JButton("Click Me!");
            button.addActionListener(new ButtonListener()); //make button do something
            panel.add(textIn);          //Add everything to the panel
            panel.add(label);
            panel.add(button);
            window.add(panel);          //add panel to the window
            window.setVisible(true);    //make window visible
        }
        
        //To make an action listener, just make a class that implements
        //ActionListener and has a method called actionPerformed()
        class ButtonListener implements ActionListener{
            public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand().compareTo("Click Me!") == 0)
                    label.setText(textIn.getText());
            }
        }
    }
    Last edited by quad64bit; 07-26-2009 at 03:35 AM.

  7. #7
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    Wouldn't it have been easier to use

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

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Importing unneeded packages is wasteful. When projects get large, importing everything increases compile time a great deal. If your program had 100 classes and 100,000 lines+ of code, it could take many many extra minutes, or even hours.

    As such, when you use a good IDE, it will import only what is needed and as needed. In netbeans, when I type my code, the imports happen automatically; therefor there are many import statements, but they are specific.

    Its no extra work on my part, as I said its automatic.

    PS: I added some comments to the code. Also, since you are doing this by hand, you're required to readup quite a bit about some of the simpler parts of GUI design. Read up about layout managers, as its the only way to get your gui laid out the way you want. I suggest a grid layout or border layout.
    Last edited by quad64bit; 07-26-2009 at 03:43 AM.

Similar Threads

  1. Swing themes & custom GUI's
    By Mr.Beans in forum AWT / Swing
    Replies: 3
    Last Post: 04-28-2009, 02:10 AM
  2. GUI's
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-22-2008, 09:19 PM
  3. Creating GUI's for simulators.
    By Modifier in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 12:35 AM

Posting Permissions

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