【发布时间】:2017-08-22 07:00:40
【问题描述】:
请帮忙。我对编码并不陌生,但对 java 很陌生。我不知道我做错了什么。我正在用一本书来学习java,下面的代码是我目前正在做的。我去了书籍网站并下载了这个程序的源代码,它给了我同样的错误信息。有人请帮忙。我知道有人问过这个问题,但我被困住了,真的可以使用一些帮助。
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class TitleBar extends JFrame implements ActionListener {
JButton b1;
JButton b2;
public TitleBar() {
super("Title Bar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
b1 = new JButton("Rosencrantz");
b2 = new JButton("Guildenstern");
b1.addActionListener(this);
b2.addActionListener(this);
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(b1);
add(b2);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == b1) {
setTitle("Rosencrantz");
} else if (source == b2) {
setTitle("Guildenstern");
}
repaint();
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception exc) {
System.err.println("Couldn't use the system "
+ "look and feel: " + exc);
}
}
public static void main(String[] arguments) {
TitleBar frame = new TitleBar();
}
}
【问题讨论】:
-
请分享您的错误信息
-
你需要学习基本的java、swing和awt。
-
您的代码没有给出任何错误
-
对我来说工作正常,请分享完整的错误消息(stacktrace),它给我们提示问题的确切位置。如果你有额外的代码,你也可以分享它,也许错误就在那里。
-
尝试只实现接口,然后让 ide(eclipse) 自动继承方法,有时 eclipse 的行为会很奇怪。或者尝试清理项目,这可能会起作用。
标签: java actionlistener