- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到的问题是,在这段代码中,如果我从中删除 JLabel,则该线根本不会绘制。我对其进行了测试,如果没有 JLabel 内容,paintComponent(Graphics g) 根本不会运行。我想知道这是为什么以及为什么 JLabel 对 JPanel 产生这样的影响。
我在网上读到,有时,如果处于无限循环中,paintComponent(Graphics g) 可能无法运行,但我仍然不明白 JLabel 将如何影响此特性。
我猜它可能与布局有关,但我不太明白布局如何影响这样的事情
MouseAction类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseAction extends JFrame{
private JPanel mousePanel;
private JLabel statusBar;
private GraphicsPanel g;
private int xS;
private int yS;
public MouseAction (){
super("Mouse example");
mousePanel = new JPanel();
statusBar = new JLabel("Deafault");
g = new GraphicsPanel(0,0,0,0);
add(mousePanel, BorderLayout.CENTER);
add(statusBar, BorderLayout.SOUTH);
MouseExploits handler = new MouseExploits();
mousePanel.addMouseListener(handler);
mousePanel.addMouseMotionListener(handler);
}
private class MouseExploits extends MouseAdapter{
public void mousePressed(MouseEvent mouse){
if(mouse.isMetaDown()){
xS= mouse.getX();
yS= mouse.getY();
}
}
public void mouseDragged(MouseEvent mouse){
if(!(mouse.isMetaDown() || mouse.isAltDown())){
g.updateCoordinates(mouse.getX(),mouse.getY(), xS,yS);
add(g);
System.out.printf("%d,%d\n",mouse.getX(),mouse.getY());
statusBar.setText(String.format("%d,%d",mouse.getX(),mouse.getY()));
}
}
}
}
GraphicsPanel类
import java.awt.*;
import javax.swing.*;
public class GraphicsPanel extends JPanel{
private int x;
private int y;
private int xS;
private int yS;
private Graphics dB;
private Image dBImage;
public graphics(int mouseX, int mouseY, int xSm, int ySm){
x = mouseX;
y = mouseY;
xS=xSm;
yS=ySm;
//System.out.println("Breaks2");
}
public void updateCoordinates(int mouseX, int mouseY, int xSm, int ySm){
x = mouseX;
y = mouseY;
xS=xSm;
yS=ySm;
//repaint();
}
public void paint(Graphics g){
dBImage = createImage(getWidth(),getHeight());
dB = dBImage.getGraphics();
paintComponent(dB);
g.drawImage(dBImage,0,0,this);
}
public void paintComponent(Graphics g){
//super.paintComponent(g);
System.out.println("Breaks3");
g.drawLine(xS,yS,x,y);
repaint();
}
}
MainProg 类
import javax.swing.JFrame;
public class MainProg {
public static void main(String args[]){
MouseAction frameObj= new MouseAction ();
frameObj.setSize(300,230);
frameObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameObj.setVisible(true);
}
}
编辑:代码现在包含双缓冲并经过修改,因此 GraphicsPanel 不会多次实例化,但无法在不破坏程序的情况下删除 JLabel 的问题仍然存在。
编辑2:在删除JLabel后对窗口进行了一些困惑之后,它似乎只有在我在屏幕上拖动、调整窗口大小然后再次在屏幕上拖动时才起作用。因此,似乎存在一些 jpanel 布局错误,其中的 jlabel 如何修复该错误。
最佳答案
代码的第一个问题是您在每个 mouseDragged
事件中实例化graphics
。这很奇怪而且很糟糕。您必须仅处理每个 mouseDragged
,存储坐标并在开始时创建并添加到界面的单个 graphics
实例上调用 repaint
。这将导致刷新。
其次,paintComponent
应该只制作绘图,因此您必须使用某种 (1) 双缓冲(创建离线 BufferedImage
及其关联的 >图形
,绘制到其中并在刷新时使用它),(2)在mouseDragged
中捕获Point
的集合,并使用它们来drawPolyline
在 paintComponent
方法中。
第三,不要在 paintComponent
内调用 repaint()
。这将不必要地以永无止境的循环方式触发对 paintComponent
的调用。
第四,不要在每次调用paint
时创建双缓冲区。每次刷新时,您都会创建一个新的图像
。创建一次,在您的 mouseDragged
方法中绘制它并从中触发 repaint()
,然后在 paintComponent 中创建一个
.drawImage
关于java - JPanel "paintComponent"没有 JLabel 就无法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30783539/
我是 Java 的新手。下面是两个类,一个类扩展JComponent,需要重写paintComponent方法。另一个类扩展了JPanel,同样需要重写paintComponent方法。 为什么Dra
我想知道 paintcomponent() 和 paintcomponents() 有什么不同? 当我从 paintcomponent 使用它时,它显示了我们想要的一切,但是 paintcompone
public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Gr
我稍后在我的主方法中使用这样的调用: public static void main(String[] args) { SwingUtilities.invokeLater(new Runna
所以我正在开发一款由两个网格组成的游戏。每个网格都是一个 JPanel,我在其中(可能)在网格中的每个空间上绘制一个对象。我发现绘制对象正在减慢应用程序的速度,我想知道是否有办法防止这种情况发生。 详
这个问题已经有答案了: paintComponent is not being called in JPanel (1 个回答) 已关闭 4 年前。 很抱歉,这个问题可能已经解决了,但我已经到处搜索但
我的问题非常简单:我有这个小程序,它只是没有显示我正在尝试制作的红框: 主要 public class Main { public static void main(String[] args
我正在尝试编写一个类似这样的应用程序: - 显示对话框 - 当用户单击“确定”时,关闭对话框,转到主应用程序 以下是相关代码片段: public class Owari extends JPanel
这应该是一个抽象类,可以扩展以绘制连接 JPanel 上某些坐标点的线。 但是,永远不会调用专门的 PaintComponents() 方法。我已经看到其他答案并尝试使用他们的解决方案,例如检查我是否
我有以下结构: method1 method2 ... methodn methodX 方法x包含: JFrame frame = new JFrame("Sample"); frame.setDef
我在使用矩形时遇到一些问题 这是我的代码: import java.awt.*; import javax.swing.*; import java.util.*; public class squa
我正在尝试制作西洋跳棋游戏,但以下架构没有在 JFrame 上显示我做错了什么 //基类 import java.awt.event.MouseEvent; import java.awt.event
问题涉及我项目中的 2 个类:主类和绘图类。主类创建 JFrame 并在其中放置 2 个 JPanel。第一个 JPanel 包含用于输入数字的 JTextFields 和用于选择不同选项的 JBut
我已经处理学校作业有一段时间了。但我无法真正理解我应该做什么。作业明天就要交了,我感觉压力很大。 任务是,我将获取一些图片,将它们放在窗口中,然后能够在它们周围移动并能够旋转。 最大的问题是我不知道如
我在使用 PaintComponent 绘制形状时遇到不清楚的问题。 public class Shape extends JPanel { private String shape; private
据我了解,Swing 将决定何时需要重新绘制,这可以解释为什么 paintComponent() 执行两次。但我制作了一个 hibernate 16 毫秒、重绘、 hibernate 16 毫秒、重绘
这是我的代码,它工作完美,并在 JFrame 中绘制形状。每个方法都通过其名称进行调用,在我的程序中我没有调用任何 paintComponent() 方法。那么paintComponent()方法的调
我正在尝试在 JPanel 上绘制一些内容,但似乎我遇到了 paint 和 paintComponent 方法的问题。可能与双缓冲有关,但我不确定。 public void PaintComponen
我是 Swing 新手,正在尝试将图像背景添加到我的 JFrame 中。但是我的 paintComponent 方法不起作用。您能否就如何修复我的代码以便在背景中绘制图像提供一些建议? 代码如下: /
我正在尝试编写简单的程序,当鼠标拖动时绘制圆圈,但paintComponent方法仅被调用一次(启动后)。 在这个类中,我有一些在拖动鼠标时绘制圆圈的方法。 import javax.swing.*;
我是一名优秀的程序员,十分优秀!