Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,160 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,522 people online right now. Registration is fast and FREE... Join Now!




when setting the location of jlabel, nothing happened

 

when setting the location of jlabel, nothing happened, i tried using setlocation and assign a value still no effect. Please h

janine000

30 Mar, 2009 - 06:39 AM
Post #1

New D.I.C Head
*

Joined: 29 Mar, 2009
Posts: 2

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.net.MalformedURLException;
import java.net.URL;



public class FramePanelTask extends JFrame{
private Panel MyPanel;
private int pixelSize=41;
private int numberOfBlocks=14;
private JLabel label1;


public FramePanelTask ()
{
Container theContainer = this.getContentPane();
createpanel();
theContainer.add(this.MyPanel);
createicon();
MyPanel.add(this.label1);

this.setSize(this.MyPanel.getWidth(), this.MyPanel.getHeight());
this.setTitle("My Frame");
this.setResizable(true);
this.setVisible(true);

this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

pack();
}

private void createpanel(){

this.MyPanel = new Panel();
this.MyPanel.setLayout(null);
this.MyPanel.setBackground(Color.black);
int tempSize=pixelSize*numberOfBlocks;
this.MyPanel.setSize(tempSize, tempSize);
System.out.println("Panel width=" + this.MyPanel.getWidth() + " height=" + this.MyPanel.getHeight());

}

public void createicon(){
String IconPath = "src/images/brick.png";
ImageIcon icon=null;

File imagefile = new File(IconPath);
URL url = null;
try{
URL imageurl = imagefile.toURL();
if (imageurl != null){
icon = new ImageIcon(imageurl);
}
else{
System.err.println("File not found" + imageurl);
}
}
catch(MalformedURLException e){
System.out.println("exception caught " + e);
}
label1 = new JLabel(icon);
label1.setLayout(null);
label1.setLocation(50, 0);
label1.setSize(50, 40);
//label1.setLocation(this.MyPanel.getWidth() - 100, this.MyPanel.getHeight() - 50);

add(label1);
pack();

}

public static void main(String args[]){
FramePanelTask f = new FramePanelTask();
}
}




User is offlineProfile CardPM
+Quote Post


pansylea

RE: When Setting The Location Of Jlabel, Nothing Happened

30 Mar, 2009 - 07:30 AM
Post #2

New D.I.C Head
*

Joined: 29 Nov, 2008
Posts: 17



Thanked: 1 times
My Contributions
You didn't have a JPanel, thats why the JLabel wouldn't add to the Panel. Code below.... smile.gif

CODE
public class FramePanelTask extends JFrame{
      private Panel MyPanel;
      private int pixelSize=41;
      private int numberOfBlocks=14;
      private JLabel label1 = new JLabel("First Name");
      private JPanel p1;
  
       public FramePanelTask ()
      {
         p1 = new JPanel(new FlowLayout());
         Container theContainer = this.getContentPane();
         createpanel();
         theContainer.add(this.p1);
         createicon();
      
         p1.add(new JLabel("First Name"));
         setLayout(new BorderLayout());
         add(p1, BorderLayout.CENTER);
         this.setSize(this.p1.getWidth(), this.p1.getHeight());
         this.setTitle("My Frame");
         this.setResizable(true);
         this.setVisible(true);
      
         this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      
         pack();
      }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:56PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month