0

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));
    }

}
Pawel
  • 417
  • 1
  • 6
  • 25
  • 1
    Make PrankPanel object as instance variable of escButton class and in and in the actionPerformed method just use panel.ok.setText("test"); – Balaji Krishnan May 03 '17 at 12:21
  • 1
    `if (source == "Nie")` doesnt work. Use if (source.equals("whatever")) instead. – GhostCat May 03 '17 at 12:22
  • And just for the record: comparing strings is absolute basic work. If you are struggling with such things, then I personally would recommend you to step back and do some learning on **basic** stuffs for a week or two. As you are probably overburdening yourself doing Swing UI work right now. – GhostCat May 03 '17 at 12:23
  • ok. now I get it. Thanks guys. – Pawel May 03 '17 at 12:42

0 Answers0