这是我在StackOverflow上的第一篇文章。我是Java的初学者,最近正在阅读Head First Java。我已经搜索过Google了很多次,但是仍然找不到答案来解决我的疑问。
在第12章,我将代码复制到Eclipse。我的代码是可执行的,但是单击按钮更改圆圈的颜色后,窗口上没有任何圆圈。而另一个类“ SimpleAnimation”也有同样的问题。窗口上没有任何圆圈。它困扰了我两天。请帮助这个可怜的孩子。谢谢!
Run TwoButtons
这是代码。
这是TwoButtons类:
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);
}
}
}
再次感谢!
最佳答案
对于初学者:
public void paintConponent(Graphics g) {
应该:
@Override
public void paintComponent(Graphics g) {
更改方法时,请始终使用
@Override
表示法,以确保正确拼写方法名称并使用正确的方法参数。或者换种说法,使用编译器标志检查代码是否实际上覆盖了父方法,而不是定义新方法!其他技巧
任何定制的涂漆组件应:
在自定义绘画之前调用
super
方法,以确保删除较旧的绘画并绘制组件的BG颜色(等)。覆盖
getPreferredSize()
方法以向布局管理器提供提示。关于java - 为什么JPanel的子类“TwoButtons”没有显示在屏幕上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50543961/