我正在努力为JFileChooser提供渐变背景。借助StackOverflow帮助,我可以为JFileChooser提供渐变背景,但JFilechooser中的按钮仍具有白色背景。 有什么办法可以删除它?
package javaapplication2;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RenderingHints;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.UIManager;
/**
*
* @author navpreet.kaur
*/
class MyFileChooser extends JFileChooser {
private final Color color1 = Color.DARK_GRAY;
private final Color color2 = new Color(0, 0, 0, 0.65f); //Color.BLACK;
final Color color3 = new Color(0, 0, 0, 0.33f);
private final Color color4 = new Color(0, 0, 0, 0.0f);
private final int shadowHeight = 20;
private final GradientPaint mainGradient;
private final GradientPaint shadowGradient;
java.net.URL defaultViewURL = IRLookAndFeel.class.getResource("DefaultView_Icon.png");
java.net.URL detailsViewURL = IRLookAndFeel.class.getResource("DetailsView_Icon.png");
java.net.URL homeFolderURL = IRLookAndFeel.class.getResource("Home_Icon.png");
java.net.URL listViewURL = IRLookAndFeel.class.getResource("listView_Icon.png");
java.net.URL newFolderURL = IRLookAndFeel.class.getResource("Newfolder.png");
java.net.URL upFolderURL = IRLookAndFeel.class.getResource("Upfolder.png");
ImageIcon fcDefaultViewIcon = new ImageIcon(defaultViewURL);
ImageIcon fcDetailsViewIcon = new ImageIcon(detailsViewURL);
ImageIcon fcHomeFolderIcon = new ImageIcon(homeFolderURL);
ImageIcon fcListViewIcon = new ImageIcon(listViewURL);
ImageIcon fcNewFolderIcon = new ImageIcon(newFolderURL);
ImageIcon fcUpFolderIcon = new ImageIcon(upFolderURL);
public MyFileChooser() {
Component[] comps = getComponents();
recursiveTransparent(comps);
mainGradient = new GradientPaint(0, 0, color1, 0, 600, color2);
shadowGradient = new GradientPaint(0, 0, color3, 0, shadowHeight, color4);
UIManager.put("FileChooser.detailsViewIcon", fcDetailsViewIcon);
UIManager.put ("FileChooser.homeFolderIcon", fcHomeFolderIcon);
UIManager.put ("FileChooser.listViewIcon", fcListViewIcon);
UIManager.put ("FileChooser.newFolderIcon", fcNewFolderIcon);
UIManager.put ("FileChooser.upFolderIcon", fcUpFolderIcon);
UIManager.put ("FileChooser.viewMenuIcon", fcDefaultViewIcon);
}
private void recursiveTransparent(Component[] comps) {
for (Component comp : comps) {
if (comp instanceof JComponent && !(comp instanceof JList)) {
((JComponent) comp).setOpaque(false);
}
if (comp instanceof Container) {
Component[] subComps = ((Container) comp).getComponents();
recursiveTransparent(subComps);
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
int width = getWidth();
int height = getHeight();
g2d.setPaint(mainGradient);
g2d.fillRect(0, 0, width, height);
g2d.setPaint(shadowGradient);
g2d.fillRect(0, 0, width, shadowHeight);
}
}`
我尝试使用UIManager属性,但没有帮助。我的问题是关于如何更改JFilechooser上的图标以及“确定和取消”按钮?
![在此处输入图片描述] [1]
使用您提到的代码,我可以在JFilechooser中执行按钮和标签
if (comp instanceof JButton) {
JButton b = (JButton) comp;
b.setContentAreaFilled(false);
b.setForeground(Color.white);
b.setOpaque(false);
b.setBorderPainted(true);
}
if (comp instanceof JLabel) {
JLabel l = (JLabel) comp;
l.setForeground(Color.white);
}
但是然后有一个带有JFilechooser的组合框,我也在尝试更改属性,但JComboBox没有跟随L和F。我该怎么办?
答案 0 :(得分:0)
LAF控制JButton的背景画,以在寡妇上为您提供简单的渐变画。其他LAF可能会有所不同。
如果要禁用此功能,可以尝试:
JButton button = new JButton( "Text Button" );
button.setContentAreaFilled( false );
现在,面板的背景将显示出来。