Results 1 to 4 of 4
- 01-23-2012, 09:20 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
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:
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.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; } } } }
How can I achieve this?Last edited by Alpa; 01-23-2012 at 09:26 PM.
- 01-23-2012, 09:29 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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)
- 01-23-2012, 09:35 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 17
- Rep Power
- 0
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.
-
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
-
Absolute beginner
By duff18 in forum New To JavaReplies: 15Last Post: 02-19-2011, 10:07 AM -
[SOLVED] I'm having problems using JButtons with ImageIcons and Actions. Any suggest
By Singing Boyo in forum New To JavaReplies: 1Last Post: 03-09-2009, 03:18 AM -
get position in string from caret position
By helloworld111 in forum AWT / SwingReplies: 5Last Post: 02-19-2009, 01:36 AM -
Problems with ImageIcons
By The_L in forum AWT / SwingReplies: 2Last Post: 10-24-2008, 02:04 AM -
Specifying absolute path in web.xml
By Felissa in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-05-2007, 06:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks