I wrothe this application in java that makes one of the buttons escape from the coursor. The app works just fine, but I'd like to add another functionallity: changing "ok" button text to "Test" when button "nie" is clicked. I just cant figure out how to do that. Please help. here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class escButton extends JFrame implements ActionListener {
public escButton(){
super("Wybierz odpowiedź");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
BorderLayout border = new BorderLayout();
setLayout(border);
JLabel msg = new JLabel("Some Question");
add(BorderLayout.NORTH, msg);
PrankPanel panel = new PrankPanel();
add(BorderLayout.CENTER, panel);
panel.ok.addActionListener(this);
panel.nie.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String source = event.getActionCommand();
if (source == "Nie") {
this.setVisible(true);
JOptionPane.showMessageDialog(null,"Zgadza się!", "Odpowiedź",
JOptionPane.INFORMATION_MESSAGE);
setTitle("abcd");
ok.setText("test");
}
else {
System.exit(0);
}
}
public static void main(String[] args) {
new escButton();
}
}
class PrankPanel extends JPanel implements MouseMotionListener{
public JButton ok = new JButton("Tak");
JButton nie = new JButton("Nie");
int buttonX, buttonY, mouseX, mouseY ;
int width, height;
public PrankPanel(){
super();
setLayout(null);
addMouseMotionListener(this);
buttonX = 110;
buttonY = 110;
ok.setBounds(new Rectangle(buttonX, buttonY, 70, 20));
nie.setBounds(new Rectangle (200,110,70,20));
add(ok);
add(nie);
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
width = (int) getSize().getWidth();
height = (int) getSize().getHeight();
if (Math.abs((mouseX)- buttonX) < 50){
buttonX = moveButton(mouseX,buttonX,width);
repaint();
}
if (Math.abs((mouseY )- buttonY) < 50){
buttonY = moveButton(mouseY,buttonY,height);
repaint();
}
}
private int moveButton(int mouseAt, int buttonAt, int bord){
if (buttonAt < mouseAt) {
buttonAt-=10;
}
else {
buttonAt+=10;
}
if (buttonAt > (bord - 20) ) {
buttonAt = 10;
}
if (buttonAt < 0) {
buttonAt = bord - 100;
}
return buttonAt;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
ok.setBounds(new Rectangle(buttonX, buttonY, 70, 20));
}
}