• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

moving labels

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic