在 JPanel 中实现图形

发布于 2024-12-10 18:41:57 字数 517 浏览 0 评论 0原文

我一直在尝试在 Netbeans 中制作的 GUI 应用程序中实现图形。我已经使用 GUI 编辑器完成了布局(按钮、文本框等),到目前为止一切正常。基本上我想要做的是在 JFrame 的下半部分生成一些图形(线条等),并将复选框、按钮等保留在 JFrame 的上半部分。

从我看到的示例来看,一般方法是创建一个扩展 JPanel 的类,并包含用于创建线条和其他各种形状的图形代码。我已经能够独立于 GUI 编辑器来实现此功能,但该应用程序仅由占据整个 JFrame 的 JPanel 组成。我在 GUI 编辑器中用于执行此操作的不同方法要么生成了图形,但屏蔽了 JFrame 中的所有其他内容(即使是一条很小的线),要么根本不执行任何操作。

理想情况下,我希望能够将任意大小的 JPanel 放置在 JFrame 上的任意位置,并创建面板中包含的图形。我还希望我创建的图形的坐标引用 JPanel 而不是 JFrame (因此坐标 0,0 是我的 JPanel 的左上角,无论我决定将其放置在哪里。)

有没有一种简单的方法这样做?

(示例代码将不胜感激)

I've been trying to implement graphics in a GUI application I've made in Netbeans. I've done the layout (buttons, textboxes, etc.) using the GUI editor and it all works fine so far. Basically what I want to do is generate some graphics (lines and such) in the bottom half of my JFrame and leave the checkboxes, buttons, and such in the top half of my JFrame.

From the examples I've seen, the general approach is to create a class which extends JPanel and contains graphics code to create lines and other various shapes. I've been able to implement this independent of the GUI editor, but the application consists solely of the JPanel which occupies the entire JFrame. Different approaches I've used for doing this in the GUI editor have either generated the graphics but blocked out everything else in my JFrame (even for a tiny line) or not done anything at all.

Ideally I'd want to be able to place a JPanel of arbitrary size in an arbitrary location on my JFrame and create graphics that are contained within the panel. I'd also want the coordinates of the graphics I create to be referenced to the JPanel rather than the JFrame (so coordinate 0,0 is the top left of my JPanel regardless of where I decide to place it.)

Is there a straightforward means of doing this?

(example code would be greatly appreciated)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2024-12-17 18:41:57

首先,您必须提供一个扩展 JPanel 的类,然后重写 PaintComponent 方法以提供自定义的图形绘制。

然后在 JFrame 上您需要使用适当的布局管理器在 JFrame 上放置多个组件。举例来说,您可以使用 GridLayout 将 Jframe 分成相等的两半。上面的一个可以包含带有按钮和控件的普通 JPanel,下面的一个将是带有自定义图形的自定义 JPanel。

您在paintComponent 中使用的坐标将仅引用底部的JPanel。

当然,如果您需要更具体的布局选项,您可以使用比 GridLayout 更高级的布局管理器。

  • 这也可以在 Netbeans IDE 中通过右键单击组件并在自动调整大小选项中选择“垂直”和/或“水平”来完成。

First, you will have to provide a class that extends the JPanel, then override the paintComponent method to provide your custom painting of graphics.

Then on the JFrame you need to use an appropriate Layout Manager to place multiple components on the JFrame. Say for instance, you can use a GridLayout to divide the Jframe into two equal halves. The one on top can contain a normal JPanel with your buttons and controls and the one underneath will be your custom JPanel with custom graphics.

The coordinates that you use in paintComponent will refer to only the JPanel at the bottom.

Of course you can use more advanced Layout Managers than GridLayout if you need more specific layout options.

  • this can also be done in the netbeans ide by right-clicking the component and selecting "vertical" and/or "horizontal" in the auto resizing option.
土豪我们做朋友吧 2024-12-17 18:41:57

好吧,你可以走老路,简单而高效:

  1. 将布局管理器设置为空

    contentPane.setLayout ( null ) ;

  2. 在您的类中实现 public void doLayout() 方法

例如,

   public void doLayout () {
      Dimension size      = getSize() ;
      int       x         = wizardImage.getImage().getWidth(this)+10 ;
      int       y         = 5 ;
      int       rowHeight = 18;
      super.doLayout() ;
      st_IMAGE.setBounds ( 5,((size.height-40)/2)-(wizardImage.getImage().getHeight(this)/2), wizardImage.getImage().getWidth(this), wizardImage.getImage().getHeight(this)) ;
      st_EXPORTTYPE.setBounds ( x, y, size.width-(x)-10, rowHeight ) ;
      y+=rowHeight ;
      rb_TAB_DELIM.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight ;
      rb_COMMA_SEPARATED.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight ;
      rb_FLAT_TEXT.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight+10 ;
      pb_EXPORT.setLocation ( x, size.height-75 ) ;
      pb_CANCEL.setLocation ( x+pb_EXPORT.getMinimumSize().width+5, size.height-75 ) ;

   }

Well you can go old school, Simple and efficient:

  1. Set the layout manager as null

    contentPane.setLayout ( null ) ;

  2. Implement the public void doLayout() method in your class

for example

   public void doLayout () {
      Dimension size      = getSize() ;
      int       x         = wizardImage.getImage().getWidth(this)+10 ;
      int       y         = 5 ;
      int       rowHeight = 18;
      super.doLayout() ;
      st_IMAGE.setBounds ( 5,((size.height-40)/2)-(wizardImage.getImage().getHeight(this)/2), wizardImage.getImage().getWidth(this), wizardImage.getImage().getHeight(this)) ;
      st_EXPORTTYPE.setBounds ( x, y, size.width-(x)-10, rowHeight ) ;
      y+=rowHeight ;
      rb_TAB_DELIM.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight ;
      rb_COMMA_SEPARATED.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight ;
      rb_FLAT_TEXT.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight+10 ;
      pb_EXPORT.setLocation ( x, size.height-75 ) ;
      pb_CANCEL.setLocation ( x+pb_EXPORT.getMinimumSize().width+5, size.height-75 ) ;

   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文