简体   繁体   English

“扩展JFrame”和JPanel?

[英]“Extends JFrame” and JPanel?

The following code, as you see below: 以下代码如下所示:

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

extends JFrame. 扩展JFrame。 But I also need it to extend JPanel, in order to make a transparent JPanel. 但我还需要它来扩展JPanel,以便制作透明的JPanel。 The problem is that I can't extend both, java throws a mistake: 问题是我不能同时扩展,java抛出一个错误:

If I extend JPanel I'm able to make a transparent JPanel, but the program can't run because there's a mistake in a few lines of code (mistake that disappears if I extend JFrame). 如果我扩展JPanel我可以创建一个透明的JPanel,但是程序无法运行,因为几行代码中存在错误(如果扩展JFrame,错误会消失)。

However, if I extend JFrame the program will run just fine, but it keeps me away from doing a transparent JPanel. 但是,如果我扩展JFrame,程序将运行得很好,但它使我远离做透明的JPanel。 How can I solve this? 我怎么解决这个问题?

Basically, create your custom class extending from a JPanel , use setOpaque to false to make it transparent. 基本上,创建从JPanel扩展的自定义类,使用setOpaque为false使其透明。

Create an instance of JFrame , set it to undecorated and adjust it's opacity separately. 创建JFrame的实例,将其设置为未修饰并分别调整其不透明度。

Add the custom panel to the frame... 将自定义面板添加到框架中......

Example

在此输入图像描述在此输入图像描述

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();
            }
        }
    }    
}

A class can only ever extend one other class, that's why it won't allow you to extend more than 1 class. 一个类只能扩展另一个类,这就是为什么它不允许你扩展超过1个类。 here is an explanation why that is the case. 是一个解释为什么会这样的情况。 Perhaps you should try reading this for more information about such window modifications (window transparency and shapes) 也许您应该尝试阅读此内容以获取有关此类窗口修改(窗口透明度和形状)的更多信息

If you want specifically a transparent JPanel , perhaps you should look at this answer as it could explain it better than I could. 如果你想要一个透明的JPanel ,也许你应该看看这个答案,因为它可以比我更好地解释它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM