posted 24 years ago
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();
}
}