我試圖在部分成功的情況下將JPanel
添加到JLayeredPane
。我能夠分別看到兩個面板,但不能在將它們添加在一起時看到。當我把它們放在一起時,只有DEFAULT_LAYER顯示。任何想法都會很棒。向JLayeredPane添加兩個JPanel
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import javax.swing.JLayeredPane;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Container;
import java.awt.Component;
import java.awt.Insets;
import java.util.ArrayList;
import java.awt.LayoutManager;
public class ImageEditorView
{
public static int dimensionWidth = 1280;
public static int dimensionHeight = 640;
private Color currentColor = Color.WHITE;
private JLayeredPane layeredPane;
private JPanel createEditPanel()
{
final CirclePanel editPanel = new CirclePanel();
//editPanel.setLayout(new CircleLayout(true));
editPanel.setBounds(50, 0, 150, 150);
editPanel.setOpaque(true);
return editPanel;
}
private JPanel createImageView()
{
JPanel imageView = new JPanel(new GridBagLayout());
imageView.setPreferredSize(new Dimension(dimensionWidth, dimensionHeight));
imageView.setBounds(0, 0, dimensionWidth, dimensionHeight);
imageView.setBackground(currentColor);
return imageView;
}
private void addToLayeredPane(JComponent component, int level)
{
//This prints out what is expected. values of 1 and 100 respectfully
System.out.println("The level has a value of " + level);
layeredPane.add(component, level);
}
public void createAndShowGUI()
{
JFrame f = new JFrame("ImageEditor");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
layeredPane = new JLayeredPane();
//Both createImageView and createEditPanel return with the type JPanel
addToLayeredPane(createImageView(), JLayeredPane.DEFAULT_LAYER);
addToLayeredPane(createEditPanel(), JLayeredPane.PALETTE_LAYER);
f.setSize(dimensionWidth, dimensionHeight);
f.setContentPane(layeredPane);
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
}
class CirclePanel extends JPanel
{
@Override
protected void paintComponent(Graphics g)
{
g.drawOval(0, 0, g.getClipBounds().width, g.getClipBounds().height);
}
}
我試過你的建議,沒有骰子。我編輯了代碼並添加了其餘部分,以便編譯,以供參考。 – acampb311
'我編輯了代碼並添加了其餘的代碼,以便編譯'yes,但由於您沒有添加main()方法,因此它不可執行。使代碼完整,所以我們需要做的就是複製/粘貼/編譯/執行!無論如何,請參閱編輯真正的問題。 – camickr
對不起,這裏有新東西,很匆忙,我感謝你的幫助。 – acampb311