我試圖將一個JPanel
放在另一個之上,完全重疊。 我使用JLayeredPane
使它們處於不同的「深度」,因此我可以更改深度和不透明度,以便在另一個「下」看到某個面板。 這裏是一個小測試,我做了哪些工作正常:Java Swing:結合CardLayout和JLayeredPane的效果
public class LayeredCardPanel extends JPanel {
private static final String BLUE_PANEL = "blue ";
private static final String RED_PANEL = "red ";
private static final String GREEN_PANEL = "green";
private String[] panelNames = { BLUE_PANEL, RED_PANEL, GREEN_PANEL };
private Color[] panelColors = { Color.BLUE, Color.RED, Color.GREEN };
private List<JPanel> panels = new ArrayList<>();
private final int TOP_POSITION = 30;
private static final int PANELS_FIRST_POS = 10;
private JLayeredPane layeredPane;
public LayeredCardPanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(createControlPanel());
layeredPane = new JLayeredPane();
////////////////////////////////////////////////////////////////
//setting layout here results in all grey, non functioning panel.
//layeredPane.setLayout(new CardLayout(0, 0));
//////////////////////////////////////////////////////////////
add(layeredPane);
//adding 3 panel
for (int i = 0; i < panelNames.length; i++) {
JPanel panel = new JPanel();
panel.setBackground(panelColors[i]);
layeredPane.add(panel);
layeredPane.setLayer(panel, PANELS_FIRST_POS + i);
panels.add(panel);
}
////////////////////////////////////////////////////////////
//setting the card here, after adding panels, works fine
layeredPane.setLayout(new CardLayout(0, 0));
//////////////////////////////////////////////////////////
}
,你可以在//////註釋行之間看到,這項測試工作正常,只有當我將CardLayout
設置爲JLayeredPane
後我添加了JPanel
s到它。 在我看來,使用默認佈局管理器將JPanel
添加到JLayeredPane
。佈局(設置和更改邊界以填充JLayeredPane
)由稍後應用的CardLayout
完成。
我的問題是:雖然這是工作,我需要的解決方案(使用一個佈局管理器添加,然後取代它來實現我想要的佈局)看起來不是一個優雅的解決方案。我正在尋找更好的方法來做到這一點。
這裏是整個SSCE:
public class LayeredCardPanel extends JPanel {
private static final String BLUE_PANEL = "blue ";
private static final String RED_PANEL = "red ";
private static final String GREEN_PANEL = "green";
private String[] panelNames = { BLUE_PANEL, RED_PANEL, GREEN_PANEL };
private Color[] panelColors = { Color.BLUE, Color.RED, Color.GREEN };
private List<JPanel> panels = new ArrayList<>();
private final int TOP_POSITION = 30;
private static final int PANELS_FIRST_POS = 10;
private JLayeredPane layeredPane;
public LayeredCardPanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(createControlPanel());
layeredPane = new JLayeredPane();
add(layeredPane);
//add 3 panel
for (int i = 0; i < panelNames.length; i++) {
JPanel panel = new JPanel();
panel.setBackground(panelColors[i]);
layeredPane.add(panel);
layeredPane.setLayer(panel, PANELS_FIRST_POS + i);
panels.add(panel);
}
layeredPane.setLayout(new CardLayout());
}
private JPanel createControlPanel() {
ActionListener aListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JButton) {
moveToTop(((JButton) e.getSource()).getActionCommand());
}
}
};
JPanel controls = new JPanel();
controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
JButton blueViewBtn = new JButton(BLUE_PANEL);
blueViewBtn.setActionCommand(BLUE_PANEL);
blueViewBtn.addActionListener(aListener);
controls.add(blueViewBtn);
JButton redViewBtn = new JButton(RED_PANEL);
redViewBtn.setActionCommand(RED_PANEL);
redViewBtn.addActionListener(aListener);
controls.add(redViewBtn);
JButton greenViewBtn = new JButton(GREEN_PANEL);
greenViewBtn.setActionCommand(GREEN_PANEL);
greenViewBtn.addActionListener(aListener);
controls.add(greenViewBtn);
return controls;
}
private void moveToTop(String panelName) {
for(int i = 0; i < panelNames.length; i++) {
if(panelNames[i].equals(panelName)) {
layeredPane.setLayer(panels.get(i),TOP_POSITION);
} else {
layeredPane.setLayer(panels.get(i), PANELS_FIRST_POS + i);
}
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Layered Card Panel Simulation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new LayeredCardPanel();
newContentPane.setPreferredSize(new Dimension(300,100));
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
});
}
}
'你可以見' - 不,我們不能看到,因爲你的代碼不可執行。發佈適當的[SSCCE](http://sscce.org/),可以在發佈代碼時證明問題。 – camickr
「正如你可以在//////之間看到的那樣:是的,你可以看到它。它在代碼中。我加載了最小的代碼來演示問題。 SSCE要長得多。 – c0der