无法在Java中将图形添加到JPanel中 [英] Can't add Graphics into JPanel in Java
问题描述
我正在为我一直在做的宠物项目编写UI,并且正在尝试使用Java Graphics,绘制线条,形状和其他东西.而且,我整天都在尝试在Jpanel中插入一条简单的行,但仍然没有弄清楚出了什么问题.
I'm writing the UI for the pet project I've been doing and I'm experimenting with java Graphics, drawing lines, shapes, and stuff. And, I've been trying all day to insert a simple line in a Jpanel but still haven't figured out what went wrong.
package thuake;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.geom.Line2D;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Main extends JFrame{
static Dimension DEFAULT_SIZE = new Dimension(530, 320);
static JFrame Frame1 = new JFrame();
static JScrollPane spanel = new JScrollPane();
static JPanel Panel1 = new JPanel();
static MenuBar menu = new MenuBar();
static Menu menusub1 = new Menu("Open");
public static void main(String[] args)
{
start();
}
public static void start (){
Frame1.setLayout(new FlowLayout(FlowLayout.CENTER,5,10));
spanel.add(new draw());
Frame1.add(spanel);
spanel.setBorder(BorderFactory.createLineBorder(Color.black));
spanel.setPreferredSize(new Dimension(500, 500));
Frame1.add(new JButton("ad"));
Frame1.add(new JButton("ad"));
Frame1.add(new JButton("ad"));
Frame1.add(new draw());
Frame1.setMenuBar(menu);
menu.add(menusub1);
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame1.pack();
spanel.setVisible(true);
Frame1.setVisible(true);
System.out.println();
}
static class draw extends Component {
public void paint(Graphics g) {
Graphics2D line = (Graphics2D)g;
line.drawLine(0, 0, 120, 120);
}
}
}
推荐答案
这是在制作图形时应该遵循的基本框架.
Here is a basic framework you should follow when you are doing graphics.
关键点是:
- 不要扩展JFrame,请使用单独的实例.
- 覆盖
paintComponent()
,而不覆盖paint()
- 在EDT中启动该过程.在EDT中进行所有摇摆操作.
- 请勿在EDT外部或使用您自己检索的图形上下文绘画.始终使用
paintComponent()
中的一个 - 设置面板的
宽度和高度
(<尺寸>尺寸),而不是JFrame
.原因是JFrame
上的那些尺寸包括粗边框.对于JPanel
,您可以获得完整宽度.
- Don't extend JFrame, use a separate instance.
- Override
paintComponent()
and notpaint()
- Start the process in the EDT. Do all swing stuff in the EDT.
- Do not paint outside the EDT or with a graphics context that you retrieved yourself. Always use the one from
paintComponent()
- Set the
width and height
(dimension
) for the panel and not theJFrame
. The reason being that those dimensions on theJFrame
include the thick borders. For theJPanel
you get the full width.
您可以随时对此进行修改,并将其他组件添加到JFrame和/或JPanel.目前,这应该为实验奠定基础.
At some point you can modify this and add additional components to the JFrame and/or JPanel. For now this should provde a basis for experimenting.
您还应该阅读以下内容以及我遗漏的所有内容.查看 Java教程,以获取有关图形和绘画的更多信息.>
You should also read up on this and anything I omitted shown below. Check out the Java Tutorials for more on information on graphics and painting.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Template extends JPanel {
final static int height = 500;
final static int width = 500;
final static String title = "title";
JFrame frame = new JFrame(title);
public static void main(String[] args) {
// start on the EDT
SwingUtilities.invokeLater(() -> new Template().start());
}
public Template() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this); // add the panel
setPreferredSize(new Dimension(width, height));
frame.pack();
// center on screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // always do this
Graphics2D g2d = (Graphics2D) g.create();
// Optional. It averages the edges of a figure to give a smoothing effect
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// do something here.
g2d.setColor(Color.red);
g2d.fillRect(200,200,100,100);
g2d.dispose();
}
}
这篇关于无法在Java中将图形添加到JPanel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!