为什么 fillRect 命令不会出现在我的 JFrame 上?
Why won't the fillRect command show up on my JFrame?
所以我试图通过创建一个对象并将其添加到 JFrame 来在单击鼠标时绘制一个矩形。但是一旦命令是运行,它就不会出现了。有什么想法吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui3 extends JFrame {
private JPanel mousepanel;
private JLabel statusbar;
public Gui3(){
super("The title");
mousepanel = new JPanel();
mousepanel.setBackground(Color.WHITE);
add(mousepanel, BorderLayout.CENTER);
statusbar = new JLabel("Default");
add(statusbar, BorderLayout.SOUTH);
HandlerClass handler = new HandlerClass();
mousepanel.addMouseListener(handler);
mousepanel.addMouseMotionListener(handler);
}
private class HandlerClass implements MouseListener, MouseMotionListener
{
这就是问题所在。该程序及其所有方法都有效,只是绘制问题所在的矩形。绘制形状的对象在下面。
public void mouseClicked(MouseEvent event) {
statusbar.setText(String.format("Clicked at %d,%d",event.getX(),event.getY()));
DrawShapes shapes = new DrawShapes();
add(shapes);
}
public void mousePressed(MouseEvent event){
statusbar.setText("You pressed down the mouse");
}
public void mouseReleased(MouseEvent event){
statusbar.setText("You released the button");
}
public void mouseEntered(MouseEvent event){
statusbar.setText("You entered the area");
mousepanel.setBackground(Color.RED);
}
public void mouseExited(MouseEvent event){
statusbar.setText("The mouse has left the window");
mousepanel.setBackground(Color.WHITE);
}
//These are mouse motion events
public void mouseDragged(MouseEvent event){
statusbar.setText("You are dragging the mouse");
}
public void mouseMoved(MouseEvent event){
statusbar.setText("You are moving the mouse");
}
}
}
这是绘制矩形的对象
import java.awt.*;
import javax.swing.*;
public class DrawShapes extends JPanel {
public void PaintComponent(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0,0,30,30);
}
}
关于
public void PaintComponent(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0,0,30,30);
}
明白
PaintComponent != paintComponent
请务必使用 @Override
注释让您知道何时您正在或未覆盖您认为的方法。
正确的方法应该是这样的:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // don't forget this!
g.setColor(Color.BLUE);
g.fillRect(0, 0, 30, 30);
}
此外,如果您想替换原来的 JPanel,那么使用 CardLayout 可以帮助您轻松完成此操作。否则,您必须确保在容器中交换组件后自己调用 revalidate()
和 repaint()
。
例如,
@Override
public void mouseClicked(MouseEvent event) {
statusbar.setText(String.format("Clicked at %d,%d", event.getX(), event.getY()));
remove(mousepanel);
DrawShapes shapes = new DrawShapes();
getContentPane().add(shapes, BorderLayout.CENTER);
getContentPane().revalidate();
getContentPane().repaint();
}
所以我试图通过创建一个对象并将其添加到 JFrame 来在单击鼠标时绘制一个矩形。但是一旦命令是运行,它就不会出现了。有什么想法吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui3 extends JFrame {
private JPanel mousepanel;
private JLabel statusbar;
public Gui3(){
super("The title");
mousepanel = new JPanel();
mousepanel.setBackground(Color.WHITE);
add(mousepanel, BorderLayout.CENTER);
statusbar = new JLabel("Default");
add(statusbar, BorderLayout.SOUTH);
HandlerClass handler = new HandlerClass();
mousepanel.addMouseListener(handler);
mousepanel.addMouseMotionListener(handler);
}
private class HandlerClass implements MouseListener, MouseMotionListener
{
这就是问题所在。该程序及其所有方法都有效,只是绘制问题所在的矩形。绘制形状的对象在下面。
public void mouseClicked(MouseEvent event) {
statusbar.setText(String.format("Clicked at %d,%d",event.getX(),event.getY()));
DrawShapes shapes = new DrawShapes();
add(shapes);
}
public void mousePressed(MouseEvent event){
statusbar.setText("You pressed down the mouse");
}
public void mouseReleased(MouseEvent event){
statusbar.setText("You released the button");
}
public void mouseEntered(MouseEvent event){
statusbar.setText("You entered the area");
mousepanel.setBackground(Color.RED);
}
public void mouseExited(MouseEvent event){
statusbar.setText("The mouse has left the window");
mousepanel.setBackground(Color.WHITE);
}
//These are mouse motion events
public void mouseDragged(MouseEvent event){
statusbar.setText("You are dragging the mouse");
}
public void mouseMoved(MouseEvent event){
statusbar.setText("You are moving the mouse");
}
}
}
这是绘制矩形的对象
import java.awt.*;
import javax.swing.*;
public class DrawShapes extends JPanel {
public void PaintComponent(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0,0,30,30);
}
}
关于
public void PaintComponent(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0,0,30,30);
}
明白
PaintComponent != paintComponent
请务必使用 @Override
注释让您知道何时您正在或未覆盖您认为的方法。
正确的方法应该是这样的:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // don't forget this!
g.setColor(Color.BLUE);
g.fillRect(0, 0, 30, 30);
}
此外,如果您想替换原来的 JPanel,那么使用 CardLayout 可以帮助您轻松完成此操作。否则,您必须确保在容器中交换组件后自己调用 revalidate()
和 repaint()
。
例如,
@Override
public void mouseClicked(MouseEvent event) {
statusbar.setText(String.format("Clicked at %d,%d", event.getX(), event.getY()));
remove(mousepanel);
DrawShapes shapes = new DrawShapes();
getContentPane().add(shapes, BorderLayout.CENTER);
getContentPane().revalidate();
getContentPane().repaint();
}