JFileChooser 按钮自定义
JFileChooser Buttons Customisations
我正在努力为 JFileChooser 提供渐变背景。在 Whosebug 的帮助下,我能够为 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.What 我可以做吗?
LAF控制JButton的背景绘制,给你一个简单的渐变绘制,在Widows上。其他 LAF 可能会做一些不同的事情。
如果你想禁用它,你可以尝试:
JButton button = new JButton( "Text Button" );
button.setContentAreaFilled( false );
现在面板的背景会透出来了。
我正在努力为 JFileChooser 提供渐变背景。在 Whosebug 的帮助下,我能够为 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.What 我可以做吗?
LAF控制JButton的背景绘制,给你一个简单的渐变绘制,在Widows上。其他 LAF 可能会做一些不同的事情。
如果你想禁用它,你可以尝试:
JButton button = new JButton( "Text Button" );
button.setContentAreaFilled( false );
现在面板的背景会透出来了。