I was making the following program and found some problem in setting the JLabel Component i.e. background_image. What I was trying to have a container and in this conatiner I was trying to put this JLabel. And thought to make it right align I can use the following alogrithm:
FrameWidth-ImageWidth,FrameHeight-ImageHeight
But it push the image a bit down(about 60 pixels) though in X it is Ok. What could be the reason?
I also tried to find the menubar width and height and try to put in my alogrithm assuming that Panel will start after menubar instead of top, but in vain. Moreover on trying to find out width and height of menubar it is showing me (0,0) value.
Can you help me to set this?
regards,
Arun
Code:-
********
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.net.*;
import java.io.*;
public class
test extends JFrame
implements ActionListener
{
JButton toolbutton;
JToolBar toolbar;
JLabel back;
boolean toolbar_flag = true;
makePanel mp=null;
public test()
{
JMenuBar menubar;
JMenu menu;
JMenuItem menuItem;
Containercont = getContentPane();
cont.setBackground(new Color(140,199,222));
cont.setFont(new Font("arial",Font.BOLD,11));
cont.setLayout(null);
menubar = new JMenuBar();
setJMenuBar(menubar);
mp = new makePanel();
//Build File Menu
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menubar.add(menu);
//Items in File Menu
menuItem = new JMenuItem("Change User/Login",KeyEvent.VK_C);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
//adding separator
menu.addSeparator();
menuItem = new JMenuItem("Exit",KeyEvent.VK_X);
menuItem.addActionListener(this);
menu.add(menuItem);
//setting frame size
setSize(450,400);
//setting Background Image
//mp.setBounds(0,0,338,218);
int w = getWidth();
int h = getHeight();
mp.setBounds((w-338),(h-218),w,h);
cont.add(mp);
mp.main_cont();
System.out.println("Frame width: "+getWidth());
System.out.println("Frame height: "+getHeight());
System.out.println("Panel width: "+mp.getWidth());
System.out.println("Panel height: "+mp.getHeight());
System.out.println("Panel X: "+mp.getX());
System.out.println("Panel Y: "+mp.getY());
//setting windows listeners
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent evt)
{
System.out.println("Entered into ActionListener");
Object o = evt.getSource();
String s= ""+o;
if(s.indexOf("JMenuItem")>0)
{
s="";
JMenuItem source = (JMenuItem)(evt.getSource());
s = ""+source.getText();
}
//for File Menus
if("Change User/Login".compareToIgnoreCase(s)==0)
{
System.out.println("From Login");
}
else if("Exit".equals(s))
{
System.exit(1);
}
}
public static void main(String args[])
{
test ls=new test();
ls.setTitle("Phoneyinsta!!");
ls.setVisible(true);
}
}//phoneyinsta
class makePanel extends JPanel
{
JLabel background_image;
public makePanel()
{
super();
setLayout(null);
setBackground(new Color(0,0,0));
background_image = new JLabel(new ImageIcon("images/main_image.jpg"));
}
public void main_cont()
{
background_image = new JLabel(new ImageIcon("images/main_image.jpg"));
background_image.setBounds(0,0,338,218);
add(background_image);
}
}