我已经认真地尝试了我所能想到的关于如何使用JLayeredPane
的所有内容,而我所做的一切似乎都没有用。首先,我创建了ImageIcons
,然后将ImageIcons
添加到JLabels
,然后我将JLabel
添加到JLayeredPane
,然后添加{ {1}}到JLayeredPane
。我尝试使用JPanel
,setSize
方法以及我可以找到的任何其他方法来调整setBounds
的大小,然后再将它们添加到JLabels
中,我永远无法获得我要显示的图像,更不用说层层叠叠了。我该如何完成这个?
这是我的代码:
JLayeredPane
所有public Class FaceLayout extends JFrame
{
public FaceLayout()
{
ImageIcon face = new ImageIcon(getClass().getResource("Imported Image");
ImageIcon eyes = new ImageIcon(getClass().getResource("Imported Image");
ImageIcon nose = new ImageIcon(getClass().getResource("Imported Image");
ImageIcon mouth = new ImageIcon(getClass().getResource("Imported Image");
JLabel fLab = new JLabel(face);
fLab1.setBounds(25, 25, 50, 50);
JLabel eLab = new JLabel(eyes);
eLab1.setBounds(25, 25, 50, 50);
JLabel nLab = new JLabel(nose);
nLab1.setBounds(25, 25, 50, 50);
JLabel mLab = new JLabel(mouth);
mLab1.setBounds(25, 25, 50, 50);
JCheckBox eBox = new JCheckBox("Eyes", false);
JCheckBox nBox = new JCheckBox("Nose", false);
JCheckBox mBox = new JCheckBox("Mouth", false);
JButton submit = new JButton("Submit");
JPanel leftPanel = new JPanel(new GridLayout(4, 1));
leftPanel.add(eBox);
leftPanel.add(nBox);
leftPanel.add(mBox);
leftPanel.add(submit);
JLayeredPane layers = new JLayeredPane();
// Here I have tried the setLayer method and just adding them to the layeredPane itself. Right now I have it as:
layers.add(fLab, new Integer(1));
layers.add(eLab, new Integer(2));
layers.add(nLab, new Integer(3));
layers.add(mLab, new Integer(4));
JPanel rightPanel = new JPanel();
rightPanel.add(layers);
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
pane.setDividerLocation(150);
Dimension lSize = new Dimension(100, 50);
Dimension rSize = new Dimension(100, 400);
leftPanel.setMinimumSize(lSize);
rightPanel.setMinimumSize(rSize);
add(pane);
}
}
leftPanel
显示得很好,但我checkboxes
上的所有内容都是空白的白色背景,没有别的。我在这做错了什么?
答案 0 :(得分:1)
问题是您需要在JLayeredPane
的容器上将布局设置为null,并在JLayeredPane
上设置边界
JPanel rightPanel = new JPanel();
rightPanel.setLayout(null);
layers.setBounds(0, 0, 300, 300);
rightPanel.add(layers);
这是最终代码
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
public class FaceLayout extends JFrame {
public FaceLayout() {
ImageIcon face = new ImageIcon(getClass().getResource("/marioblobs/bowser.png"));
ImageIcon eyes = new ImageIcon(getClass().getResource("/marioblobs/mario.png"));
ImageIcon nose = new ImageIcon(getClass().getResource("/marioblobs/luigi.png"));
ImageIcon mouth = new ImageIcon(getClass().getResource("/marioblobs/koopa.png"));
JLabel fLab = new JLabel(face);
fLab.setBounds(25, 25, 100, 100);
JLabel eLab = new JLabel(eyes);
eLab.setBounds(100, 100, 100, 100);
JLabel nLab = new JLabel(nose);
nLab.setBounds(175, 175, 100, 100);
JLabel mLab = new JLabel(mouth);
mLab.setBounds(250, 250, 100, 100);
JCheckBox eBox = new JCheckBox("Eyes", false);
JCheckBox nBox = new JCheckBox("Nose", false);
JCheckBox mBox = new JCheckBox("Mouth", false);
JButton submit = new JButton("Submit");
JPanel leftPanel = new JPanel(new GridLayout(4, 1));
leftPanel.add(eBox);
leftPanel.add(nBox);
leftPanel.add(mBox);
leftPanel.add(submit);
JLayeredPane layers = new JLayeredPane();
layers.add(fLab, new Integer(1));
layers.add(eLab, new Integer(2));
layers.add(nLab, new Integer(3));
layers.add(mLab, new Integer(4));
JPanel rightPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
rightPanel.setLayout(null);
layers.setBounds(0, 0, 400, 400);
rightPanel.add(layers);
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
pane.setDividerLocation(150);
Dimension lSize = new Dimension(100, 50);
Dimension rSize = new Dimension(100, 400);
leftPanel.setMinimumSize(lSize);
rightPanel.setMinimumSize(rSize);
add(pane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new FaceLayout();
}
});
}
}