- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习如何在java中绘制椭圆形,但是我制作的paintComponent没有被任何东西调用,并且尝试调用它只会导致更多问题。
程序运行成功,但我想要显示的图像没有显示。
import java.awt.*;
import javax.swing.*;
public class TEST2{
public void paintComponent(Graphics g){
g.drawOval(70, 70, 100, 100);
}
public static void main(String[] args) {
TEST2 gui = new TEST2();
gui.setUpFrame();
}
public void setUpFrame(){
JFrame frame = new JFrame();
frame.setTitle("Images should be in this program");
frame.setSize(600,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
最佳答案
首先查看 Painting in AWT and Swing和 Performing Custom Painting
为了能够在 Swing 中执行自定义绘制,您必须...
JComponent
或 JPanel
)paintComponent
方法并在此方法中执行自定义绘制。JFrame
)您应该确保在进行任何自定义绘画之前调用super.paintComponent
为了确保您不会犯任何(常见)错误,您应该使用 @Override
注释
举个例子...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test2 extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(70, 70, 100, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame();
frame.setTitle("Images should be in this program");
frame.add(new Test2());
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
关于Java:paintComponent() Oval 未出现在 Netbeans 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30294509/
如果你正在上课 public class object { @NotNull @NotEmpty @Length(max=32) private String name; }
@ValidateWithMethod(methodName = "isValidPostalCode", parameterType = String.class) private String _
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) Null Pointer Exception on
我正在尝试学习如何在java中绘制椭圆形,但是我制作的paintComponent没有被任何东西调用,并且尝试调用它只会导致更多问题。 程序运行成功,但我想要显示的图像没有显示。 import jav
首先,我知道已经回答了非常类似的问题,但我还没有看到特别提出这个问题。 我有一个椭圆形和一个矩形,我想检测它们是否相交。我知道你可以用两个矩形来做到这一点,如下所示: if (new Rectangl
我有一个由多人开发的 Java Maven 项目。 因为我真的很喜欢做 JUnit 测试等,OVal 的概念让我很感兴趣,因为我可以编写如下代码: @NotNull @NotEmpty @Length
美好的一天。我在 Spring Boot 1.3 上有带有 net.sf.oval 1.85 的应用程序。我的模型: @Entity @Table(name = "company") public c
我正在尝试创建可绘制资源椭圆形 com 矩形形状?我想要下面的形状 但我得到以下信息: 我在用什么: 如何创建我想要的形状?提前谢谢你 最佳答案 试试这个椭圆形的。
使用 OVal 验证框架 (http://oval.sourceforge.net/),可以创建自定义注释或基于 XML 的约束 (http://oval.sourceforge.net/usergu
var qcanvas = $('#canvas'); var canvas = ctl_canvas[0]; var context = canvas.getContext('2d'); qcanv
我正在尝试定义一个圆形背景以用作按钮的背景。 问题是,如果按钮不是完全正方形,则形状会拉伸(stretch)成椭圆形(椭球体),这是不受欢迎的。我可以使用 标
我使用 Java OVal 框架来验证我的 java bean,但该 bean 的一个字段是 int 类型 @NotEmpty 或 @NotNull 不起作用,我需要验证它不等于使用简单的 OVal
我导入了: import javax.swing.ImageIcon; 我使用这段代码导入了 PNG 文件。 (我知道我可以很容易地创建一个黑色方 block ,但我想要的是将任何图像导入我的游戏)
我正在重构应用程序中的页面,并尝试对本身具有验证约束的对象执行递归验证。然而,发生了什么,我只收到一条错误,描述该对象无效。我想要的是嵌套对象的验证错误返回到页面。 ConsumerManagemen
from Tkinter import * class Ball: def __init__(self, canvas, x1, y1, x2, y2): self.x1 =
我是一名优秀的程序员,十分优秀!