Click here to Skip to main content
15,889,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a issue about layout in java application,
my frame have a menu on top and a jPanel with layout is border layout.
this panel is main panel and it is added more jPanel(component panel also has layout is borderlayout) at Center.
and I created 1 jLabel with icon is a picture.

My issue is how to display this picture(the size can change) at center(horizal and vertical are center) of screen(my frame).


please help me.

thank in advance
Posted
Updated 3-Feb-13 20:26pm
v2
Comments
[no name] 2-Feb-13 1:54am    
If its Web App, you can certainly use CSS for it..

1 solution

This example achieves what you're describing;

Java
package com.bornander.imagetest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class MainWindow extends JFrame {
	
	private JMenuBar menuBar = new JMenuBar();
	private JPanel mainPanel = new JPanel();
	
	public MainWindow(String filename) throws IOException {
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(menuBar, BorderLayout.NORTH);
		contentPane.add(mainPanel, BorderLayout.CENTER);
		
		menuBar.setBackground(Color.RED);
		menuBar.add(new JButton("Foo"));
		mainPanel.setBackground(Color.GREEN);
		mainPanel.setLayout(new BorderLayout());
		
		JLabel picLabel = new JLabel(new ImageIcon(ImageIO.read(new File(filename))));
		mainPanel.add(picLabel, BorderLayout.CENTER);
		
		setSize(320, 240);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}


Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900