Results 1 to 5 of 5
  1. #1
    Mr. Niceguy is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Standard Panel HelloWorld

    Hello all, I just started programming and have not much experience. I want to make my first GUI and before doing that, I tested this code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    
    public class FirstFrame extends JFrame implements ActionListener{
    
      private JButton testButton = new JButton("Klik");
      private JTextField tekst = new JTextField(14);
      private JPanel panel = new JPanel();
    
      public FirstFrame(){
        
        panel.setLayout(new BorderLayout());
        panel.add(testButton,BorderLayout.CENTER);
        panel.add(tekst);
    
        setTitle("HelloWorld!!");
        setLayout(new FlowLayout());   
        setContentPane(panel);
        getContentPane().setBackground(Color.cyan);
        testButton.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,400);
        setLocation(300,300);
        setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e){
        tekst.setText("Hello World!!");
      }
    }
    The thing is I get nothing printed on the screen. I must be doing something wrong ( something silly I guess ). Can someone at least point me in the right direction??
    Last edited by Norm; 12-14-2011 at 01:03 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Standard Panel HelloWorld

    How are you executing the program?
    Do you get any errors when you try to execute it? Please post them here.

  3. #3
    Mr. Niceguy is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: Standard Panel HelloWorld

    Hi, thx for posting back. Sorry, I forgot to post the main

    public class RunTestFrame{
    public static void main(String[] args){
    new FirstFrame();
    }
    }

    My code doesn't display the button. I think I might be making a common mistake. Also, I want to display the JButton and the JTextfield in the Center.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Standard Panel HelloWorld

    Where do you add the components you want to see to the GUI?
    Change the creation of the textfield to have a text in it so you can see where it is.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,842
    Rep Power
    19

    Default Re: Standard Panel HelloWorld

    If I remember correctly (and it's possible I don't) don't you need to pack() a JFrame before making it visible?
    Isn't that the thing that lays out the components properly?

Similar Threads

  1. New to Eclipse, can't run HelloWorld
    By Daimoth in forum Eclipse
    Replies: 1
    Last Post: 12-25-2011, 06:35 PM
  2. How to Run HelloWorld program?
    By siji44 in forum New To Java
    Replies: 8
    Last Post: 03-24-2010, 08:09 AM
  3. HelloWorld in EJB!
    By bdtagar in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 01-04-2010, 11:55 AM
  4. About Java HelloWorld
    By Dankydoo in forum New To Java
    Replies: 0
    Last Post: 01-11-2009, 10:08 PM
  5. Run a helloWorld in NetBeans
    By mathias in forum NetBeans
    Replies: 1
    Last Post: 08-07-2007, 01:16 AM

Tags for this Thread

Posting Permissions

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