- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好。我想绘制一个外部组件(即属于不同框架的内容 Pane ),我们将其称为框架 B,位于框架 A 的组件内。
问题是,当我绘制组件时,它也会在框架 A 的内容 Pane 中绘制,当框架重新调整大小时,它也会闪烁或变得丑陋(即绘制很少的内容)次在组件内部,出现一些蓝色方 block 等)。例如,如果我尝试在绘制之前缩放或平移外部组件,则问题会变得更加明显。
我想,过了一会儿我就整理好了。但我对这个解决方案感觉不太好,出于某种原因,我相信可能有更好、更合适的解决方案。这里我需要你。 :)
这个问题更多的是要求解释为什么外部组件被错误地绘制,而不在组件内部绘制之前和之后操纵它的双缓冲功能。例如,使用任意一对setDoubleBuffered(false) 和 setDoubleBuffered(true)或者禁用DoubleBuffering(jP)和启用DoubleBuffering(jP)
分别在调用外部组件的paint方法之前和之后。
提前谢谢您。显示问题的 SSCCE 如下所示。
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.RepaintManager;
public class PaintForeignComponentSSCCE extends JFrame
{
public static void main(String[] args) throws IOException
{
//foreign panel
JPanel fp = new JPanel();
fp.setBackground(Color.PINK);
fp.setPreferredSize(new Dimension(200, 300));
//component in which the foreign panel is painted
ForeignComponentPainter fcp = new ForeignComponentPainter(fp);
fcp.setPreferredSize(new Dimension(600, 600));
//main frame's content
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.BLUE);
contentPane.add(fcp);
//main frame
JFrame f = new PaintForeignComponentSSCCE();
f.setContentPane(contentPane);
f.setSize(700, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
//foreign panel frame
JFrame fpf = new JFrame();
JPanel panelFrameContent = new JPanel();
panelFrameContent.add(fp);
fpf.setContentPane(panelFrameContent);
fpf.setSize(400, 400);
fpf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
fpf.setVisible(true);
}
}
class ForeignComponentPainter extends JButton
{
private static final long serialVersionUID = 1L;
private JPanel jP;
public ForeignComponentPainter(JPanel jP)
{
super();
this.jP = jP;
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawString("OIOI", 50, 50);
//g2.translate(100, 50);
//g2.scale(.5, .5);
// jP.setDoubleBuffered(false);
// disableDoubleBuffering(jP);
jP.paint(g2);
// jP.setDoubleBuffered(true);
// enableDoubleBuffering(jP);
//g2.scale(1/.5, 1/.5);
}
public static void disableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
public static void enableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
与 SSCCE 示例无关。 以下内容与问题本身无关。这段代码的目的是展示我如何在组件中实现 Printable,我也想以打印预览方式呈现该组件。 print调用组件的paint(如下所示)。
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if(pageIndex >= pageHeights.size())
return NO_SUCH_PAGE;
int savedPage = currentPageIndex;
currentPageIndex = pageIndex;
Graphics2D g2 = (Graphics2D) g;
paint(g2);
currentPageIndex = savedPage;
return PAGE_EXISTS;
}
最佳答案
抱歉,我无法直接回答您的问题,但通过对同一模型有两种不同的 View 也许可以避免该问题。如图How to Write a Document Listener ,可以更新多个view 文档
的。您的模型和 View 可能不同,但该概念仍然适用。
附录:
I think that is what I am doing at the moment, isn't it? I have one model i.e. the 'foreign' component, and two views one doing default paint and second custom paint.
不,您有一个 View 正在更新另一个 View ;您需要两个 View 来响应一个模型。此相关example可能会提供一些见解。
I am still interested in suggestions as to the reasons of this erroneous painting.
在我看来,将两个组件的更新交错的尝试从根本上来说是有缺陷的;它颠覆了Painting in Swing的正常流程。在我的平台上,我只看到非常短暂的闪烁,看不到任何偶然的绘画。尽管在一个系统上可能获得令人满意的结果,但这种安排在不同的实现中可能并不可靠。
Is a simple call to repaint of the label, placed in the
ModelObserver
'supdate()
method the appropriate solution?
是的,repaint()
,但应该在 ButtonHandler
中完成对于查看
:
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
PieceButton pb = (PieceButton) e.getSource();
icon.color = pb.piece.color;
label.repaint();
model.check(pb.piece);
}
}
关于java - 在另一个组件中绘制外部组件(即属于不同框架的内容 Pane ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5274962/
我正在尝试设置一个具有三个分割 Pane 的用户界面。前两个是垂直 Pane ,位于屏幕的左侧和右侧。每个拆分的一侧都有一个标题 Pane 。用户可以从这些 Pane 中选择项目以包含在中央 Pane
我经常使用 SQL Server Management Studio,并且始终使用以下 Pane 来处理数据:图表、条件和 SQL。 SQL Panes that I always use 目前,我每
可能是一个有点愚蠢的问题。问题很简单,我倾向于有两个 Pane ,一个用于编写代码,另一个用于编译它。我用 windows 终端 (cmd + vim + Mingw) 做到了这一点,但有些事情困扰着
我想出了如何使用 the Working with Map Panes tutorial 在自定义 Pane 中创建多边形和 this Stack Overflow question及其相关的 Fid
如何将输出连接到 paneWithList? PaneWithList 在其 JList 上有一个监听器,以便将所选行输出到控制台。如何将该输出定向到输出上的 JTextPane? PaneWithL
横向和纵向不同布局之间的方向变化似乎很容易,但是我找不到适合我当前情况的解决方案。 我想在横向模式下使用带有两个 fragment 的双 Pane 布局 res\layout-land\dashboa
我在Xcode中使用Swift编程时,当我想使用实用程序 Pane 编辑按钮时,我意识到它变成了“不适用”。属性检查器,连接检查器等全部变为“不适用”,仅快速帮助检查器仍显示。 我已选择 Storyb
我正在 JavaFX 中编写 vector Canvas ( vector 图形, Canvas 上仅显示形状):好吧,这样您就可以四处移动并放大它。 由于翻译时节点左上角的位置发生变化,并且不再响应
我有一个带有中心 Pane 的边框 Pane - VBox。VBox 包含一个 GridPane 和一个 VBox Pane 。 垂直框(垂直框内的垂直框)最初设置为不可见。 我想要做的是,如果将 G
我正在 JavaFX 中编写 vector Canvas ( vector 图形, Canvas 上仅显示形状):好吧,这样您就可以四处移动并放大它。 由于翻译时节点左上角的位置发生变化,并且不再响应
简单地说,我想让 java 做我想做的事情,但我无法理解布局管理中的任何其他自动调整大小到它感觉要做的事情。 我想要的是一个固定高度的“页脚”和顶部“主”区域,以根据窗口的高度自动调整大小。 水平方向
我尝试创建一个在堆栈 Pane 中包含 2 个 Pane 的应用程序。一个 Pane 是主 Pane 并居中,第二个 Pane 较小并停靠在舞台的左下角。 问题是我试过使用“setAlignment”
我已经在我的第一个 android 应用程序中实现了滑动 Pane 布局,但是当我滑动菜单时,操作栏仍然保持静态,我想实现像 Google+ 应用程序那样的东西 在 Stack Overflow 中也
我只是 Java 的初学者。这是我的问题: 我用这段代码创建了一个可滚动的文本字段(1): jZone_Text = new JTextPane(); scrollPane = new JScroll
我在左 Pane 中创建了一个文件夹,其中包含各种图像。它被称为“wikiImages”。如何访问此文件夹或获取此文件夹的路径? 我找到了 Bundle.main.resourcePath!,但这似乎
好吧,我是 JavaFx 的新手,而且我已经很长时间没有使用 Java 了,所以我遇到了很多问题。最大的问题是如何更改该死的 Pane 的背景。 下面是Controller类 //Styling pr
我正在使用 JavaFX 编写一个程序,其中我有 边框 Pane -> 中心 -> VBox -> 滚动 Pane -> 网格 Pane 我希望能够向网格 Pane 添加任意多的行,并让滚动 Pane
我已经尝试使这项工作 2 天了,并且阅读了许多示例和堆栈溢出问题。我是 html 和 css 的新手,这是我的第 3 天。对于我做错了什么的任何提示或见解,以及一般性评论或批评,我们将不胜感激。 我试
我有一个位于另一个 Pane 内的可拖动 Pane 。我想让子 Pane 只能在父 Pane 的边界内拖动,但默认情况下,子 Pane 可以拖动到任何地方。如何解决这个问题。 最佳答案 看看这个dem
我现在正在学习基本的 JavaFX,我不明白我正在阅读的书中的这句话:“不,像文本字段这样的节点只能添加到一个 Pane 中一次。添加一个节点多次或不同的 Pane 将导致运行时错误。”从书上提供的U
我是一名优秀的程序员,十分优秀!