Results 1 to 6 of 6
  1. #1
    crazysketch is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default How can I add JLabels, JButtons, JTextFields to a JPanel then JFrame?

    Here's my code.. Everything works except the text class doesnt show up :confused:.

    Java Code:
    public class Main{
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JFrame f = new JFrame();
            
            f.add(new Visual());
            f.setSize(500, 500);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    }
    Java Code:
    package javarobot;
    import javax.swing.*;
    import java.awt.*;
    /**
     *
     * @author Sketch
     */
    public class Visual extends JPanel{
    
        private text t;
        private ImageIcon image;
    
    
        public void text(){
            t = new text();
        }
    
        public void paint(Graphics g) {
            super.paint(g);
            image = new ImageIcon(getClass().getResource("bender.jpg"));
            image.paintIcon(this, g, 80, 10);
    
        }
    }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javarobot;
    import javax.swing.*;
    /**
     *
     * @author Sketch
     */
    public class text{
         private static JLabel label;
         private static JTextField input;
         private static JButton button;
         private static String content = "Ask me anything! I dare ya...!";
    
        public text() {
            label = new JLabel(content);
            label.setBounds(150, 200, 200, 100);
    
            input = new JTextField();
            input.setBounds(80, 300, 300, 30);
    
            button = new JButton("ASK");
            button.setBounds(170, 350, 100, 30);
    
            //content = input.getText();
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) make all static variables instance or non-static variables. They have no business being static.
    2) You're not adding the components to any thing so it makes sense that they won't be shown. To learn how to do this, you should go through the Oracle Swing tutorials which will show you all you need to know.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    public void text(){
        t = new text();
    }
    Bleech!

  4. #4
    crazysketch is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Sorry guys I'm new to Java. I updated my coding a bit. I was wondering why the variable string "content" doesn't change? Any improvement suggestions would be appreciated. Thanks.

    Java Code:
    package javabot;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     *
     * @author Sketch
     */
    public class Main extends JFrame implements ActionListener{
        String content = "Ask me anything! I dare ya...!";
        
        public Main() {
    
            JLabel label; // privates can only be uses within a class
            JTextField input;
            JButton button;
            
            JLabel img;
            Icon icons;
    
            icons = new ImageIcon(getClass().getResource("bender.jpg"));
            img = new JLabel(icons);
            img.setBounds(80, 10, 300, 233);
    
            label = new JLabel(content);
            label.setBounds(150, 220, 200, 100);
    
            input = new JTextField();
            input.setBounds(80, 300, 300, 30);
            input.addActionListener(this);// fiedlds have addactionlisteners when user press enter
    
            button = new JButton("ASK");
            button.setBounds(170, 350, 100, 30);
    
            add(img);
            add(label);
            add(input);
            add(button);
    
            setSize(460, 450);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(null);
            setVisible(true);
            setResizable(false);
            setTitle("Robot");;
        }
        public void actionPerformed(ActionEvent e){ // all methods in an interface is public
            content = "You said something!";
        }
    
        public static void main(String[] args) {
    
            new Main();
        }
    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I was wondering why the variable string "content" doesn't change?

    The variable content does change: that's what the assignment does in

    Java Code:
    content = "You said something!";

    Perhaps something else does not change? If so go to the API docs for that something. (ask if you don't know where they are) and find the methods that will make it change.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Sorry, that was meant to be a hint and not totally cryptic...

    The thing is you say

    Java Code:
    label = new JLabel(content);

    but that does not set up any "link" between the label and the string content. Rather it just creates a label displaying whatever the string is at that time. What you need to do in your actionPerformed() is change what the label displays not the string assigned to content.

    Methods that can make the label do stuff are here.

Similar Threads

  1. JPanel and JLabels Not Repainting
    By phosphide in forum AWT / Swing
    Replies: 6
    Last Post: 11-13-2010, 03:17 AM
  2. Replies: 6
    Last Post: 06-16-2010, 06:03 AM
  3. JFrame: JButtons in a grid
    By jackal in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 07:56 PM
  4. How to add lines to a JFrame full of JTextFields
    By Dumisan in forum AWT / Swing
    Replies: 2
    Last Post: 02-07-2010, 09:12 PM
  5. Replies: 5
    Last Post: 03-09-2009, 03:09 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
  •