我有一个主框架重组了一些JPanels。我的JFrame完全填满了。
我希望能够在我左边的JFrame中的另一个JPanel上显示/隐藏一个小JPanel。该JPanel是用户的配置区域。
所以这是我的问题,在我的JFrame 中,在一小块区域中显示JPanel的最佳方式是什么?
我试过了,但它没有按预期工作(这是点击设置图标时执行的代码):
private void jLabelSettingsMouseClicked(java.awt.event.MouseEvent evt) {
settingsActive = !this.jLabelEmargement.isVisible();
if(!settingsActive){
FSettings.setSize(222, 380);
FSettings.setLocation(0, 150);
FSettings.setVisible(true);
FSettings.setBackground(new Color(226,236,241));
this.add(FSettings,BorderLayout.CENTER);
this.frameLearners.setVisible(false);
this.jLabelEmargement.setVisible(false);
this.jLabelFinalEval.setVisible(false);
this.jLabelLeaners.setVisible(false);
}
else{
FSettings.setVisible(false);
this.frameLearners.setVisible(true);
this.jLabelEmargement.setVisible(true);
this.jLabelFinalEval.setVisible(true);
this.jLabelLeaners.setVisible(true);
}
}
谢谢!
答案 0 :(得分:2)
您可以使用玻璃板覆盖框架内容。
http://www.java2s.com/Code/Java/Swing-JFC/DemonstrateuseofGlassPane.htm
JFrame myFrame = ...
JComponent glassPane = new JPanel(null);
myFrame.setGlassPane(glassPane);
private void jLabelSettingsMouseClicked(java.awt.event.MouseEvent evt) {
settingsActive = !this.jLabelEmargement.isVisible();
if(!settingsActive){
FSettings.setSize(222, 380);
FSettings.setLocation(0, 150);
FSettings.setBackground(new Color(226,236,241));
glassPane.add(FSettings);
this.frameLearners.setVisible(false);
this.jLabelEmargement.setVisible(false);
this.jLabelFinalEval.setVisible(false);
this.jLabelLeaners.setVisible(false);
}
else{
glassPane.remove(FSettings);
this.frameLearners.setVisible(true);
this.jLabelEmargement.setVisible(true);
this.jLabelFinalEval.setVisible(true);
this.jLabelLeaners.setVisible(true);
}
}
我用它来显示JFrame
内的效果或标记。
答案 1 :(得分:1)
感谢brainiac我想出了一个解决方案,它按预期工作:
public void toggleSettings(){
if(this.jLabelEmargement.isVisible()){
// Set size of JPanel
FSettings.setSize(222, 380);
// Set location of JPanel
FSettings.setLocation(0, 150);
// Show JPanel
FSettings.setVisible(true);
FSettings.setBackground(new Color(226,236,241));
// Add JPanel to LayeredPane
jLayeredPaneSettings.add(FSettings, new Integer(5));
this.frameLearners.setVisible(false);
this.jLabelEmargement.setVisible(false);
this.jLabelFinalEval.setVisible(false);
this.jLabelLeaners.setVisible(false);
ImageIcon icon = new ImageIcon(getClass().getResource("/com/images/cog_00997d_28.png"));
jLabelSettings.setIcon(icon);
}
else{
// Hide JPanel
FSettings.setVisible(false);
// Remove from LayeredPane
jLayeredPaneSettings.remove(FSettings);
this.frameLearners.setVisible(true);
this.jLabelEmargement.setVisible(true);
this.jLabelFinalEval.setVisible(true);
this.jLabelLeaners.setVisible(true);
ImageIcon icon = new ImageIcon(getClass().getResource("/com/images/cog_000000_28.png"));
jLabelSettings.setIcon(icon);
}
}
您只需要在LayeredPane
中隐藏/显示所有组件。