Java 在 Jframe 上绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2134840/
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
drawing on Jframe
提问by n0ob
I cannot get this oval to draw on the JFrame.
我无法在 JFrame 上绘制此椭圆形。
static JFrame frame = new JFrame("New Frame");
public static void main(String[] args) {
makeframe();
paint(10,10,30,30);
}
//make frame
public static void makeframe(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(375, 300));
frame.getContentPane().add(emptyLabel , BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
// draw oval
public static void paint(int x,int y,int XSIZE,int YSIZE) {
Graphics g = frame.getGraphics();
g.setColor(Color.red);
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
The frame displays but nothing is drawn in it. What am I doing wrong here?
框架显示但没有绘制任何内容。我在这里做错了什么?
采纳答案by Vincent Ramdhanie
You have created a static method that does not override the paint method. Now others have already pointed out that you need to override paintComponent etc. But for a quick fix you need to do this:
您已经创建了一个不会覆盖 Paint 方法的静态方法。现在其他人已经指出你需要覆盖paintComponent等。但是为了快速修复你需要这样做:
public class MyFrame extends JFrame {
public MyFrame() {
super("My Frame");
// You can set the content pane of the frame to your custom class.
setContentPane(new DrawPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
// Create a component that you can actually draw on.
class DrawPane extends JPanel {
public void paintComponent(Graphics g) {
g.fillRect(20, 20, 100, 200); // Draw on g here e.g.
}
}
public static void main(String args[]){
new MyFrame();
}
}
However, as someone else pointed out...drawing on a JFrame is very tricky. Better to draw on a JPanel.
但是,正如其他人指出的那样……在 JFrame 上绘图非常棘手。最好在 JPanel 上绘图。
回答by Jason Nichols
Several items come to mind:
想到几个项目:
- Never override paint(), do paintComponent() instead
- Why are you drawing on a JFrame directly? Why not extends JComponent (or JPanel) and draw on that instead? it provides more flexibility
- What's the purpose of that JLabel? If it sits on top of the JFrame and covers the entire thing then your painting will be hidden behind the label.
- The painting code shouldn't rely on the x,y values passed in paint() to determine the drawing routine's start point. paint() is used to paint a section of the component. Draw the oval on the canvas where you want it.
- 永远不要覆盖paint(),而是使用paintComponent()
- 为什么要直接在 JFrame 上绘图?为什么不扩展 JComponent(或 JPanel)并利用它呢?它提供了更多的灵活性
- 那个 JLabel 的目的是什么?如果它位于 JFrame 的顶部并覆盖整个事物,那么您的绘画将隐藏在标签后面。
- 绘制代码不应依赖于paint() 中传递的x,y 值来确定绘制例程的起点。Paint() 用于绘制组件的一部分。在画布上您想要的位置绘制椭圆。
Also, you're not seeing the JLabel because the paint() method is responsible for drawing the component itself as well as child components. Overriding paint() is evil =)
此外,您没有看到 JLabel,因为paint() 方法负责绘制组件本身以及子组件。覆盖paint()是邪恶的=)
回答by Omar Al Kababji
You are overriding the wrong paint() method, you should override the method named paintComponentlike this:
您覆盖了错误的paint() 方法,您应该覆盖名为paintComponent的方法,如下所示:
@Override
public void paintComponent(Graphics g)
回答by Martin Kraj?írovi?
You need to Override an exist paint method that actually up to dates your Frame. In your case you just created your custom new method that is not called by Frame default.
您需要覆盖实际更新您的框架的现有绘制方法。在您的情况下,您刚刚创建了 Frame 默认未调用的自定义新方法。
So change your method to this:
因此,将您的方法更改为:
public void paint(Graphics g){
}