Click here to Skip to main content
12,635,149 members (22,854 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
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 1-Feb-13 16:06pm
ngthtra1.4K
Updated 3-Feb-13 21:26pm
v2
Comments
Krunal Rohit 2-Feb-13 1:54am
   
If its Web App, you can certainly use CSS for it..

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This example achieves what you're describing;

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100