我正在尝试制作一个在按下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("");
}
}
});