基本的に、 a から拡張するカスタム クラスを作成し、false をJPanel
使用して透明にします。setOpaque
のインスタンスを作成し、JFrame
装飾なしに設定し、不透明度を個別に調整します。
カスタムパネルをフレームに追加...
例
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();
}
}
}
}