1

我正在尝试制作一个在按下 JButton 时显示文本的应用程序,但我不知道如何,这是我的代码。这是一个帮助某人振作起来的应用程序。如果有帮助,我正在使用 Eclipse。

public LoveYou() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(200, 200, 900, 600);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(200, 200, 200, 200));
    contentPane.setLayout(new BorderLayout(5, 5));
    setContentPane(contentPane);

    JButton btnNewButton = new JButton("Feeling down? Press me.");
    btnNewButton.setActionCommand("enter");
    btnNewButton.addActionListener((ActionListener) this);
    btnNewButton.setBackground(Color.green);
    btnNewButton.setForeground(new Color(30, 144, 255));
    contentPane.add(btnNewButton, BorderLayout.CENTER);

    public void onActionPerformed(ActionEvent e; {
        boolean isClicked = false;
        if(!isClicked){
             isClicked = true;
           System.out.println("Someone loves YOU!");
          }
        else {
            System.out.println("");
     }
    }
}    
4

1 回答 1

0

删除btnNewButton.addActionListener((ActionListener) this);并试试这个:

    btnNewButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            boolean isClicked = false;
            if (!isClicked) {
                isClicked = true;
                System.out.println("Someone loves YOU!");
            } else {
                System.out.println("");
            }

        }
    });
于 2013-09-27T13:23:56.407 回答