第二十章《Java Swing》第8节:选择器

在Swing体系中有文件选择器和颜色选择器,它们分别用来帮助用户选择文件和颜色,这些选择操作是可视化桌面应用程序常用的操作,本小节将详细讲解这两种选择器的使用方式。

20.8.1文件选择器JFileChooser

文件选择器用于选择文件或文件夹。当用户打开一个文件时需要用文件选择器选择一个文件,而保存文件时又需要用文件选择器去选择一个文件夹。Swing体系用JFileChooser类来表示文件选择器。通常情况下,文件选择器都是以一个模态对话框的形式出现。

JFileChooser有一个无参数的构造方法,使用这个构造方法创建对象的语句是:

JFileChooser fileChooser = new JFileChooser();

使用以上语句所创建的fileChooser对象会以系统默认的路径打开文件选择器,程序员也可以在构造方法中指定了文件选择器的打开路径,例如:

JFileChooser fileChooser = new JFileChooser("D:/");

使用以上语句所创建的ileChooser对象会以D盘根目录打开文件选择器。

弹出选择文件的模态对话框的方法被定义在JFileChooser类中,它们分别是showOpenDialog()、showSaveDialog()和showDialog()。showOpenDialog()方法所弹出的对话框上标题和按钮的文本都是“打开”,而showSaveDialog()方法所弹出的对话框上标题和按钮的文本都是“保存”。使用showDialog()方法则可以自定义对话框上的标题和按钮文本。以上三个方法的返回值均为int型,这个返回值表示了用户在对话框上点击了那个按钮。如果用户点击了“打开”或“保存”按钮,在方法的返回值为0,一般以JFileChooser类的静态属性APPROVE_OPTION表示这个返回值,而用户如果点击的是“取消”按钮,则方法的返回值为1,一般以JFileChooser类的静态属性APPROVE_OPTION表示这个返回值。

JFileChooser可以通过为setFileSelectionMode()方法传递不同的参数来文件选择器指定只能打开文件还是只能打开文件夹或是二者都能打开,setFileSelectionMode()方法的参数一般以JFileChooser类的静态属性表示,如表20-18所示。

表20-18 选择模式

静态属性

意义

FILES_ONLY

只能打开文件

DIRECTORIES_ONLY

只能打开文件夹

FILES_AND_DIRECTORIES

文件和文件夹都能打开

文件选择器还可以设置是否可以同时选中多个文件或文件夹,默认情况下在文件选择器对话框中只能选中一个文件或文件夹,如果调用了JFileChooser类对象的setMultiSelectionEnabled()方法并为其传递true为参数,则可以设置文件选择器可以同时选中多个文件或文件夹。

当用户选择了一个文件和文件夹后,通过调用JFileChooser类定义的getSelectedFile()方法就能获得被选中的文件或文件夹。而如果用户同时选择了多个文件或文件夹,则可以调用getSelectedFiles()方法来获得这些文件或文件夹,getSelectedFiles()方法的返回值是File数组。下面的【例20_27】展示了如何弹出一个文件选择器,并把用户所选择的文件名称显示到文本框中。

【例20_27 JFileChooser的使用】

Exam20_27.java

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
class Exam20_27Frame extends JFrame{
    JButton btnOpen,btnSave,btnSelect;
    JTextField txtOpen,txtSave,txtSelect;
    public Exam20_27Frame(){
        init();
    }
    private void init( ){
        Container container = this.getContentPane();//获得窗体的主体区域
        container.setLayout(null);
        btnOpen = new JButton("打开");
        btnSave = new JButton("保存");
        btnSelect = new JButton("选择");
        txtOpen = new JTextField();
        txtSave = new JTextField();
        txtSelect = new JTextField();
        txtOpen.setSize(350, 30);
        txtOpen.setLocation(30, 30);
        txtOpen.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        btnOpen.setSize(80, 30);
        btnOpen.setLocation(400, 30);
        btnOpen.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        btnOpen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser("D:/");
                fileChooser.setMultiSelectionEnabled(true);
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = fileChooser.showOpenDialog(Exam20_27Frame.this);
                if(option==JFileChooser.APPROVE_OPTION){
                    File[] files = fileChooser.getSelectedFiles();
                    String fileNames = "";
                    for(int i=0;i<files.length;i++){
                        fileNames = fileNames+files[i].getAbsolutePath()+";";
                    }
                    txtOpen.setText(fileNames);
                }else{
                    txtOpen.setText("");
                }
            }
        });

        txtSave.setSize(350, 30);
        txtSave.setLocation(30, 90);
        txtSave.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        btnSave.setSize(80, 30);
        btnSave.setLocation(400, 90);
        btnSave.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        btnSave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int option = fileChooser.showSaveDialog(Exam20_27Frame.this);
                if(option==JFileChooser.APPROVE_OPTION){
                    File file = fileChooser.getSelectedFile();
                    String fileName = file.getAbsolutePath();
                    txtSave.setText(fileName);
                }else{
                    txtSave.setText("");
                }
            }
        });
        txtSelect.setSize(350, 30);
        txtSelect.setLocation(30, 150);
        txtSelect.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        btnSelect.setSize(80, 30);
        btnSelect.setLocation(400, 150);
        btnSelect.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        btnSelect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int option = fileChooser.showDialog(Exam20_27Frame.this, "选择");
                if(option==JFileChooser.APPROVE_OPTION){
                    File file = fileChooser.getSelectedFile();
                    String fileName = file.getAbsolutePath();
                    txtSelect.setText(fileName);
                }else{
                    txtSelect.setText("");
                }
            }
        });
        container.add(btnOpen);
        container.add(btnSave);
        container.add(btnSelect);
        container.add(txtOpen);
        container.add(txtSave);
        container.add(txtSelect);
    }
}
public class Exam20_27 {
    public static void main(String[] args) {
        Exam20_27Frame frame = new Exam20_27Frame();
        frame.setSize(550, 250);
        frame.setLocationRelativeTo(null);
        frame.setTitle("Exam20_26Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

【例20_27】的运行结果如图20-28所示。

图20-28【例20_27】运行结果

在图20-28所示的窗体上单击任意一个按钮都能弹出一个文件选择对话框,当选中一个文件后,文件的路径就会出现在对应的文本框中。

20.8.2颜色选择器JColorChooser

Swing体系用JColorChooser类表示颜色选择器。JColorChooser如同JoptionPane,只需要通过静态方法就能打开一个颜色选择器对话框。打开颜色选择器对话框的方法是showDialog(),它有两个重载版本,如表20-19所示。

表20-19 showDialog()方法

方法

功能

showDialog(Component component,

String title, Color initialColor)

从component上打开颜色选择对话框,对话框标题是title,默认颜色是initialColor

Color showDialog(Component component, String title, Color initialColor, boolean colorTransparencySelectionEnabled)

从component上打开颜色选择对话框,对话框标题是title,默认颜色是initialColor,参数colorTransparencySelectionEnabled指定是否可以选择颜色的透明度

showDialog()方法的返回值类型是Color,它代表用户在颜色选择器上选择的颜色,如果用户点击的是颜色选择器的取消按钮,则showDialog方法()返回null。下面的【例20_28】展示了如何打开颜色选择器,以及如何获得用户所选择的颜色。

【例20_28 JColorChooser的使用】

Exam20_28.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Exam20_28Frame extends JFrame{
    JButton button;
    public Exam20_28Frame(){
        init();
    }
    private void init( ){
        Container container = this.getContentPane();
        container.setLayout(null);
        button = new JButton("颜色选择器");
        button.setSize(200, 40);
        button.setLocation(50,20);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //打开颜色选择器
                Color color = JColorChooser.showDialog(Exam20_28Frame.this,"颜色选择",Color.BLACK);
                System.out.println("所选择的颜色是:"+color);
            }
        });
        container.add(button);
    }
}
public class Exam20_28{
    public static void main(String[] args) {
        Exam20_28Frame frame = new Exam20_28Frame();
        frame.setSize(320, 150);
        frame.setLocationRelativeTo(null);
        frame.setTitle("Exam20_28Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

【例20_28】的运行结果如图20-29所示。

图20-29【例20_28】运行结果

从图20-29所示的窗体上单击“颜色选择器”按钮就能弹出颜色选择器对话框,在对话框上选择一个颜色并单击“确定”按钮就能把选择的颜色输出到控制台上。

除阅读文章外,各位小伙伴还可以点击这里观看我在本站的视频课程学习Java!​

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穆哥细讲Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值