[英]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.