[英]How to Make Text Appear When Pressing a JButton
I am trying to make an application that displays text when a JButton is pressed, but I can't figure out how, here's my code. 我正在尝试创建一个在按下JButton时显示文本的应用程序,但是我不知道怎么做,这是我的代码。 It's an application to help someone cheer up.
这是一个可以帮助某人振作的应用程序。 I am using Eclipse if that helps.
如果有帮助,我正在使用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("");
}
}
}
Remove btnNewButton.addActionListener((ActionListener) this);
删除
btnNewButton.addActionListener((ActionListener) this);
and try 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.