Results 1 to 12 of 12
  1. #1
    zusss is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default JFrame+JPanel+JButton+JtextArea+ImageIcon

    Hello there,

    I was trying to create a window, with a picture (preferred .jpg) on left top corner, 3 buttons on center (I was trying to resize as you can see the buttons and full them with color but i take them out for the moment) and one text to the bottom of the window. I think I have done everything alright, but something is wrong and I take as a result just a frame window without panels.. :confused:

    That's my code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ControlPanel extends JFrame
    {
            JButton b1 = new JButton ("Enrol");
            JButton b2 = new JButton ("View Marks");
            JButton b3 = new JButton ("Search");
            JTextArea txtArea = new JTextArea("Copyright C University Of Darbdorf 2011");
    
            JLabel label1 = new JLabel();
    
            JPanel bottomPanel = new JPanel();
            JPanel eastPanel = new JPanel();
    
        public ControlPanel()
        {
            //bottomPanel.setLayout(new FlowLayout());
            bottomPanel.add(txtArea, BorderLayout.SOUTH);
            bottomPanel.add(b1);
            bottomPanel.add(b2);
            bottomPanel.add(b3);
            bottomPanel.setVisible(true);
    
            ImageIcon icon1 = new ImageIcon ("image.jpg");
            label1.setIcon(icon1);
    
            //upperPanel.setLayout(new BorderLayout());
            eastPanel.add(label1, BorderLayout.EAST);
            eastPanel.setVisible(true);
    
    
            //b1.setPreferredSize(new Dimension(25, 25));
            //b1.setBackground(Color.orange);
            //b1.setLocation(450, 330);
    
            //b2.setPreferredSize(new Dimension(25, 25));
            //b2.setBackground(Color.orange);
            //b2.setLocation(450, 360);
    
    
            //b3.setPreferredSize(new Dimension(25, 25));
            //b3.setBackground(Color.orange);
            //b3.setLocation(450, 390);
    
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
        }
        public static void main (String[] args)
        {
                ControlPanel myCP = new ControlPanel();
                myCP.setLocation(10, 10);
                myCP.setSize(1024, 768);
    
                myCP.setVisible(true);
         }
    }
    Thanks
    Panagiotis
    Last edited by Eranga; 02-15-2011 at 12:33 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Look at the following simple example

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class example {
    
        JFrame expFrame;
        JPanel expPanel;
        JTextField tempCelsius;
        JLabel celsiusLabel, fahrenheitLabel;
        JButton expButton;
    
        public example() {
            expFrame = new JFrame("Convert Celsius to Fahrenheit");
            expPanel = new JPanel();
            expPanel.setLayout(new GridLayout(2, 2));
            expPanel.setPreferredSize(new Dimension(200, 200));
    
            // Add the widgets.
            addWidgets();
    
            // Add the panel to the frame.
            expFrame.getContentPane().add(expPanel, BorderLayout.CENTER);
    
            // Exit when the window is closed.
            expFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Show the converter.
            expFrame.pack();
            expFrame.setVisible(true);
        }
    
        private void addWidgets() {
            expButton = new JButton("Button");
    
            expPanel.add(expButton);
        }
    
        public static void main(String[] args) {
            try {
          UIManager.setLookAndFeel(UIManager
              .getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
        }
    
        example obj = new example();
        }
    }
    If you cannot understand anything please let me know.

  4. #4
    zusss is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Well, I have already got the example you gave me and its better version

    Java Code:
     import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    
    public class CelsiusToFahrenheit extends JFrame implements ActionListener {
    
        JTextField tempCelsius;
        JLabel celsiusLabel, fahrenheitLabel;
        JButton convertTemp;
    
    
    public CelsiusToFahrenheit () { // Constructor
        super ("Celsius to Fahrenheit Converter");
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setSize (new Dimension (120, 40));
        setLayout (new GridLayout (2, 2));
        // INPUT
        tempCelsius = new JTextField();
        getContentPane().add(tempCelsius);
        celsiusLabel = new JLabel ("Celsius", SwingConstants.LEFT);
        getContentPane().add(celsiusLabel);
        // CONTROL
        convertTemp = new JButton("Convent");
        getContentPane().add(convertTemp);
        convertTemp.addActionListener(this); // LISTEN to button events
        // OUTPUT
        fahrenheitLabel = new JLabel ("Fahrenheit", SwingConstants.LEFT);
        getContentPane().add(fahrenheitLabel);
    }
    
    public void actionPerformed (ActionEvent e) {
    
        double dTempCelsius = Double.parseDouble (tempCelsius.getText());
        int tempFahr = (int) (dTempCelsius * 1.8 + 32);
        fahrenheitLabel.setText (tempFahr + " Fahrenheit");
    }
    
    public static void main(String [] args) {
        CelsiusToFahrenheit converter = new CelsiusToFahrenheit();
        converter.pack();
        converter.setVisible (true);
    }
    }
    I cant understand how I can add the image... can you please take a look at my first code and make some critical corrections in order to make me understand?

    Thank you very much for your time.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by zusss View Post
    I cant understand how I can add the image...
    I can't see that you've tried out anything related to that in your code. Give a try first of all.

    And also I don't know where you want to add it.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by zusss View Post
    can you please take a look at my first code and make some critical corrections in order to make me understand?.
    Sounds good actually. It's all about the practice. I've few comments anyway.

    You've comment the code more clearly. So it's easy to read. Keep it up.

    In the following line of code,

    Java Code:
    convertTemp.addActionListener(this); // LISTEN to button events
    using 'this' parameter can be dangerous in constructor. Because the object is not initialized. If you are confusing with that, leave it at the moment. You can learn more about 'this' later.

    Java Code:
    pack();
            setVisible(true);
    Put the above two lines in constructor. There is no reason to do that respect to the functionality, only for the clarity. Say you've several frames in your code. As you did you can code related stuff separately, with clear comments. Adding those make it complete.

    You've this line of code,

    Java Code:
    setSize (new Dimension (120, 40));
    Change the size something to 300,300 and run your code. Is that your UI changed to new size? If not, read the following article. If you stuck on something there, let me know.

    Steer clear of Java pitfalls - JavaWorld

  8. #8
    zusss is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Dear friend,

    Hello again. When I was saying that I dont know how to input an image I was talking about my first code, which you can see at the top of this page. This code is the reason I started this thread. The second programe I submit, it was about to show you that I have already done the example you had given to me. All I need is to correct the first code... and the example you gave me doesn't help me at all. Thank you very much for your time.

    Zuss

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

    Default

    You're not adding anything to the JFrame in your first post, so it makes perfect sense that you'll see nothing in it. You're also using BorderLayout wrong in that you're trying to add components to JPanels with BorderLayout constants but none of the containers (the JPanels receiving the components) use BorderLayout. Eranga's example shows how you can add things to the JFrame. I suggest you re-look at it and at the Swing tutorials which will show you this and more, and they can be found here: Using Swing Components

    Check out the section on using top level containers, the one on How to Make Frames, and the section on laying out components in a container. If you study these tutorials, you'll see where you're making your mistakes, and even if not, you'll have a better grasp on things and be better able to understand the advice we give.
    Last edited by Fubarable; 02-16-2011 at 02:24 AM.

  10. #10
    zusss is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you very much Fubarable... I have already correct my code
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ControlPanel extends JFrame
    {
    
        private JPanel TopPanel;
        private JPanel MidPanel;
        private JPanel BotPanel;
    
        public ControlPanel()
        {
            setTitle("Welcome to University Of Darbdord - Control Panel");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
    
            TopPanel = new TPClass();
            MidPanel = new MPClass();
            BotPanel = new BPClass();
    
            getContentPane().add(TopPanel, BorderLayout.PAGE_START);
            getContentPane().add(MidPanel, BorderLayout.CENTER);
            getContentPane().add(BotPanel, BorderLayout.PAGE_END);
        }
    
        public static void main(String args[])
        {
             JFrame CP = new ControlPanel();
             CP.setSize(770, 355);
             CP.setVisible(true);
        }
    
        static class TPClass extends JPanel
        {
            private JLabel LBI;
            ImageIcon icon = new ImageIcon("image.jpg");
    
            public TPClass()
            {
                 LBI = new JLabel(icon);
    
                 add(LBI);
            }
        }
    
        static class MPClass extends JPanel
        {
            private JButton b1;
            private JButton b2;
            private JButton b3;
            private JButton b4;
    
            public MPClass()
            {
                 b1 = new JButton("Enroll");
                 b1.setPreferredSize(new Dimension(150, 25));
                 b1.setBackground(Color.green);
    
                 b2 = new JButton("View Marks");
                 b2.setPreferredSize(new Dimension(150, 25));
                 b2.setBackground(Color.green);
    
                 b3 = new JButton("Search");
                 b3.setPreferredSize(new Dimension(150, 25));
                 b3.setBackground(Color.green);
                 
                 b4 = new JButton("Options");
                 b4.setPreferredSize(new Dimension(180,30));
                 b4.setBackground(Color.yellow);
    
                 add(b1);
                 add(b2);
                 add(b3);
                 add(b4);
            }
        }
    
        static class BPClass extends JPanel
            {
                private JLabel TextLabel;
    
                public BPClass()
                {
                    TextLabel = new JLabel("Copyright © University Of Darbdorf 2011");
    
                    add (TextLabel);
                }
            }
    }
    Anyway thank you for your help and you ofcourse your time!
    Now I am on how to make it a little bit more advance by adding a button which will prompt you to the options of this control panel like chaning the colours of buttons and back ground colour for the sensitive eyes and so on...

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by zusss View Post
    Dear friend,

    Hello again. When I was saying that I dont know how to input an image I was talking about my first code, which you can see at the top of this page. This code is the reason I started this thread. The second programe I submit, it was about to show you that I have already done the example you had given to me. All I need is to correct the first code... and the example you gave me doesn't help me at all. Thank you very much for your time.

    Zuss
    If you cannot identify what I've explain in my code, means you've even not try to read and understand it.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by zusss View Post
    Anyway thank you for your help and you ofcourse your time!
    Now I am on how to make it a little bit more advance by adding a button which will prompt you to the options of this control panel like chaning the colours of buttons and back ground colour for the sensitive eyes and so on...
    Do some experiments all the time while doing your exact task.

Similar Threads

  1. [SOLVED] JButton does not display ImageIcon properly
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 03:47 AM
  2. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 02:27 PM
  3. Netbeans Help JButton Imageicon
    By hitmen in forum AWT / Swing
    Replies: 1
    Last Post: 02-19-2009, 07:56 PM
  4. system-print(ln) on jtextarea or jpanel
    By Tamu in forum Advanced Java
    Replies: 1
    Last Post: 11-25-2008, 05:18 PM
  5. Replies: 1
    Last Post: 08-08-2007, 01:25 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
  •