Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

moving labels

 
karan roy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic