| | |
Working with JLayeredPane and positioning of components.. HELP!
![]() |
•
•
Join Date: Sep 2004
Posts: 4
Reputation:
Solved Threads: 0
HELP.. I have a frame set to the screen size of my monitor, background color is yellow. I have a panel returned by midFrame() (outerPanel) supposed to be located at the center of my frame. i used the setBounds() to place my panel in the center but it doesn't do so.. and i have an inner panel with 2 more panels (a header and view panel) in it. the view panel is where i used the JLayeredPane so i could put components on top of each other-- a background image(using JLabel) and another panel(with buttons and more components). but i don't seem to add the components in the JLayeredPane correctly. what's wrong with my code? i hope you could help me.. parts of my code is included..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Frame extends JFrame {
//variables
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = new Dimension(640,600);
//constructor
public Frame() {
JPanel mainPanel = new JPanel();
mainPanel.setBackground(new Color(255,255,158)); //pale yellow
mainPanel.add(midFrame());
getContentPane().add(mainPanel);
//frame attributes
setTitle("PicPuzz");
setSize(screenSize);
setUndecorated(true);
show();
}
//middle frame
public JPanel midFrame() {
JPanel outerPanel = new JPanel();
outerPanel.setBackground(new Color(129,220,254)); //pale blue
//centers middle frame
Point origin = new Point(0,0);
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
origin.x = (screenSize.width - frameSize.width) / 2;
origin.y = (screenSize.height - frameSize.height) / 2;
outerPanel.setBounds(origin.x, origin.y, 640,600);
//create inner panel
JPanel innerPanel = new JPanel(new BorderLayout());
JPanel headerPanel = new JPanel();
JLayeredPane viewPanel = new JLayeredPane();
JLabel headerlbl = new JLabel(new ImageIcon("../bg/header.png"));
JLabel viewlbl = new JLabel(new ImageIcon("../bg/bg_view.png"));
viewPanel.setOpaque(true);
headerPanel.setBackground(new Color(128,255,128)); //light green
viewPanel.setBackground(new Color(128,255,128));
headerPanel.add(headerlbl);
viewPanel.add(viewlbl, new Integer(0)); //this doen't seem to work
viewPanel.add(shapePanel(), new Intger(1));
innerPanel.add(headerPanel, BorderLayout.NORTH);
innerPanel.add(viewPanel, BorderLayout.CENTER);
innerPanel.setBounds((origin.x+20), (origin.y+20), 600,500);
outerPanel.add(innerPanel);
return outerPanel;
}
......... more codes ...........
public static void main(String[] args) {
System.out.println("Frame.java");
new Frame();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Frame extends JFrame {
//variables
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = new Dimension(640,600);
//constructor
public Frame() {
JPanel mainPanel = new JPanel();
mainPanel.setBackground(new Color(255,255,158)); //pale yellow
mainPanel.add(midFrame());
getContentPane().add(mainPanel);
//frame attributes
setTitle("PicPuzz");
setSize(screenSize);
setUndecorated(true);
show();
}
//middle frame
public JPanel midFrame() {
JPanel outerPanel = new JPanel();
outerPanel.setBackground(new Color(129,220,254)); //pale blue
//centers middle frame
Point origin = new Point(0,0);
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
origin.x = (screenSize.width - frameSize.width) / 2;
origin.y = (screenSize.height - frameSize.height) / 2;
outerPanel.setBounds(origin.x, origin.y, 640,600);
//create inner panel
JPanel innerPanel = new JPanel(new BorderLayout());
JPanel headerPanel = new JPanel();
JLayeredPane viewPanel = new JLayeredPane();
JLabel headerlbl = new JLabel(new ImageIcon("../bg/header.png"));
JLabel viewlbl = new JLabel(new ImageIcon("../bg/bg_view.png"));
viewPanel.setOpaque(true);
headerPanel.setBackground(new Color(128,255,128)); //light green
viewPanel.setBackground(new Color(128,255,128));
headerPanel.add(headerlbl);
viewPanel.add(viewlbl, new Integer(0)); //this doen't seem to work
viewPanel.add(shapePanel(), new Intger(1));
innerPanel.add(headerPanel, BorderLayout.NORTH);
innerPanel.add(viewPanel, BorderLayout.CENTER);
innerPanel.setBounds((origin.x+20), (origin.y+20), 600,500);
outerPanel.add(innerPanel);
return outerPanel;
}
......... more codes ...........
public static void main(String[] args) {
System.out.println("Frame.java");
new Frame();
}
}
![]() |
Similar Threads
- What is White Hat SEO? (Search Engine Optimization)
- fixing position of awt objects in java (Java)
- "Turn Features On or Off" broken (Windows Vista and Windows 7)
- No able to set the jpanel positions (Java)
- Context Help Error 129 (C++)
- CD burning and CD drive not working (Troubleshooting Dead Machines)
Other Threads in the Java Forum
- Previous Thread: java programming
- Next Thread: reading a file into code
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac main map method methods mobile netbeans notdisplaying number online printf problem program project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor





