使 jtoolbar 透明
Make jtoolbar transparent
我有一个带背景的 jPanel,我想在上面添加一个 JToolbar。
我的问题是当我添加 JToolbar 时,它的默认背景很烦人,我将它的不透明设置为 false 但没有效果。
我想删除它的默认背景并使其透明。
我阅读了以下文章但没有帮助:
JToolbar background image
这是我的代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
public class Toolbar extends JToolBar {
private JButton manage;
private JButton add;
private JButton search;
private JButton exit;
public Toolbar() {
super();
manage = new JButton();
add = new JButton();
search = new JButton();
exit = new JButton();
ImageIcon icon = new ImageIcon("pics/Add-01.png");
Image img = icon.getImage();
Image newImage = img.getScaledInstance(80, 80, Image.SCALE_SMOOTH);
add.setIcon(new ImageIcon(newImage));
setOpaque(false);
setBackground(Color.RED);
add(add);
add(search);
add(manage);
add(exit);
}
}
感谢您的支持。
无效代码:
public class Toolbar extends JToolBar {
public Toolbar() {
setBackground(Color.RED);
setOpaque(false);
add(new JButton("add"));
}
protected void addImpl(Component comp, Object constraints, int index) {
super.addImpl(comp, constraints, index);
if (comp instanceof JButton) {
((JButton) comp).setContentAreaFilled(false);
}
}
}
基本上,按钮仍然是 "filling" 它们的背景。您可以通过 JButton#setContentAreaFilled
指示他们不要绘制他们的内容(背景)
此示例将 JToolBar
的背景颜色设置为红色,因此您可以看到按钮现在是透明的。要使工具栏透明,只需在构造函数
中添加setOpaque(false)
public class CustomToolBar extends JToolBar {
public CustomToolBar() {
setBackground(Color.RED);
}
protected void addImpl(Component comp, Object constraints, int index) {
super.addImpl(comp, constraints, index);
if (comp instanceof JButton) {
((JButton) comp).setContentAreaFilled(false);
}
}
}
扩展示例....
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JButton manage = new JButton("Manage");
JButton add = new JButton("Add");
JButton search = new JButton("Search");
JButton exit = new JButton("Exit");
CustomToolBar tb = new CustomToolBar();
tb.add(manage);
tb.add(add);
tb.add(search);
tb.add(exit);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.add(tb, BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage bgImg;
public TestPane() {
setLayout(new BorderLayout());
try {
bgImg = ImageIO.read(new File("..."));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return bgImg == null ? new Dimension(200, 200) : new Dimension(bgImg.getWidth(), bgImg.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bgImg != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - bgImg.getWidth()) / 2;
int y = (getHeight() - bgImg.getHeight()) / 2;
g2d.drawImage(bgImg, x, y, this);
g2d.dispose();
}
}
}
public class CustomToolBar extends JToolBar {
public CustomToolBar() {
setBorder(new LineBorder(Color.BLACK, 2));
setOpaque(false);
}
@Override
protected void addImpl(Component comp, Object constraints, int index) {
super.addImpl(comp, constraints, index);
if (comp instanceof JButton) {
((JButton) comp).setContentAreaFilled(false);
}
}
}
}
我有一个带背景的 jPanel,我想在上面添加一个 JToolbar。
我的问题是当我添加 JToolbar 时,它的默认背景很烦人,我将它的不透明设置为 false 但没有效果。
我想删除它的默认背景并使其透明。
我阅读了以下文章但没有帮助:
JToolbar background image
这是我的代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
public class Toolbar extends JToolBar {
private JButton manage;
private JButton add;
private JButton search;
private JButton exit;
public Toolbar() {
super();
manage = new JButton();
add = new JButton();
search = new JButton();
exit = new JButton();
ImageIcon icon = new ImageIcon("pics/Add-01.png");
Image img = icon.getImage();
Image newImage = img.getScaledInstance(80, 80, Image.SCALE_SMOOTH);
add.setIcon(new ImageIcon(newImage));
setOpaque(false);
setBackground(Color.RED);
add(add);
add(search);
add(manage);
add(exit);
}
}
感谢您的支持。
无效代码:
public class Toolbar extends JToolBar {
public Toolbar() {
setBackground(Color.RED);
setOpaque(false);
add(new JButton("add"));
}
protected void addImpl(Component comp, Object constraints, int index) {
super.addImpl(comp, constraints, index);
if (comp instanceof JButton) {
((JButton) comp).setContentAreaFilled(false);
}
}
}
基本上,按钮仍然是 "filling" 它们的背景。您可以通过 JButton#setContentAreaFilled
此示例将 JToolBar
的背景颜色设置为红色,因此您可以看到按钮现在是透明的。要使工具栏透明,只需在构造函数
setOpaque(false)
public class CustomToolBar extends JToolBar {
public CustomToolBar() {
setBackground(Color.RED);
}
protected void addImpl(Component comp, Object constraints, int index) {
super.addImpl(comp, constraints, index);
if (comp instanceof JButton) {
((JButton) comp).setContentAreaFilled(false);
}
}
}
扩展示例....
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JButton manage = new JButton("Manage");
JButton add = new JButton("Add");
JButton search = new JButton("Search");
JButton exit = new JButton("Exit");
CustomToolBar tb = new CustomToolBar();
tb.add(manage);
tb.add(add);
tb.add(search);
tb.add(exit);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.add(tb, BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage bgImg;
public TestPane() {
setLayout(new BorderLayout());
try {
bgImg = ImageIO.read(new File("..."));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return bgImg == null ? new Dimension(200, 200) : new Dimension(bgImg.getWidth(), bgImg.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bgImg != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - bgImg.getWidth()) / 2;
int y = (getHeight() - bgImg.getHeight()) / 2;
g2d.drawImage(bgImg, x, y, this);
g2d.dispose();
}
}
}
public class CustomToolBar extends JToolBar {
public CustomToolBar() {
setBorder(new LineBorder(Color.BLACK, 2));
setOpaque(false);
}
@Override
protected void addImpl(Component comp, Object constraints, int index) {
super.addImpl(comp, constraints, index);
if (comp instanceof JButton) {
((JButton) comp).setContentAreaFilled(false);
}
}
}
}