File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes moving labels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "moving labels" Watch "moving labels" New topic
Author

moving labels

karan roy
Greenhorn

Joined: Dec 09, 2000
Posts: 13
hi
i am making an applet in which i have made a label(an animated label)and have made 4 buttons
top
left
right
bottom
now,using event handling ,i want this label to move likewise when i click on these buttons.
just tell me a method with the help of which i can move a label
inside an applet
mark bernard
Greenhorn

Joined: Dec 27, 2000
Posts: 2
I almost got it working with the following code. It only works if you resize the window after a button is pressed. I just couldn't figure out how to get the screen to update automatically. Maybe someone here knows how to get the screen to update. I will be looking it up myself, so if I find it I will post the answer.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Move extends JApplet implements ActionListener {
JButton top,bottom,left,right;
JLabel l;
JPanel p;
Container c;

public void init() {
c=getContentPane();
top=new JButton("Top");
bottom=new JButton("Bottom");
left=new JButton("Left");
right=new JButton("Right");
top.addActionListener(this);
bottom.addActionListener(this);
left.addActionListener(this);
right.addActionListener(this);
l=new JLabel("Moving label",SwingConstants.CENTER);
p=new JPanel();
c.setLayout(new BorderLayout());
p.setLayout(new BorderLayout());
p.add(l,BorderLayout.CENTER);
c.add(p,BorderLayout.CENTER);
c.add(top,BorderLayout.NORTH);
c.add(bottom,BorderLayout.SOUTH);
c.add(left,BorderLayout.WEST);
c.add(right,BorderLayout.EAST);
}

public void actionPerformed(ActionEvent e) {
p.remove(l);
if(e.getActionCommand().compareTo("Top")==0) {
p.add(l,BorderLayout.NORTH);
}
if(e.getActionCommand().compareTo("Bottom")==0) {
p.add(l,BorderLayout.SOUTH);
}
if(e.getActionCommand().compareTo("Left")==0) {
p.add(l,BorderLayout.WEST);
}
if(e.getActionCommand().compareTo("Right")==0) {
p.add(l,BorderLayout.EAST);
}
l.repaint();
p.repaint();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: moving labels