簡體   English   中英

按下JButton時如何顯示文本

[英]How to Make Text Appear When Pressing a JButton

我正在嘗試創建一個在按下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("");
     }
    }
}    

刪除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("");
            }

        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM