目录
Timer:计时器
JFileChooser:文件选择器
JColorChooser:颜色选择器
BorderFactory:边框
JTable:表格
JOptionPane:标准对话框
JTree:树形控件
Timer:计时器
import java.util.*;public class MainClass {public static void main(String[] args) throws InterruptedException {Timer1(); // task被安排在time指定的时间执行,如果时间为过去时,则任务立即执行Timer2(); // task延迟delay毫秒后执行Timer3(); // 设指定任务task在指定延迟delay后进行固定频率period的执行Timer4(); // 指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行}public static void Timer1(){Date time = new Date();Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {System.out.println("task被安排在time指定的时间执行,如果时间为过去时,则任务立即执行");}},time);}public static void Timer2(){Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {System.out.println("task延迟delay毫秒后执行");}},2000);}public static void Timer3() throws InterruptedException {Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {System.out.println("设指定任务task在指定延迟delay后进行固定频率peroid的执行");}},1000,2000);Thread.sleep(20000);timer.cancel(); // 终止该计时器}public static void Timer4(){// 指定任务执行的时间Calendar calendar = Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY, 23); // 时calendar.set(Calendar.MINUTE, 20);// 分calendar.set(Calendar.SECOND, 0); // 秒Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的23:20:00Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {System.out.println("指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行");}},time,2000);// 每隔两秒执行一次}}
JFileChooser:文件选择器
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.*;public class MainClass{public static void main(String[] args) {new JFileChooser_12();}}class JFileChooser_12 extends JFrame {private JPanel panel;private JTextArea textArea;private JScrollPane scrollPane;private JButton btnOpen,btnSave,btnCancel;public JFileChooser_12(){super("JFileChooser文件对话框");panel = new JPanel();textArea = new JTextArea(20,10);scrollPane = new JScrollPane(textArea);// 滑动面板btnOpen = new JButton("打开");btnSave = new JButton("保存");btnCancel = new JButton("清空");btnOpen.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 实例化文件对话框对象JFileChooser fileChooser = new JFileChooser();// 显示打开文件对话框int val = fileChooser.showOpenDialog(btnOpen);// 如果单击确定if(val == fileChooser.APPROVE_OPTION){// 获取选中文件的名称和当前文件所在的路径String fileName = fileChooser.getSelectedFile().getName();String filePath = fileChooser.getCurrentDirectory().toString();// 创建字符输入流,从文件中将数据读出来并显示到文本域上try {FileReader fr = new FileReader(filePath + "/" + fileName);BufferedReader br = new BufferedReader(fr);String line = br.readLine();while(line != null){textArea.append(line + "\n");line = br.readLine();}br.close();fr.close();} catch (FileNotFoundException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}}});btnSave.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 实例化文件对话框对象JFileChooser fileChooser = new JFileChooser();// 显示保存文件对话框int val = fileChooser.showSaveDialog(btnSave);// 如果点击确定if(val == fileChooser.APPROVE_OPTION){// 获取文件名和文件所在路径String fileName = fileChooser.getSelectedFile().getName();String filePath = fileChooser.getCurrentDirectory().toString();// 创建文件字符输出流,将文本域中的内容写到对应文件中try {FileWriter fw = new FileWriter(filePath + "/" + fileName,true);fw.write(textArea.getText());fw.flush();fw.close();}catch(Exception ev){ev.printStackTrace();}}}});btnCancel.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {textArea.setText("");}});panel.add(btnOpen);panel.add(btnSave);panel.add(btnCancel);this.add(scrollPane);this.add(panel, BorderLayout.SOUTH);this.setSize(400,300);this.setLocation(200,100);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}
JColorChooser:颜色选择器
JColorChoose是一个组件,而不是对话框,但在该类中提供了一个创建颜色对话框的静态方法createDialog()static JDialog createDialog(
component comp, // 对话框的父组件
String title, // 对话框的标题
boolean modal, // 是否是模式对话框
JColorChooser chooser, // 颜色选择器对象
ActionListener okListene, // 对话框中确定按钮的事件监听事件
ActionListener cancelListener // 对话框中取消按钮的事件监听事件
)
import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;class MainClass{public static void main(String[] args) {new JColorChooser_13();} }public class JColorChooser_13 extends JFrame {private JPanel panel;private JButton btnChange;private JDialog colorDialog;private JColorChooser colorChooser;public JColorChooser_13(){super("JColorChooser颜色对话框");panel = new JPanel();btnChange = new JButton("改变面板背景颜色");colorChooser = new JColorChooser();// JColorChooser是一个组件,不是对话框,但它可提供一个创建颜色对话框的静态方法createDialog// 创建一个颜色对话框,颜色选择器作为其中参数之一colorDialog = JColorChooser.createDialog(this,"选取颜色",true,colorChooser,null,null);btnChange.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 设置颜色对话框是可见的colorDialog.setVisible(true);// 设置面板背景颜色panel.setBackground(colorChooser.getColor());}});panel.add(btnChange);this.add(panel);this.setSize(400,300);this.setLocation(200,100);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}
BorderFactory:边框
import javax.swing.*;import javax.swing.border.Border;import java.awt.*;public class MainClass {public static void main(String[] args) {MyJFrame jf = new MyJFrame();}}class MyJFrame extends JFrame{private JPanel panel,panel1,panel2,panel3,panel4,panel5;private Border border1,border2,border3,border4,border5;private JTextArea textArea1,textArea2,textArea3,textArea4,textArea5;public MyJFrame(){super("BorderFactory");panel = new JPanel();panel.setLayout(new GridLayout(5,1));// 1.创建一个指定颜色、颜色宽度的线性边框border1 = BorderFactory.createLineBorder(Color.red,20);panel1 = new JPanel();textArea1 = new JTextArea(10,20);textArea1.setText("1.创建一个指定颜色、颜色宽度的线性边框");panel1.add(textArea1);panel1.setBorder(border1);// 2.创建一个指定标题的文本边框border2 = BorderFactory.createTitledBorder("指定标题的文本边框");panel2 = new JPanel();textArea2 = new JTextArea(10,20);textArea2.setText("2.创建一个指定标题的文本边框");panel2.add(textArea2);panel2.setBorder(border2);// 3.创建一个具有凹入斜面边缘的边框border3 = BorderFactory.createLoweredBevelBorder();panel3 = new JPanel();textArea3 = new JTextArea(10,20);textArea3.setText("3.创建一个具有凹入斜面边缘的边框");panel3.add(textArea3);panel3.setBorder(border3);// 4.创建一个具有凸出斜面边缘的边框border4 = BorderFactory.createRaisedBevelBorder();panel4 = new JPanel();textArea4 = new JTextArea(10,20);textArea4.setText("4.创建一个具有凸出斜面边缘的边框");panel4.add(textArea4);panel4.setBorder(border4);// 5.创建一个具有“浮雕化”外观效果的边框,将组件的当前背景色用于突出显示和阴影显示border5 = BorderFactory.createEtchedBorder(Color.blue,Color.ORANGE);panel5 = new JPanel();textArea5 = new JTextArea(10,20);textArea5.setText("5.创建一个具有“浮雕化”外观效果的边框,将组件的当前背景色用于突出显示和阴影显示");panel5.add(textArea5);panel5.setBorder(border5);panel.add(panel1);panel.add(panel2);panel.add(panel3);panel.add(panel4);panel.add(panel5);this.add(panel);this.setLocation(200,100);this.setSize(600,500);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}
JTable:表格
TableModel接口:表格模式
Java提供了实现TableModel接口的两个类:
●AbstractTableModel类——是一个抽象类,其中实现TableModel接口中的大部分方法,通过AbstractTableModel类可以灵活地构造出自己所需的表格模式;
●DefaultTableModel类——是一个默认的表格模式类,该类继承AbstractTableModel抽象类。
TableColumnModel接口:表格列模式
TableColumnModel接口通常不需要直接实现,而是通过调用JTable对象的getColumnMode()方法来获取TableColummMode对象,再使用该对象操作表格的列
ListSelectionModel接口
JTable使用ListSelection来表示表格的选择状态
程序可以通过ListSelectionModel来控制表格的选择模式
ListSleetionModel 接口提供了以下三种不同的选择模式:
ListSelectionModel. SINGLE SELECTION——单一选择模式,只能选择单个表格单元;ListSelectionModel. SINGLE_ INTERVAL SELECTION——连续区间选择模式,用于选择单个连续区域,在选择多个单元格时单元格之间必须是连续的(通过Shift辅助键的帮助来选择连续区域);ListSelectionModel. MULTIPLE INTERVAL SELECTION——多重选择模式,没有任何限制,可以选择任意表格单元(通过Shift和Ctrl辅助键的帮助选择多个单元格),该模式是默认的选择模式。
LstSletionModel接口通常不需要直接实现,而是通过调用JTable对象的getSeletionModel()方法来获取ListSlectionModel对象,然后通过该对象的setSelectionModel()方法来设置表格的选择模式
当用户选择表格内的数据时会产生ListSelection事件,要处理此类事件,就必须实现ListSlection监听接口的一 个事件处理方法:
JOptionPane:标准对话框
1.消息对话框
JOptionPane. showMessageDialog():显示消息对话框
2.输入对话框
JOptionPane. showInputDialog():显示输入对话框
3.确认对话框
JOptionPane. showConfirmDialog():显示确认对话框
4.选项对话框
JOptionPane. showOptionDialog():显示选项对话框
int showOptionDialog (
Component parentComponent, // 父组件
Object message, // 消息
String title, // 标题
int optiontype,// 选项类型
int messageType, // 消息类型
Icon icon, // 图标类型
0bject[ ] options, // 对话框内容
object initialValue // 对话框初始值
)
package tes_8;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class MainClass{public static void main(String[] args) {new JOptionPane_11();}}class JOptionPane_11 extends JFrame {private JPanel panel;private JTextArea textArea;private JButton btnInput,btnMessage,btnComfirm,btnOption;public JOptionPane_11(){super("JOptionPane标准对话框");panel = new JPanel();textArea = new JTextArea(20,10);btnInput = new JButton("输入");btnMessage = new JButton("消息");btnComfirm = new JButton("确认");btnOption = new JButton("选项");btnInput.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String inputValue = JOptionPane.showInputDialog(btnInput,"请输入一个数字","输入对话框",JOptionPane.PLAIN_MESSAGE);try{int value = Integer.parseInt(inputValue.trim());textArea.append(value + "*" + value + " = " + value*value+"\n");}catch(Exception ev){textArea.append(inputValue + "不是数字\n");}textArea.append("===输入对话框===\n");}});btnMessage.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JOptionPane.showMessageDialog(btnMessage,"下午两点开准时睡觉!","消息对话框",JOptionPane.INFORMATION_MESSAGE);textArea.append("===消息对话框===\n");}});btnComfirm.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {int value = JOptionPane.showConfirmDialog(btnComfirm,"您确定要删除吗?","确认对话框", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);if(value == JOptionPane.YES_OPTION){textArea.append("你选择了:是\n");}else{textArea.append("你选择了:否\n");}textArea.append("===确认对话框===\n");}});btnOption.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Object[] options = {"Red","Green","Blue"};// 返回的是数组元素对应的下标int index = JOptionPane.showOptionDialog(btnOption,"选择颜色","选项对话框",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);if(index != JOptionPane.CLOSED_OPTION){textArea.append("您选择的颜色是:" + (String) options[index] + "\n");}textArea.append("===选项对话框===\n");}});panel.add(btnInput);panel.add(btnMessage);panel.add(btnComfirm);panel.add(btnOption);this.add(textArea);this.add(panel, BorderLayout.SOUTH);this.setSize(400,300);this.setLocation(200,100);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}
JTree:树形控件
TreeModel树模型
Java提供了一个实现TreeModel接口的默认模式类DefaultTreeModel
构造方法:
new DefaultTreeModel(TreeNode root)——创建一个指定根节点的DefaultTreeModel对象new DefaultModel(TreeNode node,Boolean isAllowsChild)——创建一个指定根节点的、是否具有子节点的DefaultTreeModel对象
TreeNode树节点
DefaultMutableTreeNode是一个实现了TreeNode和MutableTreeNode接口的类
该类提供的处理方法如下:
树事件(TreeModelEvent事件和TreeSelectionEvent事件)
TreeModelEvent事件的监听接口:TreeModelListener
TreeSelectionEvent事件的监听接口:TreeSelectionListener
package tes_9;import javax.swing.*;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.tree.*;import java.awt.*;public class MainClass{public static void main(String[] args) {new JTree_17();}}class JTree_17 extends JFrame{private JPanel panel;private JTextArea textArea;private JTree tree;private DefaultMutableTreeNode root;private DefaultTreeModel model;public JTree_17(){super("JTree树");textArea = new JTextArea(20,10);panel = new JPanel(new GridLayout(1,2));// 通过自定义方法返回一个根结点root = MakeRootNode();// 创建一个指定根结点的树模型model = new DefaultTreeModel(root);// 根据树模型构造出整棵树tree = new JTree(model);// 设置树的选择模式为单一节点的选择模式tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);// 注册树的监听对象,监听选择不同的树节点tree.addTreeSelectionListener(new TreeSelectionListener() {public void valueChanged(TreeSelectionEvent e) {// 获取选中节点的路径TreePath path = tree.getSelectionPath();if(path == null){return;}// 根据路径,获取选中的节点对象DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode)path.getLastPathComponent();// 获取选中节点的内容,并展示到文本域中textArea.setText(selectNode.getUserObject().toString());}});// 面板左侧放树panel.add(new JScrollPane(tree));// 面板右侧放文本域panel.add(new JScrollPane(textArea));this.add(panel);this.setSize(400,300);this.setLocation(200,100);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}private DefaultMutableTreeNode MakeRootNode() {DefaultMutableTreeNode treeNode1,treeNode2,treeNode3_1,treeNode3_2,treeNode4_1,treeNode4_2,treeNode4_3;// 创建指定内容的树节点treeNode1 = new DefaultMutableTreeNode("根结点");treeNode2 = new DefaultMutableTreeNode("子结点");treeNode3_1 = new DefaultMutableTreeNode("子结点1");treeNode3_2 = new DefaultMutableTreeNode("子结点2");treeNode4_1 = new DefaultMutableTreeNode("子结点1的内容_1");treeNode4_2 = new DefaultMutableTreeNode("子结点1的内容_2");treeNode4_3 = new DefaultMutableTreeNode("子结点2的内容");treeNode1.add(treeNode2);treeNode2.add(treeNode3_1);treeNode2.add(treeNode3_2);treeNode3_1.add(treeNode4_1);treeNode3_1.add(treeNode4_2);treeNode3_2.add(treeNode4_3);return treeNode1;}}
(27条消息) Swing UI——容器(一)_Stuttering Guy的博客-CSDN博客/Mr_Morgans/article/details/125109643?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125109643%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=l9JMT(27条消息) Swing UI——基本组件(二)_Stuttering Guy的博客-CSDN博客/Mr_Morgans/article/details/125110881?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125110881%22%2C%22source%22%3A%22Mr_Morgans%22%7D&ctrtid=Kzbcx(27条消息) Swing UI——布局管理器(四)_Stuttering Guy的博客-CSDN博客/Mr_Morgans/article/details/125115409?spm=1001..3001.5501(27条消息) Swing UI——事件处理(五)_Stuttering Guy的博客-CSDN博客/Mr_Morgans/article/details/125115417?spm=1001..3001.5501