gpt4 book ai didi

java - JFileChooser 自定义 - 将渐变绘制设置为 JFilechooser

转载 作者:行者123 更新时间:2023-11-29 04:14:43 27 4
gpt4 key购买 nike

我正在对 JFileChooser UI 进行一些定制。我正在寻找 JFilechooser 的渐变背景。我所能做的只是一个自定义的 JFilechooser,但我不确定如何将 gardientbackground 添​​加到这个 Filechooser

 public class myCustomFileChooser extends JFileChooser{

static Point compCoords;
static Boolean isMaximized = false;
static Dimension defaultSize = new Dimension(1280,720);
static IRTitleBar titleBar;
//
static java.net.URL logoURL = IRLookAndFeel.class.getResource("photos/topbar/12_white.png");
//
static BufferedImage logoImg;


@Override
protected JDialog createDialog(Component parent) throws HeadlessException {

FileChooserUI ui = getUI();
String title = ui.getDialogTitle(this);
putClientProperty(AccessibleContext.ACCESSIBLE_DESCRIPTION_PROPERTY,
title);

JDialog dialog;
Window window;
window = JOptionPane.getFrameForComponent(parent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
dialog.setUndecorated(true);
} else {
dialog = new JDialog((Dialog)window, title, true);
dialog.setUndecorated(true);
}
dialog.setComponentOrientation(this.getComponentOrientation());
//
try {
IRJFrame.logoImg = ImageIO.read(IRJFrame.logoURL);
} catch (IOException ex) {
// handle exception...
}
//
IRJFrame.titleBar = new IRTitleBar(IRJFrame.logoImg, BackgroundPanel.ACTUAL, 0.01f, 0.5f);
//
//Add Actions for Buttons
//
IRTitleBar.closeBtn.addActionListener((ActionEvent e) -> {
System.exit(0);
});
//
IRTitleBar.minimizeBtn.addActionListener((ActionEvent e) -> {
//dialog.setState(JFrame.ICONIFIED);
});
//
IRTitleBar.maximizeBtn.addActionListener((ActionEvent e) -> {
if(isMaximized){
//setExtendedState(getExtendedState() | JFrame.NORMAL);
setSize(defaultSize);
setOpaque(false);
setLocation(150,150);
}else {
//setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setOpaque(false);
}
isMaximized = !isMaximized;
});
//
compCoords = null;//new Point(0,0);
//
IRJFrame.titleBar.addMouseListener(new MouseListener(){
//
@Override
public void mouseReleased(MouseEvent e) {

compCoords = e.getPoint();

}
@Override
public void mouseClicked(MouseEvent e) {
compCoords = e.getPoint();
}

@Override
public void mousePressed(MouseEvent e) {
compCoords = e.getPoint();
}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}
});
IRJFrame.titleBar.addMouseMotionListener(new MouseMotionListener(){

@Override
public void mouseDragged(MouseEvent e) {
Point currCoords = e.getLocationOnScreen();
setLocation(currCoords.x - compCoords.x, currCoords.y - compCoords.y);
}

@Override
public void mouseMoved(MouseEvent e) {
}
});

//


Container contentPane = dialog.getContentPane();
contentPane.setLayout(new BorderLayout());
//contentPane.add(titleBar,BorderLayout.NORTH);
contentPane.add(this, BorderLayout.CENTER);

/*if (JDialog.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
dialog.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG);
}
} */
dialog.pack();
dialog.setLocationRelativeTo(parent);
//dialog.setUndecorated(true);
//dialog.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

return dialog;
}
}

我应该怎么做才能自定义 JFileChooser 以便我拥有 gardient 背景。我无法找到绘画方法,也无法访问 JFilechooser 内容 Pane 。我应该如何进行?

最佳答案

你可以:

  • 扩展 JFileChooser
  • 覆盖其paintComponent方法
  • 在此覆盖中,创建您的 GradientPaint 并用它填充组件
  • 然后(这是关键)递归地遍历它的子组件并使它们不透明,这样渐变就会显示出来
  • 选择哪些组件不透明,例如保存文件的 JList。

例如:

enter image description here

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.geom.Point2D;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.SwingUtilities;

public class TestFileChooser {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFileChooser fileChooser = new MyFileChooser();
fileChooser.showOpenDialog(null);
});
}
}

class MyFileChooser extends JFileChooser {

private static final Color COLOR_0 = new Color(200, 200, 255);
private static final Color COLOR_1 = Color.BLUE;

public MyFileChooser() {
Component[] comps = getComponents();
recursiveTransparent(comps);
}

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 g2 = (Graphics2D) g;
Point p0 = new Point(0, 0);
Point p1 = new Point(getWidth(), getHeight());
Paint paint = new GradientPaint(p0 , COLOR_0, p1, COLOR_1);
g2.setPaint(paint);
g2.fillRect(0, 0, p1.x, p1.y);
}
}

关于java - JFileChooser 自定义 - 将渐变绘制设置为 JFilechooser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53102971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com