无法在Java中将图形添加到JPanel中

编程入门 行业动态 更新时间:2024-10-10 21:26:29
本文介绍了无法在Java中将图形添加到JPanel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为我一直在做的宠物项目编写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 not paint()
  • 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 the JFrame. The reason being that those dimensions on the JFrame include the thick borders. For the JPanel 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中

    本文发布于:2023-11-12 11:17:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1581345.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:中将   图形   Java   JPanel

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!