简体   繁体   English

如何在一帧中添加绘图(绘制方法)和另一个带控件的面板?

[英]How to add a drawing (paint method) and another panel with controls in one frame?

I need to have a JFrame where the upper part is a drawing made by paint() and the lower part is a panel composed of JLabel , JTextField and JButton components. 我需要一个JFrame ,其中上部是由paint()制作的绘图,下部是由JLabelJTextFieldJButton组件组成的面板。

Is this possible? 这可能吗? How am I supposed to do this? 我该怎么做?

I need to have a Jframe where the upper part is a drawing made by paint() and the lower part is a panel composed of JLabel, JTextField and JButtons. 我需要一个Jframe,其中上部是由paint()制作的绘图,下部是由JLabel,JTextField和JButtons组成的面板。

There is no conflict on what you want to do. 你想做什么没有冲突。 You can have a main JPanel with 2 sub panels. 你可以有一个带有2个子面板的主JPanel。 One on the top for your drawings, the other at the bottom for containing your JComponents such as JButtons: 一个在顶部用于您的绘图,另一个在底部用于包含您的JComponents,例如JButtons:

在此输入图像描述

The structure in code may look like this: 代码中的结构可能如下所示:

class MainPanel extends JPanel{
    private DrawingSpace drawingSpace;  //Customized JPanel for drawing
    private JPanel subPanel;

    public MainPanel(){
        setPreferredSize(new Dimension(400, 400));
        initComponents();
    }

    private void initComponents(){
        drawingSpace = new DrawingSpace();
        subPanel = new JPanel();
    }
}

You can have a customized JPanel as follows (this is optional): 您可以按如下方式使用自定义JPanel(这是可选的):

class DrawingSpace extends JPanel
{
    public DrawingSpace(){
        //Set size..etc
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        //perform your drawings here..
    }
}

After the implementations for the JPanels , you can just add an instance of MainPanel to the JFrame : 为的实现后JPanels ,你可以添加的一个实例MainPanelJFrame

JFrame frame = new JFrame();
frame.add(new MainPanel());
//Other codes for JFrame not shown here

The soulution suggested by user3437460 (use a JPanel for the upper part, and override the painting methods in that JPanel) is the preferrable way to solve this. user3437460建议的soulution(上部使用JPanel,并覆盖JPanel中的绘制方法)是解决此问题的首选方法。

However as you asked for a solution to directly paint the upper part (which is not advised, but there are solutions): 但是,当您要求直接绘制上部的解决方案时(不建议,但有解决方案):

  • A (nasty) workaround for the question would be overriding the necessary paint method of JFrame, draw your upper part, translate the graphics context by some 100 pixels and call inherited paint methods to draw the bottom part. 这个问题的一个(令人讨厌的)解决方法是覆盖JFrame的必要绘制方法,绘制上部,将图形上下文翻译大约100个像素,并调用继承的绘制方法来绘制底部部分。 (Note that you'll have layout manager issues, as the layout manager won't see the 100px height of the upper part. However, if you're using an absolute layout, it could work. Hacks, hacks hacks :( (请注意,您将遇到布局管理器问题,因为布局管理器不会看到上部的100px高度。但是,如果您使用绝对布局,它可能会工作。黑客,黑客攻击:(

  • Another super-hack is to actually make the lower part big enough (if you use absolute layout, position your lower part at y=100px). 另一个超级黑客实际上是让下半部分足够大(如果你使用绝对布局,将你的下半部分定位在y = 100px)。 Then add your own GlassPane and render the content for the upper part (or anywhere) on the glassPane. 然后添加自己的GlassPane并渲染glassPane上部(或任何位置)的内容。

  • Of course you can create a dedicated layout manager, which leaves the top 100 px part empty. 当然,您可以创建一个专用的布局管理器,使前100个px部分为空。 Use that layout manager, and then you get some empty space on the top, which you can draw on. 使用该布局管理器,然后在顶部获得一些空白区域,您可以使用它。

I think now you can agree that the problem is rather "how to put a custom drawn component on the top of the window" , which is solved by putting a custom drawn JPanel on the top of the window. 我想你现在可以同意问题是“如何将自定义绘制的组件放在窗口顶部” ,这可以通过在窗口顶部放置一个自定义绘制的JPanel来解决。 Keep it easy! 保持简单! Peace! 和平!

ps: override paintComponent() instead of paint() of JPanel. ps:覆盖paintComponent()而不是JPanel的paint()。 See bottom of http://www.oracle.com/technetwork/java/painting-140037.html 请参见http://www.oracle.com/technetwork/java/painting-140037.html的底部

For the painting portion, create a JPanel (or other paintable component) and override the paint method. 对于绘画部分,创建JPanel(或其他可绘制的组件)并覆盖paint方法。 Use a second JPanel and place all of your other components in that. 使用第二个JPanel并将所有其他组件放入其中。

From there, check out how to do layout management at https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html#set 从那里,请访问https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html#set ,了解如何进行布局管理。

The simplest way to do this is to use GridLayout, with the painted panel on the top half and the components panel on the bottom half. 最简单的方法是使用GridLayout,上半部分的绘制面板和下半部分的组件面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM