这是我在StackOverflow上的第一篇文章。我是Java的初学者,我最近正在阅读Head First Java。我已经多次搜索谷歌,但我仍然无法找到答案来解决我的疑问。 在第12章,我将代码复制到Eclipse。我的代码是可执行的,但在我点击按钮更改圆圈的颜色后,窗口上没有显示任何圆圈。另一类“SimpleAnimation”也有同样的问题。窗口上没有任何圆圈。它困扰了我两天。请帮助这个可怜的孩子(TAT)。谢谢! Run TwoButtons 这是代码。 这是ClassButtons类:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class TwoButtons {
JFrame frame;
JLabel label;
public static void main(String[] args) {
TwoButtons gui = new TwoButtons();
gui.go();
}
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton labelButton = new JButton("Change label");
labelButton.addActionListener(new LabelListener());
JButton colorButton = new JButton("Change circle");
colorButton.addActionListener(new ColorListener());
label = new JLabel("I'm a label");
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.EAST, labelButton);
frame.getContentPane().add(BorderLayout.WEST, label);
frame.setSize(500, 500);
frame.setVisible(true);
}
class LabelListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
label.setText("Ouch!");
}
}
class ColorListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
}
这是MydrawPanel类:
import javax.swing.JPanel;
import java.awt.*;
public class MyDrawPanel extends JPanel {
public void paintConponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
Color startColor = new Color(red, green, blue);
red = (int) (Math.random() * 255);
green = (int) (Math.random() * 255);
blue = (int) (Math.random() * 255);
Color endColor = new Color(red, green, blue);
GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
}
Run SimpleAnimation 这是SimpleAnimation类:
import javax.swing.*;
import java.awt.*;
public class SimpleAnimation {
int x = 70;
int y = 70;
public static void main(String[] args) {
SimpleAnimation gui = new SimpleAnimation();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
for(int i = 0; i < 130; i++) {
x++;
y++;
drawPanel.repaint();
try {
Thread.sleep(50);
} catch(Exception ex) {
}
}
}
class MyDrawPanel extends JPanel{
public void paintConponent(Graphics g) {
g.setColor(Color.green);
g.fillOval(x, y, 40, 40);
}
}
}
再次感谢!
答案 0 :(得分:3)
首先:
public void paintConponent(Graphics g) {
应该是:
@Override
public void paintComponent(Graphics g) {
更改方法时始终使用@Override
表示法,以确保方法名称拼写正确并使用正确的方法参数。或者换句话说,使用编译器标志来检查代码是否实际覆盖了父方法,而不是定义一个新方法!
任何自定义绘制的组件都应该:
super
方法,以确保删除较旧的颜料并且组件的BG颜色(等)被涂上颜色。getPreferredSize()
方法以向布局管理器提供提示。答案 1 :(得分:0)
您尚未覆盖paintComponent
方法。名字上有一个拼写错误。请注意M
中的paintComponent
。在每种情况下,您的方法都被命名为paintConponent
。
如果您在方法上使用@Override
注释,如果您尝试从超类覆盖方法但是方法签名错误(名称或数量和参数类型),编译器将发出警告:< / p>
@Override
public void paintComponent(Graphics g) {
// . . .
}
答案 2 :(得分:-1)
你错过了年轮覆盖
bool sucess;
sucess = car.Select(p => p.definition.Contains(checkingList)).FirstorDefault();
sucess = car.Select(p => p.Where(o => o.definition.Contains(checkingList)).FirstorDefault();
sucess = car.Select(p => p.Where(o => o.definition.ForEach.Contains(checkingList)).FirstorDefault();