“扩展JFrame”和JPanel?

时间:2013-09-09 01:53:48

标签: java swing jframe jpanel

以下代码,如下所示:

public class app extends javax.swing.JFrame implements Runnable {

扩展JFrame。但我还需要它来扩展JPanel,以便制作透明的JPanel。问题是我不能同时扩展,java抛出一个错误:

如果我扩展JPanel,我可以创建一个透明的JPanel,但程序无法运行,因为几行代码中存在错误(如果扩展JFrame,错误会消失)。

但是,如果我扩展JFrame,程序将运行得很好,但它让我远离做透明的JPanel。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

基本上,创建从JPanel扩展的自定义类,使用setOpaque为false以使其透明。

创建JFrame的实例,将其设置为未修饰并分别调整它的不透明度。

将自定义面板添加到框架中......

示例

enter image description here enter image description here

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TransparentPanel {

    public static void main(String[] args) {
        new TransparentPanel();
    }

    public TransparentPanel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                BufferedImage img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/issue459.jpg"));
                final JLabel label = new JLabel(new ImageIcon(img.getScaledInstance(-1, 200, Image.SCALE_SMOOTH)));
                label.setLayout(new CardLayout());

                JPanel menu = new JPanel(new GridBagLayout());
                JButton button = new JButton("Show");
                menu.add(button);

                JPanel transparent = new JPanel(new GridBagLayout());
                transparent.setOpaque(false);
                transparent.add(new JLabel("Look, I'm see through"));

                label.add(menu, "menu");
                label.add(transparent, "transparent");
                CardLayout layout = (CardLayout) label.getLayout();
                layout.show(label, "menu");

                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        CardLayout layout = (CardLayout) label.getLayout();
                        layout.show(label, "transparent");
                    }
                });

                add(label);
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }
    }    
}

答案 1 :(得分:1)

一个类只能扩展另一个类,这就是为什么它不允许你扩展超过1个类。 here解释了为什么会这样。也许您应该尝试阅读this以获取有关此类窗口修改(窗口透明度和形状)的更多信息

如果你想要一个透明的JPanel,也许你应该看看this answer,因为它可以更好地解释它。