Results 1 to 4 of 4
  1. #1
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default How to set an ImageIcons position variable while using an absolute layout?

    Hey guys, I have a problem.

    Right now, I'm trying to work with GUIs. And in my example I want to have a button that is able to output three pictures randomly. Therefore I'm using the following code:

    Java Code:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class gui3{
    	
    	JFrame frame = new JFrame();
    	JPanel panel = new JPanel();
    	JButton button1 = new JButton("Show pictures");
    	JLabel label1 = new JLabel();
    	JLabel label2 = new JLabel();
    	JLabel label3 = new JLabel();
    	
    	public gui3(){		
    		button1.setBounds(6, 6, 117, 29);
    		label1.setBounds(479, 223, 100, 160);
    		label2.setBounds(452, 249, 100, 160);
    		label3.setBounds(509, 265, 100, 160);
    		
    		panel.setLayout(null);
    		panel.add(button1);
    		panel.add(label1);
    		panel.add(label2);
    		panel.add(label3);
    		
    		button1.addActionListener(new buttonhandler());
    
    		frame.setTitle("Test");
    		frame.setSize(800, 600);
    		frame.add(panel);
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    	
    	public class buttonhandler implements ActionListener{
    
    		public void actionPerformed(ActionEvent e) {
    			ArrayList<Integer> pics = new ArrayList<Integer>();
    			
    			for(int i = 0; i < 3; i++){
    				pics.add(i);
    			}
    			
    			Collections.shuffle(pics);
    			System.out.println(pics);
    			
    			switch(pics.remove(0)){
    			case 0:
    				label1.setIcon(new ImageIcon("Pic1.jpg"));
    				break;
    			case 1:
    				label2.setIcon(new ImageIcon("Pic2.jpg"));
    				break;
    			case 2:
    				label3.setIcon(new ImageIcon("Pic3.jpg"));	
    				break;
    			}
    		}
    		
    
    	}
    }
    As you can see the bounds are set to each picture. If case 0 is removed, Pic1.jpg will be displayed at (479, 223, 100, 160). If case 1 is removed, Pic2.jpg will be displayed at (452, 249, 100, 160). If case 2 is removed, Pic2.jpg will be displayed at (509, 265, 100, 160). So every picture has its defined bounds and positions. But now I want this to be variable. I want the first selected picture to be placed at (479, 223, 100, 160), the second selected picture at (452, 249, 100, 160) and the third selected at (509, 265, 100, 160). So that no matter which case is true, the pictures should be placed at this position.

    How can I achieve this?
    Last edited by Alpa; 01-23-2012 at 09:26 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How to set an ImageIcons position variable while using an absolute layout?

    Perhaps read the following to learn about whether you might achieve what you need with the appropriate layout manager (s)

    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  3. #3
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to set an ImageIcons position variable while using an absolute layout?

    Why can't I just use the absolute layout? I made the window not resizable, so using the absolute layout shouldn't be a problem?
    Last edited by Alpa; 01-23-2012 at 09:41 PM.

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

    Default Re: How to set an ImageIcons position variable while using an absolute layout?

    There are times when you want to use a null layout (such as when overlapping components) but they are rare. I'm not 100% sure the behavior you're trying to achieve. Can you describe more -- what stays constant for instance? What doesn't?

Similar Threads

  1. Absolute beginner
    By duff18 in forum New To Java
    Replies: 15
    Last Post: 02-19-2011, 10:07 AM
  2. Replies: 1
    Last Post: 03-09-2009, 03:18 AM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  4. Problems with ImageIcons
    By The_L in forum AWT / Swing
    Replies: 2
    Last Post: 10-24-2008, 02:04 AM
  5. Specifying absolute path in web.xml
    By Felissa in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-05-2007, 06:56 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
  •