简体   繁体   中英

How to use multiple jpanels in the general jframe

I'm going to visualize the implementation of cluster analysis k-means and for this I use swing. There was a problem with the fact that the second jpanel (nodePanel), which was added, is not processed as it happens with clusters.

I tried to deal with the classes jframe and jpanel, as I understood it can be solved not at the level of multithreading, but then I had difficulties with the implementation of this idea.

public class MainUI extends JFrame {
    JPanel canvas;
    NodePanel nodePanel;
    ClusterPanel clusterPanel;
    public static boolean isPaintCluster;
    MainUI(String title) {
        super(title);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        init();
        super.setSize(700, 540);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frm = super.getSize();
        int xpos = (int) (screen.getWidth() / 2 - frm.getWidth() / 2);
        int ypos = (int) (screen.getHeight() / 2 - frm.getWidth() / 2);
        super.setLocation(xpos, ypos);
        super.setVisible(true);
    }
    void init(){
        this.setLayout(null);
        canvas = new JPanel();
        nodePanel = new NodePanel();
        clusterPanel = new ClusterPanel();
        nodePanel.addMouseListener(new NodeClickListener(nodePanel));
     clusterPanel.addMouseListener(new ClusterClickListener(clusterPanel));
        canvas.setBackground(Color.white);
        canvas.setBounds(10,10,480,480);
        nodePanel.setBounds(10,10,480,480);
        clusterPanel.setBounds(10,10,480,480);
        this.add(canvas);
        this.add(clusterPanel);
        this.add(nodePanel);
        JPanel ButtonPanel = new JPanel();
        JRadioButton radioButtonNodes = new JRadioButton("add Nodes");
        radioButtonNodes.addActionListener(new isPaintNode());
        JRadioButton radioButtonCluster = new JRadioButton("add Clusters");
        radioButtonCluster.addActionListener(new isPaintCluster());
        ButtonPanel.setLayout(null);
        this.add(ButtonPanel);
        ButtonPanel.setBounds(500,10,180,480);
        ButtonPanel.add(radioButtonNodes);
        radioButtonNodes.setBounds(0,200,120,20);
        ButtonPanel.add(radioButtonCluster);
        radioButtonCluster.setBounds(0,230,120,20);
    }

    class isPaintCluster implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(isPaintCluster){isPaintCluster = false;}
            else {isPaintCluster = true;}
        }
    }
    class isPaintNode implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(isPaintNode){isPaintNode = false;}
            else {isPaintNode = true;}
        }
    }
}

I expect to get a solution in which the clusters and nodes will be independent as end classes, but in the application at each step of learning the position of the cluster will be redefined and the color of the nodes will also change according to the color of the cluster.

    canvas.setBounds(10,10,480,480);
    nodePanel.setBounds(10,10,480,480);
    clusterPanel.setBounds(10,10,480,480);
    this.add(canvas);
    this.add(clusterPanel);
    this.add(nodePanel);

Swing paints components in the reverse order the component is added to a panel. So the nodePanel is painted, then the clusterPanel is painted and finally the canvas panel is painted.

Since your panels have the same location and size, the canvas paints on top of the clusterPanel which paints on top of the nodePanel. So in reality you will only see the "canvas" panel.

You could try making all the panels non-opaque by using setOpaque( false ) , but I would not recommend this approach.

Instead, I suggest you should only have a single panel and then you override the paintComponent() method of the panel to paint multiple objects on the panel.

Check out Custom Painting Approaches for an example of this approach.

除了将NodePanel和СlusterPanel类组合到一个将处理表单中添加的节点和群集的类之外,我没有想到任何更好的方法了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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