mark bernard

Greenhorn
+ Follow
since Dec 27, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by mark bernard

You have to use the method getModifiers():
public void mousePressed(MouseEvent e) {
if(e.getModifiers()==InputEvent.BUTTON1_MASK) {
System.out.println("Left button");
}
if(e.getModifiers()==InputEvent.BUTTON2_MASK) {
System.out.println("Center button");
}
if(e.getModifiers()==InputEvent.BUTTON3_MASK) {
System.out.println("Right button");
}
}
23 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();
}
}
23 years ago