繁体   English   中英

如何在通用jframe中使用多个jpanel

[英]How to use multiple jpanels in the general jframe

我将可视化聚类分析k均值的实现,为此,我使用了swing。 存在一个问题,因为添加的第二个jpanel(nodePanel)不会在集群中发生,因此不会被处理。

我尝试处理jframe和jpanel类,因为我知道不能在多线程级别上解决它,但是后来我很难实现这个想法。

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;}
        }
    }
}

我期望得到一个解决方案,其中群集和节点将作为端类独立,但是在应用程序中,在学习的每个步骤中,群集的位置都将被重新定义,并且节点的颜色也将根据的颜色而变化。集群。

    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);

将组件添加到面板的顺序相反,将组件旋转绘画。 因此,绘制节点面板,然后绘制clusterPanel,最后绘制画布面板。

由于面板的位置和大小相同,因此画布在clusterPanel的顶部绘制,而在nodePanel的顶部绘制。 因此,实际上您只会看到“画布”面板。

您可以尝试使用setOpaque( false )将所有面板设置为不透明,但是我不建议您使用这种方法。

相反,我建议您应该只有一个面板,然后覆盖面板的paintComponent()方法以在面板上绘制多个对象。

查看“ 自定义绘画方法”以获取此方法的示例。

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

暂无
暂无

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

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