如何自定义Swing的JFileChooser:背景色与路径文本框形状修改
Swing自定义JFileChooser遇到的两个外观问题
我需要在Swing应用中插入JFileChooser对话框,希望自定义其外观以匹配其他窗口,但目前遇到以下问题:
- 修改JFileChooser的背景色
- 修改显示文件夹路径的JTextField的形状
以下是三个示例类,用于说明我遇到的问题:
自定义JDialog类
(将JFileChooser嵌入JDialog的思路来自Stack Overflow相关讨论)
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class FileChooserDialog extends JDialog { private ModifiedFileChooser modifiedFileChooser; public FileChooserDialog(ModifiedFileChooser modifiedFileChooser) { super(); this.modifiedFileChooser = modifiedFileChooser; setUndecorated(true); setLayout(new BorderLayout()); add(modifiedFileChooser, BorderLayout.CENTER); setBackground(new Color(180, 14, 14)); pack(); setVisible(true); } public static void main(String[] args) { JFrame frame = new JFrame(); JButton openB = new JButton("Open"); openB.setPreferredSize(new Dimension(50,20)); frame.setLayout(new BorderLayout()); frame.add(openB, BorderLayout.CENTER); openB.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new FileChooserDialog(new ModifiedFileChooser()); } }); frame.setPreferredSize(new Dimension(300, 200)); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true); } }
自定义JFileChooser类
(自定义JFileChooser的思路参考Stack Overflow相关讨论)
import javax.swing.*; import java.awt.*; public class ModifiedFileChooser extends JFileChooser { public ModifiedFileChooser() { super(); customizeFileChooser(this.getComponents()); setBackground(new Color(180, 14, 14)); repaint(); } private void customizeFileChooser(Component[] comp) { for (int x = 0; x < comp.length; x++) { if (comp[x] instanceof JScrollPane) { ((JScrollPane) comp[x]).setBackground(new Color(180, 14, 14)); } if (comp[x] instanceof JPanel) { ((JPanel) comp[x]).setBackground(new Color(180, 14, 14)); customizeFileChooser(((JPanel) comp[x]).getComponents()); Component[] c = ((JPanel) comp[x]).getComponents(); for (int i = 0; i < c.length; i++) { if (c[i] instanceof JTextField) { int previousSize = ((JTextField) c[i]).getColumns(); c[i] = new RoundedTextField(previousSize); } if (c[i] instanceof JPanel) { ((JPanel) c[i]).setBackground(new Color(180, 14, 14)); } if (c[i] instanceof JScrollPane) { ((JScrollPane) c[i]).setBackground(new Color(180, 14, 14)); } } } } } }
自定义JTextField类
(圆角文本框的实现思路来自Stack Overflow相关讨论)
import javax.swing.*; import java.awt.*; import java.awt.geom.RoundRectangle2D; public class RoundedTextField extends JTextField { private Shape shape; public RoundedTextField(int size) { super(size); setOpaque(false); } protected void paintComponent(Graphics g) { g.setColor(getBackground()); g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15); super.paintComponent(g); } protected void paintBorder(Graphics g) { g.setColor(getForeground()); g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15); } public boolean contains(int x, int y) { if (shape == null || !shape.getBounds().equals(getBounds())) { shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15); } return shape.contains(x, y); } }
点击框架中的按钮后显示的对话框如下:
理想状态下,我希望“文件名:”标签旁的文本框带有圆角,且JFileChooser的背景为红色而非灰色。
感谢您提供的任何帮助!
内容的提问来源于stack exchange,提问作者andy_07
相关产品推荐
相关产品推荐

