简体   繁体   English

在Java中将jpanel添加到jpanel中

[英]adding a jpanel into a jpanel in java

I have a Jpanel named mainPanel and another class named MyPanel that extends JPanel . 我有一个Jpanel命名mainPanel和其他类名为MyPanel扩展JPanel I want to add MyPanel into mainPanel . 我想将MyPanel添加到mainPanel I did this code and getting no result . 我做了这段代码,没有结果。

This is my Main class that holds mainPanel : 这是我的main类,其中包含mainPanel

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class Main extends JFrame {

    private JPanel contentPane;
    private final JPanel mainPanel = new JPanel();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main() {
        inItGUI();
    }
    private void inItGUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 662, 417);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        mainPanel.setBounds(25, 11, 611, 341);

        contentPane.add(mainPanel);
        mainPanel.setLayout(null);

        mainPanel.add(new MyPanel());// **Here i added MyPanel**
        mainPanel.revalidate();
        repaint();
    }

}

and this is my MyPanel class that has beeb added to mainPanel earlier: 这是我MyPanel已将beeb添加到mainPanel MyPanel类:

import java.awt.Component;

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class MyPanel extends JPanel {

    private final JButton btnNewButton = new JButton("New button");
    private final JButton btnNewButton_1 = new JButton("New button");
    public MyPanel() {

        inItGUI();
    }
    private void inItGUI() {
        setLayout(null);
        btnNewButton.setBounds(60, 49, 106, 43);

        add(btnNewButton);
        btnNewButton_1.setBounds(189, 49, 89, 43);

        add(btnNewButton_1);
    }
}

Welcome to one of the wonderful pitfalls of null layout managers 欢迎使用null布局管理器的奇妙陷阱之一

If you remove the layout manager, you remove any chance of your components from being automatically size and positioned. 如果删除布局管理器,则可以自动调整尺寸和位置,从而消除组件的任何可能性。

Basically what's happening is that MyPanel is being added to mainPanel and position 0x0 with a size of 0x0, so nothing is begin displayed. 基本上发生的事情是将MyPanel添加到mainPanel且位置0x0的大小为0x0,所以什么也没有开始显示。

The real solution is use an appropriate layout manager 真正的解决方案是使用适当的布局管理器

Don't fool your self. 不要自欺欺人。 Managing the position and size of components, especially the relationship between the components, in the field of modern computer systems is a complex one. 在现代计算机系统领域中,管理组件的位置和大小,尤其是组件之间的关系是一项复杂的工作。 You have to be able to take into account how each component is rendered based on the font, DPI, orientation, etc... of the current system, then take into account how that might effect other components that share the same container...You also have to monitor for changes in the hierarchy of components that you are attached to... 您必须能够根据当前系统的字体,DPI,方向等来考虑每个组件的呈现方式,然后考虑如何影响共享同一容器的其他组件...您还必须监视所连接的组件层次结构中的更改...

This is the job that layout managers provide...Swing is designed around the use of layout managers and you should have a VERY good reason to ignore them...IMHO 这是布局管理器提供的工作... Swing是围绕布局管理器的使用而设计的,您应该有一个很好的理由忽略它们...恕我直言

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

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