我正在尝试创建一个在按下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("");
}
}
}
答案 0 :(得分: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("");
}
}
});