- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的程序(其中大部分将用于其他用途),它只在用户单击 drawPanel JPanel 并显示椭圆时绘制椭圆。问题是 repaint() 方法没有调用 paintComponent()。这是为什么?
代码如下:
// Imports Used:
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.geom.*;
// Class DrawPolygon
public class DrawPolygon extends JPanel implements MouseListener
{
// Variables used in GUI
static JFrame frame;
static JPanel drawPanel;
static JPanel labelPanel;
static JPanel primaryPanel;
static JButton loadButton;
static JButton saveButton;
static JButton clearButton;
static JButton addButton;
static JButton moveButton;
static JButton exitButton;
// Variables used for GUI interaction
static final int SIZE = 6;
static int numVertices;
ArrayList<Point> vertices;
static Color outlineColor = Color.RED;
static int lineWidth = 10;
static Color fillColor = Color.BLACK;
// Constructor for new Polygon Drawing Application
public DrawPolygon()
{
// Create Frame
frame = new JFrame("Vector Painter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set Location of GUI on screen
frame.setLocation(225,100);
// Primary Panel to store Draw Panel and Lable/Button Panel
primaryPanel = new JPanel();
// Create Label Panel
createLabelPanel();
// Create Draw Panel
createDrawPanel();
// Add panels to Primary
primaryPanel.add(labelPanel);
primaryPanel.add(drawPanel);
// Add to frame
frame.getContentPane().add(primaryPanel);
frame.pack();
frame.setVisible(true);
}
// Add Buttons to left side
public void createLabelPanel()
{
// Label Panel to add Buttons
labelPanel = new JPanel();
labelPanel.setBackground(Color.BLACK);
labelPanel.setPreferredSize(new Dimension(200,600));
// Create JButtons
loadButton = new JButton("LOAD");
loadButton.setPreferredSize(new Dimension(180,75));
loadButton.setBackground(Color.BLACK);
loadButton.setForeground(Color.WHITE);
loadButton.addMouseListener(this);
saveButton = new JButton("SAVE");
saveButton.setPreferredSize(new Dimension(180,75));
saveButton.setBackground(Color.BLACK);
saveButton.setForeground(Color.WHITE);
saveButton.addMouseListener(this);
clearButton = new JButton("CLEAR");
clearButton.setPreferredSize(new Dimension(180,75));
clearButton.setBackground(Color.BLACK);
clearButton.setForeground(Color.WHITE);
clearButton.addMouseListener(this);
addButton = new JButton("ADD");
addButton.setPreferredSize(new Dimension(180,75));
addButton.setBackground(Color.BLACK);
addButton.setForeground(Color.WHITE);
addButton.addMouseListener(this);
moveButton = new JButton("MOVE");
moveButton.setPreferredSize(new Dimension(180,75));
moveButton.setBackground(Color.BLACK);
moveButton.setForeground(Color.WHITE);
moveButton.addMouseListener(this);
exitButton = new JButton("EXIT");
exitButton.setPreferredSize(new Dimension(180,75));
exitButton.setBackground(Color.BLACK);
exitButton.setForeground(Color.WHITE);
exitButton.addMouseListener(this);
// Add Buttons to Label Panel
labelPanel.add(loadButton);
labelPanel.add(saveButton);
labelPanel.add(clearButton);
labelPanel.add(addButton);
labelPanel.add(moveButton);
labelPanel.add(exitButton);
}
// Creates Draw Panel
public void createDrawPanel()
{
// Draw Panel to Draw Polygons
drawPanel = new JPanel();
drawPanel.setBackground(Color.BLACK);
drawPanel.setPreferredSize(new Dimension(600,600));
drawPanel.addMouseListener(this);
vertices = new ArrayList<>();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.ORANGE);
for (Point spot : vertices)
{
g.fillOval(spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2);
}
g.drawString("Count: " + vertices.size(), 5, 15);
System.out.println("repaint is working?");
}
// Execute when Load button is clicked
public void loadButton()
{
System.out.println("Load Button CLICKED!!");
}
// Execute when Save button is clicked
public void saveButton()
{
System.out.println("Save Button CLICKED!!");
}
// Execute when Clear button is clicked
public void clearButton()
{
System.out.println("Clear Button CLICKED!!");
}
// Execute when Add button is clicked
public void addButton()
{
System.out.println("Add Button CLICKED!!");
}
// Execute when Move button is clicked
public void moveButton()
{
System.out.println("Move Button CLICKED!!");
}
public void mouseClicked(MouseEvent e)
{
if (e.getSource() == loadButton)
{
loadButton();
}
else if (e.getSource() == saveButton)
{
saveButton();
}
else if (e.getSource() == clearButton)
{
clearButton();
}
else if (e.getSource() == addButton)
{
addButton();
}
else if (e.getSource() == moveButton)
{
moveButton();
}
else if (e.getSource() == exitButton)
{
System.exit(0);
}
else if (e.getSource() == drawPanel)
{
System.out.println("TEST");
vertices.add(e.getPoint());
repaint();
}
}
// These are here because program wouldn't compile without them
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
// Main Function
public static void main(String[] args)
{
// Create Frame
new DrawPolygon();
}
}
最佳答案
实际上正在调用重绘,但是在当前的 DrawPolygon 对象上,但是由于您似乎没有显示其中一个 paintComponent(...)
永远不会被调用。澄清一下,您当前的类 DrawPolygon 扩展了 JPanel,但它似乎并未添加到作为 GUI 层次结构一部分的任何容器中。也许您想将当前对象(this
)添加到 GUI 的某处?事实上,您可能应该考虑使用当前对象代替 drawPanel JPanel 对象。我会考虑完全删除该变量。
同样与您的问题无关的是,您几乎不想在应该使用 ActionListeners 的 JButtons 上使用 MouseListeners。
关于java - repaint() 未在 Java 程序中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12999030/
我每秒收到数百个事件,持续几秒钟,每个事件都会更新我的模型。如果我在每个事件之后在 invokeLater() 内部调用 repaint() ,那么 repaint 每秒会被调用数百次吗?它是否足够聪
我在类collection中有一个循环,它repaint() JPanel连续 while(loop) { imageicon = (ImageIcon) ois.readOb
我仍在尝试让一个 repaint() 方法在一个单独的类中工作,该类具有一个扩展 JComponent 的类。我已经在这里发布了几篇文章,但到目前为止我还无法让代码正常工作。我得到了一些很好的建议。我
谁能解释一下JPanel.repaint() 方法和JFrame.repaint() 方法之间的区别,我想两者都调用了paintComponent() JPanel 中的方法。 请说明,谢谢 最佳答案
在repaint(long maxDelay)(来自java.awt.Component)中,maxDelay指定之前可以经过的最大毫秒数调用update。 普通的repaint()有这样的最大值吗?
这个问题是基于我不久前遇到的一个简单的 Swing 骰子程序问题。我发布的原始问题是 here并且有一个可接受的答案,但我想确切地知道发生了什么,为什么会出现问题,以及解决方案为什么有效。 我设法削减
我正在为错误程序编写一个插件,并且在使用 repaint() 方法时遇到问题。 简短的问题:有什么方法可以在 JPanel 重绘完成后立即获得通知或与其同步代码吗? 详细版本: 我的程序可以将 xy
我在java中使用2个类class1和class2,它们都位于不同的包中。 class2 中定义了一个 paintComponent() 和一个 test()。我按以下顺序从 class1 调用这些方
Full project here https://github.com/jafetrd/easyImageEditor 我正在尝试使用paintComponent()将颜色应用于图像,这是我使用的类
我正在尝试制作一个 JFrame 程序,将二进制-十进制-十六进制转换器与按钮连接起来。其中一部分,我想绘制圆圈来表示一排圆圈中的二进制数,其中实心圆圈代表一,空心圆圈代表零。但是,当我尝试调用 re
我两周前才开始学习java,所以我对java还不太了解。我正在尝试让一个球在框架内弹跳或移动。但当我在线程中运行它时,它不会重新绘制/更新,但如果我使用 while 循环或计时器,它就可以正常工作,我
我正在尝试制作一款三消游戏。我试图为实际发生的情况创建一些视觉辅助,首先将需要删除的 gem 标记为“黑色”,然后让重力完成它的工作。我正在努力做到这一点,在将它们标记为“黑色”后,我调用了 repa
我正在编写一个 Java 程序,它将根据按下的按钮绘制圆形或矩形。虽然它确实绘制了给定的形状,但在绘制时它会在窗口的左上角(最有可能是(0,0))创建新按钮。我是否违反了paint()/repaint
我正在为学校项目开发一款游戏,一款类似炸弹人的游戏。 我正在使用 swing,并且使用 Canvas 来绘制图形,但 KeyListener 无法正常工作,因此我退出使用 Canvas 并开始使用 p
嘿,我正在尝试学习如何在java中使用图形,并且正在编写一个非常简单的代码,该代码获取图像并将其在窗口中成一条线移动,当它到达窗口边缘时,它会向下移动几个像素并从另一端开始。 我的问题是我的程序在重新
我试图了解 repaint 和 paintComponents 在 Java Swing 中如何工作,并想知道为什么这个程序在执行时只显示“hello”。 class MyLabel extends
我无法让我的重绘方法在我的 SimonPanel 类中工作。起初,我认为这是因为我使用了paint()而不是paintComponent(),但这似乎并没有解决问题。我的 SimonShape.jav
这就是我的任务。 我必须生成 4 张随机卡。之后,当按下“刷新”按钮时,卡片应再次随机化。我已经像这样实现了 repaint() 方法,但它不会改变卡片的显示方式。 public class Four
我看了很多答案,但仍然找不到解决方案。我有一个 JFrame 和两个 JPanel。我想在按下按钮时删除第一个面板并将其替换为第二个面板,但 repaint() 方法不会刷新框架。请帮忙。 这是我的框
我在这里尝试做的是从我的 loadbg() 方法中调用 repaint() 方法。但是,repaint() 不起作用,并且我的图像(存储在 var bg 中)不会加载到屏幕上。关于为什么这行不通的任何
我是一名优秀的程序员,十分优秀!