Java JPanel的绘制背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1987976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Paint background of JPanel
提问by Jessy
How can I tell the paint method to draw background on JPanel only and not on the entire JFrame. My JFrame size is bigger than the JPanel. When I try to paint a grid background for the JPanel, the grid seems to be painted all over the JFrame instead of just the JPanel.
如何告诉paint方法仅在JPanel上而不是在整个JFrame上绘制背景。我的 JFrame 大小比 JPanel 大。当我尝试为 JPanel 绘制网格背景时,网格似乎被绘制在整个 JFrame 上,而不仅仅是 JPanel。
Here parts of the code:
这里部分代码:
public class Drawing extends JFrame {
JPanel drawingPanel;
...........
public Drawing (){
drawingPanel = new JPanel();
drawingPanel.setPreferredSize(new Dimension(600,600));
}
public void paint(Graphics g)
{
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
paintBackground(g2); //call a METHOD to paint the for JPANEL
}
private void paintBackground(Graphics2D g2)
{
g2.setPaint(Color.GRAY);
for (int i = 0; i < drawingPanel.getSize().width; i += 300)
{
Shape line = new Line2D.Float(i, 0, i, drawingPanel.getSize().height);
g2.draw(line);
}
for (int i = 0; i < drawingPanel.getSize().height; i += 300)
{
Shape line = new Line2D.Float(0, i, drawingPanel.getSize().width, i);
g2.draw(line);
}
} //END private void paintBackground(Graphics2D g2)
}
采纳答案by Thorsten S.
camickr is correct. So:
camickr 是正确的。所以:
public class Drawing extends JFrame {
JPanel drawingPanel;
...........
public Drawing (){
drawingPanel = new MyPanel();
drawingPanel.setPreferredSize(new Dimension(600,600));
add(drawingPanel);
}
}
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
myBackgroundRoutine(g2);
}
}
You need to strictly separate your drawing from different components. Swing is already managing subcomponents, so there is absolutely no need to implement drawings in your Panel in the Frame (calling paintComponents() is a severe error). And you should never override paint(), because only paintComponent() is used in Swing. Don't mix both until you absolutely know what you are doing.
您需要将您的绘图与不同的组件严格分开。Swing 已经在管理子组件,因此绝对不需要在 Frame 中的 Panel 中实现绘图(调用paintComponents() 是一个严重的错误)。并且您永远不应该覆盖paint(),因为在Swing 中只使用paintComponent()。在您完全清楚自己在做什么之前,不要将两者混用。
回答by Pool
super.paintComponents(g);
I would suggest as your first point of investigation.
我建议您作为调查的第一点。
回答by camickr
If you want to do painting on the JPanel then override the JPanel, not the JFrame.
如果您想在 JPanel 上进行绘画,请覆盖 JPanel,而不是 JFrame。
You should be overriding the paintComponent() method of JPanel. Read the section from the Swing tutorial on Custom Paintingfor a working example.
您应该覆盖JPanel 的paintComponent() 方法。阅读有关自定义绘画的 Swing 教程中的部分,以获取工作示例。
回答by user85421
The code you posted is not complete, it's missing how the panel is added to the JFrame and which LayoutManager is being used.
The code seams to be correct. Are you sure the JPanel is not occupying the whole JFrame? Add a System.out.println(drawingPanel.getSize())
to check this.
If you are using the BorderLayout, the default for JFrame, and has just added the panel without any constraint, the panel willuse the whole area. The PreferredSize is ignored.
Try this, just for testing:
您发布的代码不完整,缺少将面板添加到 JFrame 的方式以及正在使用的 LayoutManager。
代码接缝是正确的。您确定 JPanel 没有占用整个 JFrame 吗?添加一个System.out.println(drawingPanel.getSize())
来检查这个。
如果您使用的是 BorderLayout(JFrame 的默认设置),并且刚刚添加了没有任何约束的面板,则面板将使用整个区域。PreferredSize 被忽略。
试试这个,只是为了测试:
public Drawing (){
drawingPanel = new JPanel();
drawingPanel.setPreferredSize(new Dimension(600,600)); // ignored
drawingPanel.setBounds(0, 0, 600, 600); // location and size
setLayout(null);
add(drawingPanel);
}
but IMO this is not the best or correctway to do it. I would prefer to override the paintComponent()
method from the JPanel, as suggested by Thorsten and camickr.
But it will still use the whole area of the JFrame until other Component is added to the JFrame or the LayoutManager changed.
但 IMO 这不是最好或正确的方法。我更愿意按照paintComponent()
Thorsten 和 camickr 的建议覆盖JPanel 中的方法。
但它仍然会使用 JFrame 的整个区域,直到其他 Component 添加到 JFrame 或 LayoutManager 更改。
回答by CuriosMan
You should override the JPanel, not the JFrame to do painting. You can override the paintComponent() method of the JPanel
您应该覆盖 JPanel,而不是 JFrame 来进行绘画。您可以覆盖 JPanel 的paintComponent() 方法