更改其他课程

时间:2017-05-03 12:13:28

标签: java swing

我在java中使用这个应用程序,使其中一个按钮从游标中逃脱。该应用程序工作正常,但我想添加另一个功能: 改变" ok"按钮文本到"测试"当按钮" nie"点击。 我只是想弄清楚如何做到这一点。 请帮忙。 这是代码:

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

}

0 个答案:

没有答案