Results 1 to 9 of 9
  1. #1
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default Help placing images in JFrame

    Ok, I would like to add a row of 10 images between the QuestionLabel which is centered, and the Buttons which are south. The images to be added are 10 of the ImageIcon icon2. Is there a way to place these all lined up in a row in such a specific location.

    Here is the code

    Java Code:
     public QuestionsFrame(Sequencer theSequencer,String anIcon,String q,String b1,String b2,String b3,String b4,int a)
        {
            
           //this.frame = frame;
          // this.counter = counter;
           answer = a;
           theicon = anIcon;
           
          // mario1.aSequencer1 = theSequencer;
           
           //Theframe = Theframe;
          //  Question = q;
            Question = q;
            B1 = b1;
            B2 = b2;
            B3 = b3;
            B4 = b4;
            
             //size the frame
            setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
            cp = getContentPane();
            cp.setBackground(Color.white);
              
            //create Icon object and set its location
           ImageIcon icon = new ImageIcon("C:\\users\\Matt\\Documents\\" + theicon);
           ImageIcon icon2 = new ImageIcon ("C:\\users\\Matt\\Documents\\coin.jpg");
                   
                
           // ImageIcon icon1 = new ImageIcon("C:\\users\\Matt\\Documents\\mario1.gif");
            JButton button1 = new JButton(new AnswerAction(B1,answer ==1,questionNumber));  //create button
            JButton button2 = new JButton(new AnswerAction(B2,answer ==2,questionNumber));
            JButton button3 = new JButton(new AnswerAction(B3,answer==3,questionNumber));
            JButton button4 = new JButton(new AnswerAction(B4,answer==4,questionNumber));
              
            ImagePanel = new JPanel();
            ImagePanel.setBackground(Color.white);
            //ImagePanel1 = new JPanel();
            QuestionPanel = new JPanel();
          //  QuestionPanel.setBackground(Color.BLUE);
           
            add(ImagePanel,BorderLayout.NORTH);
            //add(ImagePanel,BorderLayout.SOUTH);
            newLabel = new JLabel();
        
            //add the icon to the label
           // newLabel.setLocation(29,40);
        
            newLabel.setPreferredSize((new Dimension(600,450)));
            newLabel.setIcon(icon);
          
           // newLabel.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
           // newLabel1.setIcon(icon1);
              
            ImagePanel.add(newLabel);
            
           
            //ImagePanel2.setLocation(200,10);
            //ImagePanel1.add(newLabel1);
                           
            QuestionPanel.add(button1);  // adding button to frame
            QuestionPanel.add(button2);
            QuestionPanel.add(button3);
            QuestionPanel.add(button4);
            
            add(QuestionPanel,BorderLayout.SOUTH);
            
            QuestionLabel = new JLabel(Question,JLabel.CENTER); 
            add(QuestionLabel,BorderLayout.CENTER);
        }//end of QuestionFrame Contstructor

  2. #2
    IYIaster is offline Member
    Join Date
    Jul 2009
    Posts
    46
    Rep Power
    0

    Default

    I always just use labels and html to add images
    I'm sorry never mind I stopped using HTML I use netbeans so putting things where I want them is easy. But, here is the code that I took from my app
    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/setup/newpackage/e.png")));
    Last edited by IYIaster; 07-23-2009 at 09:19 PM.

  3. #3
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    Ok I know that but how do I place it in the frame at exactly that place?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Welcome to the world of layout management.
    Here's a couple of suggestions for inspiration. I played around with Box layouts a bit but couldn't find anything I liked. You never know about Box layouts.
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class Placement {
        private JPanel getGBCenter() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weighty = 1.0;    // <-- play with
            gbc.weightx = 1.0;
            panel.add(new JLabel(), gbc);
            JLabel label = new JLabel("Question Label", JLabel.CENTER);
            label.setBorder(BorderFactory.createEtchedBorder());
            gbc.weighty = 0.5;    // <-- these three
            panel.add(label, gbc);
            gbc.weighty = 0.7;    // <-- weighty values
            panel.add(getIconPanel(), gbc);
            return panel;
        }
    
        private JPanel getGridCenter() {
            JLabel label = new JLabel("Question Label", JLabel.CENTER);
            label.setBorder(BorderFactory.createEtchedBorder());
            JPanel panel = new JPanel(new GridLayout(0,1));
            panel.add(new JPanel());
            panel.add(label);
            panel.add(getIconPanel());
            return panel;
        }
    
        private JPanel getIconPanel() {
            BufferedImage image = getImage();
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,2,2,2);
            gbc.weightx = 1.0;
            for(int i = 0; i < 10; i++) {
                panel.add(new JLabel(new ImageIcon(image)), gbc);
            }
            panel.setBorder(BorderFactory.createEtchedBorder());
            return panel;
        }
    
        private BufferedImage getImage() {
            int w = 30, h = 30, type = BufferedImage.TYPE_INT_RGB;
            BufferedImage image = new BufferedImage(w, h, type);
            Graphics2D g2 = image.createGraphics();
            g2.setPaint(Color.blue);
            g2.fillRect(0, 0, w, h);
            g2.setPaint(Color.orange);
            int m = 3;
            g2.fillOval(m, m, w-2*m, h-2*m);
            g2.dispose();
            return image;
        }
    
        private JPanel getLast() {
            JPanel panel = new JPanel();
            for(int i = 0; i < 4; i++) {
                panel.add(new JButton("button " + (i+1)));
            }
            panel.setBorder(BorderFactory.createEtchedBorder());
            return panel;
        }
    
        public static void main(String[] args) {
            Placement test = new Placement();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getGBCenter());
    //        f.add(test.getGridCenter());
            f.add(test.getLast(), "Last");
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }

  5. #5
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    Wow thanks a lot.

  6. #6
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    Lets say I want to use setLocation(Point,Point). Within the JFrame, depending on its size, how do I know where the points are. For example is 0,0 the top left corner? If a frame were 400,400 would the center be at 200,200?

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

    Default

    Why do you want to know this location? Layout managers usually frees you from having to worry about such things.

  8. #8
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    Well if you look at my first post, I am trying to put a row of images, between an located North, and buttons located south. Since I am building a quiz game, each time a question is answered correctly a coin will appear in this location, if another question is answered correctly the next image would be placed just next to it on the right hand side, so another words with each correct answer and another image being placed in the frame, it would form a row of images. Since I want them in such a specific spot, I thought I would just use setLocation for each one.

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

    Default

    Quote Originally Posted by gammaman View Post
    Well if you look at my first post,...
    I saw your first post, and I also saw Hardwire's excellent response where he shows you how you really don't need to know absolute positioning, how layout managers allow you to let them worry about the positioning. I recommend that you follow this recommendation.

Similar Threads

  1. Images in JFrame
    By AWPtic in forum New To Java
    Replies: 9
    Last Post: 03-23-2009, 03:22 PM
  2. Placing Files In Folder
    By ramesh.8189 in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 12:36 PM
  3. Placing a progress bar in a SWT table
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:59 PM
  4. Swing Components Placing
    By ne2000 in forum Eclipse
    Replies: 2
    Last Post: 06-23-2008, 08:00 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
  •