目录
| 构造方法 | 描述 |
| ImageIcon() | 创建一个未初始化的图标对象 |
| ImageIcon(Image image) | 根据图像创建图标 |
| ImageIcon(String fineName) | 根据指定的图片文件创建图标对象 |
|
|
|
| 常用方法 | 功能 |
| int getIconWidth() | 获取图标宽度 |
| int getIconHeight() | 获取图标高度 |
| Image getImage() | 返回此图标Image对象 |
| void setImage(Image image) | 设置图标显示的image对象 |
| void printIcon(Component comp,Graphics graph, int x, int y) | 绘制图标 |
- import javax.swing.*;
- import java.awt.*;
- public class ImageIcon_MainClass{
- public static void main(String[] args) {
- Icon_ImageIcon icon_imageIcon = new Icon_ImageIcon();
- }
- }
- class Icon_ImageIcon extends JFrame {
- private JPanel panel;
- private ImageIcon imageIcon;
- public Icon_ImageIcon(){
- super("Icon接口——ImageIcon类");
- panel = new JPanel();
- imageIcon = new ImageIcon("images\\2.png"); // 创建ImageIcon图标
- this.setIconImage(imageIcon.getImage()); // 设置窗体的Icon
- this.add(panel);
- this.setSize(500,300);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void paint(Graphics graphics){
- ImageIcon imageIcon = new ImageIcon("images\\1.png"); // 创建ImageIcon图标
- graphics.drawImage(imageIcon.getImage(),0,20,this); // 在窗体中画图标
- graphics.drawString("宽:"+imageIcon.getIconWidth()+"px," + // 显示图标的宽度和高度
- "高:" +imageIcon.getIconHeight()+"px",
- 20,210);
- }
- }


| 构造方法 | 描述 |
| new JLabel( String str) | 创建指定文本的标签对象 |
| new JLabel( Icon icon) | 创建指定图标的标签对象 |
| new JLabel(String str,ICon icon,int horizontalAlignment) | 创建指定文本、图标、对齐方式的标签对象 |
|
|
|
| 常用方法 | 功能 |
| void setText() | 设置标签上的文本 |
| String getText() | 获取标签上的文本 |
| void setIcon(Icon icon) | 设置标签上的图像 |
| Icon getIcon() | 获取标签中的图像 |
| void setHorizontalAlignment(int alignmen) | 设置标签上文本的对齐方式 |
| void setVisible(boolean b) | 设置标签是否可见 |
- import javax.swing.*;
- import java.awt.*;
- public class JLabel_3{
- public static void main(String[] args) {
- MyJLabel myJLabel = new MyJLabel();
- }
- }
- class MyJLabel extends JFrame {
- private JPanel panel;
- private JLabel textLabel,iconLabel,addLabel;
- public MyJLabel(){
- super("JLabel");
- panel = new JPanel(new GridLayout(3,1));
- textLabel = new JLabel("这是一个文本标签");
- iconLabel = new JLabel(new ImageIcon("images\\e.png"));
- addLabel = new JLabel("商标",new ImageIcon("images\\e.png"),SwingConstants.CENTER);
- panel.add(textLabel);
- panel.add(iconLabel);
- panel.add(addLabel);
- this.add(panel);
- this.setSize(400,300);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JTextField(int length) | 指定长度的文本框 |
| TextField(String str) | 指定初始内容的文本框 |
| JTextField(String str,int length) | 指定初始内容和长度的文本框 |
|
|
|
| 常用方法 | 功能 |
| void setText(String s) | 设置文本框中的文本 |
| String getText() | 返回文本框中的文本 |
| String getSelectedText() | 返回被选定的文本 |
| void setEditable(boolean b) | 设置文本框是否可以编辑 |
| void setEnabled(boolean b) | 设置启用(禁用)文本框 |
| void setEchoChar(char c) | 设置回显字符 |
| void setHorizontalAlignment(int alignment) | 设置文本框中的文本对齐方式 取值: JTextField.LEFT JTextField.CENTER JTextField.Right |
- import javax.swing.*;
-
- public class Text_3{
- public static void main(String[] args) {
- MyJText myJLabel = new MyJText();
- }
- }
- class MyJText extends JFrame {
- private JLabel label;
- private JTextField textField;
- private JPanel panel;
- public MyJText(){
- super("JTextArea");
- panel = new JPanel();
- label = new JLabel("文本框:");
- textField = new JTextField(10); // 指定长度的文本框
- textField.setText("文本框的内容");
- panel.add(label);
- panel.add(textField);
- this.add(panel);
- this.setSize(400,300);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JTextArea(int row,int columns) | 指定行数、列数的文本域 |
| new JTextArea(String str) | 指定初始内容的文本域 |
| new JTextArea(String str,int rows, int columns) | 指定初始内容、行数、列数的文本域 |
|
|
|
| 常用方法 | 功能 |
| void setText(String s) | 设置文本域中的文本 |
| String getText() | 返回文本域中的文本 |
- import javax.swing.*;
-
- public class Text_3{
- public static void main(String[] args) {
- MyJText myJLabel = new MyJText();
- }
- }
- class MyJText extends JFrame {
- private JTextArea textArea;
- private JLabel label;
- private JPanel panel;
- public MyJText(){
- super("JTextArea");
- panel = new JPanel();
- label = new JLabel("文本域:");
- textArea = new JTextArea(10,20);
- textArea.setText("文本框的内容");
- this.add(label);
- panel.add(textArea);
- this.add(panel);
- this.setSize(400,300);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JPasswordField(int columns) | 指定长度 |
| new JPasswordField(String str) | 指定初始内容 |
| newJPasswordField(String str,int columns) | 指定初始内容和长度 |
|
|
|
| 常用方法 | 功能 |
| void addActionListener(ActionListener) | 添加一个事件监听器 |
| char[ ] getPassword( ) | 返回一个字符数组,该数组保存的是输入的密码 |
| void setEchoChar(char ch) | 设置回显字符 |
| char getEchoChar() | 返回回显字符 |
- package tes_5;
-
- import javax.swing.*;
-
- public class MainClass {
- public static void main(String[] args) {
- MyFrame myFrame = new MyFrame();
- }
- }
- class MyFrame extends JFrame {
- private JPanel panel;
- private JLabel label;
- private JPasswordField passwordField;
- public MyFrame(){
- super("-----密码框-----");
- panel = new JPanel();
- label = new JLabel("密码框:");
- passwordField = new JPasswordField(10); // 创建一个指定长度的密码框
- passwordField.setEchoChar('*'); // 设置回显字符
- panel.add(passwordField);
- panel.add(label);
- this.add(panel);
- this.setLocation(300,400);
- this.setSize(300,150);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JButton(String str) | 创建一个指定文本的按钮 |
| new JButton(ICon icon); | 创建一个指定图标的按钮 |
| new JButton(String str, Icon icon) | 创建一个指定文本、指定图标的按钮 |
|
|
|
| 常用方法 | 功能 |
| void setActionCommand(String) | 给一个JButton设置一个属性的字符串值,然后通过在actionPerformed(ActionEvent e)方法里通过if(e.getActioncommand.equals("改变"))判断哪个按钮发生了事件 |
| void addActionListener(ActionListener) | 接收操作事件的监视器 |
| void setText() | 设置按钮上的文本 |
| void getText() | 获取按钮上的文本 |
| void setBackground(Color bg) | 设置按钮的背景色 |
| Color getBackground() | 获得按钮的背景色 |
| void setEnabled(boolean b) | 设置启用(禁用)按钮,由参数b决定 |
| void setVisible(boolean b) | 设置按钮是否可见,由参数b决定 |
| void setToolTipText(String text) | 设置按钮的悬停提示信息 |
| void setMnemonic(int munemonic) | 设置按钮的快捷键 |
- import javax.swing.*;
- import java.awt.event.*;
-
- public class MainClass{
- public static void main(String[] args) {
- new JButton_4();
- }
- }
-
- class JButton_4 extends JFrame {
- private JPanel panel;
- private JButton TextButton,IconButton;
- public JButton_4(){
- super("JButton");
- panel = new JPanel();
- TextButton = new JButton("您点击了0次按钮!"); // 指定文本的按钮
- IconButton = new JButton(new ImageIcon("images\\dialog.png")); // 指定图标的按钮
- panel.add(TextButton);
- panel.add(IconButton);
- this.add(panel);
- this.setSize(400,400);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
-
-
- }

| 构造方法 | 描述 |
| new JRadioButton(String str) | 指定文本的单选按钮 |
| new JRadioButton(String str,boolean state) | 指定文本和选择状态的单选按钮 |
| new JRadioButton(String str,Icon icon,boolean state) | 指定文本、图标、选择状态的单选按钮 |
|
|
|
| 常用方法 | 功能 |
| void addActionListener(ActionListener) | 接收操作事件的监视器 |
| void addItemListener(ItemListener) | 选项事件监听 |
| boolean isSelected( ) | 返回单选框的状态 |
| void setSelected(boolean) | 设置单选框的状态 |
| 按钮组 | 常用方法 |
| void clearSelection() | 清空所有单选按钮的选中状态 |
- public class MainClass{
- public static void main(String[] args) {
- new JRadioButton_9();
- }
- }
- class JRadioButton_9 extends JFrame {
- private JPanel panelSex;
- private JLabel labelSex;
- private ButtonGroup buttonGroup;
- private JRadioButton buttonMale,buttonFemale;
- public JRadioButton_JCheckBox_9(){
- super("JRadioButton");
- panelSex = new JPanel(new FlowLayout(FlowLayout.LEFT));
- labelSex = new JLabel("性别:");
- buttonMale = new JRadioButton("男",true); // 性别为男的单选按钮被选中
- buttonFemale = new JRadioButton("女");
- buttonGroup = new ButtonGroup();
- buttonGroup.add(buttonMale);
- buttonGroup.add(buttonFemale);
- panelSex.add(labelSex);
- panelSex.add(buttonMale);
- panelSex.add(buttonFemale);
- this.add(panelSex);
- this.setSize(300,100);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
-
- }

| 构造方法 | 描述 |
| JCheckbox() | 创建一个没有标签的复选框 |
| JCheckbox( Icon icon) | 创建一个有图标的复选框 |
| JCheckbox(Icon icon ,boolean sele ) | 创建一个有图标icon的复选框,初始状态为sels |
| JCheckbox( String s) | 创建一个有标签的复选框 |
| JCheckbox( String s,boolean b) | 创建一个有标签的复选框,参数b设置初始状态 |
| JCheckbox(String str ,Icon icon ) | 创建一个有str文字及图标icon的复选框 |
| JCheckbox(String str,Iconicon,boolean sele) | 创建一个有str文字及图标icon的复选框,初始状态为sels |
|
|
|
| 常用方法 | 功能 |
| isSelected() | 返回复选按钮的状态 |
| setSelected (Boolean state) | 设置复选按钮的状态 |
- package tes_8;
-
- import javax.swing.*;
- import java.awt.*;
- public class MainClass{
- public static void main(String[] args) {
- new JCheckBox_9();
- }
- }
- class JCheckBox_9 extends JFrame {
- private JPanel panelHobby;
- private JLabel labelHobby;
- private JCheckBox boxRead,boxOnline,boxSwim,boxTravel;
- public JCheckBox_9(){
- super("JCheckBox");
- panelHobby = new JPanel(new FlowLayout(FlowLayout.LEFT));
- labelHobby = new JLabel("爱好:");
- boxRead = new JCheckBox("阅读",true); // 阅读的复选框被选中
- boxOnline = new JCheckBox("上网");
- boxSwim = new JCheckBox("游泳",true); // 游泳的复选框被选中
- boxTravel = new JCheckBox("旅游");
- panelHobby.add(labelHobby);
- panelHobby.add(boxRead);
- panelHobby.add(boxOnline);
- panelHobby.add(boxSwim);
- panelHobby.add(boxTravel);
- this.add(panelHobby);
- this.setSize(300,100);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JList() | 创建一个空的列表框 |
| new JList(String[ ] str) | 创建一个指定数组内容的列表框 |
| new JList(Vector vector); | 创建一个指定集合内容的列表框 |
|
|
|
| 常用方法 | 功能 |
| void addListSelectionListener( ) | 选择事件监视器 |
| void setSelectionMode(int) | 列表使用选择器,取值如下(在ListSelectionModel中定义): SINGLE_SELECTION SINGLE_INTERVAL_SELECTION MULTIPLE_INTERVAL_SELECTION(默认) |
| int getSelectedIndex( ) | 返回被选中的所有元素中最小的index |
| int[ ] getSelectedIndices( ) | 返回一个整型数组,包含被选中的所有index |
| Object getSelectedValue( ) | 返回被选中的,index最小的元素值 |
| Object[ ] getSelectedValues( ) | 返回一个object数组,包含被选中的所有元素对象 |
| void clearSelection( ) | 清除被选中 |
| boolean isSelectedIndex(int index) | 判断指定index是否被选中 |
- 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 JList_8();
- }
- }
-
- class JList_8 extends JFrame {
- private JPanel panel;
- private JList list1,list2;
- private JButton button1,button2;
- private DefaultListModel model;
- /*
- DefaultListModel类用于创建一个默认的列表值模型,
- 使用值模型可以对列表中的选项内容进行添加或删除操作
- */
- public JList_8(){
- super("JList");
- setLayout(new GridLayout(1,3));
- panel = new JPanel(new GridLayout(2,1));
- button1 = new JButton("→");
- button2 = new JButton("←");
- model = new DefaultListModel();
- list1 = new JList(new String[]{"看书","写字","画画","爬山","跑步","游泳"}); //指定内容的列表
- list2 = new JList(); // 空列表
- list2.setModel(model);
- panel.add(button1);
- panel.add(button2);
- add(list1);
- add(panel);
- add(list2);
- setSize(500,300);
- setLocation(500,250);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JComboBox() | 创建一个空的下拉列表 |
| new JComboBox(String[ ] str) | 创建一个指定数组内容的下拉列表 |
| new JComboBox(Vector vector) | 创建一个指定集合内容的下拉列表 |
|
|
|
| 常用方法 | 功能 |
| addItem( ) | 添加一个项目到 JComboBox |
| get/setSelectedIndex( ) | 获取/设置 JComboBox 中选中项目的索引 |
| get/setSelectedItem( ) | 获取/设置选中的对象 |
| removeAllItems( ) | 从 JComboBox 删除所有对象 |
| removeItem( int index) | 从 JComboBox 删除特定对象 |
| setEditable() | 把一个组合框设置为可编辑的 |
注意编辑只会影响当前项,它不会改变列表的内容
-
- import javax.swing.*;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
-
- public class MainClass{
- public static void main(String[] args) {
- new JComboBox_7();
- }
- }
-
- class JComboBox_7 extends JFrame {
- private JPanel panel;
- private JLabel label1,label2;
- private JComboBox comboBox1,comboBox2;
- public JComboBox_7(){
- super("JComboBox");
- panel = new JPanel();
- label1 = new JLabel("省份");
- label2 = new JLabel("城市");
- comboBox1 = new JComboBox(new String[]{"北京","上海","广东","安徽"});// 指定数组内容的下拉列表
- comboBox2 = new JComboBox(); // 空的下拉列表
- panel.add(label1);
- panel.add(comboBox1);
- panel.add(label2);
- panel.add(comboBox2);
- this.add(panel);
- this.setSize(300,200);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
-
-
- }

| 构造方法 | 描述 |
| new JMenuBar() | 创建一个空的JMenuBar对象 |
|
|
|
| 常用方法 | 功能 |
| setJMenuBar(menuBar) | 将菜单栏对象添加到窗口的顶部 |
| 构造方法 | 描述 |
| new JMenu() | 创建一个空的菜单对象 |
| new JMenu(String str) | 创建一个指定文本的菜单对象 |
| new JMenu(String str, boolean bool) | 创建一个指定文本的、是否分离式的菜单对象 |
|
|
|
| 常用方法 | 功能 |
| Component add(Component c) | 在菜单末尾添加组件,通常添加JMenuItem菜单项,也可以添加子菜单 |
| void addSeparator( ) | 在菜单末尾添加分隔线 |
| void addMenuListener( MenuListener e) | 添加菜单监听 |
| JMenuItem getItem(int index) | 返回指定索引处的菜单项,索引从0开始 |
| int getItemCount() | 返回菜单项的数目 |
| JMenulItem insert(JMenuItem item, int index) | 在指定索引处插人菜单项 |
| void insertSeparator(int index) | 在指定索引处插人分割线 |
| vold remove(int index) | 移除指定索引处的菜单项 |
| void remove(Component c) | 移除指定组件 |
| void removeAll( ) | 移除菜单中的所有组件 |
| 构造方法 | 描述 |
| new JMenuItem() | 创建一个空的菜单项 |
| new JMenuItem( Icon icon) | 创建一个指定图标的菜单项 |
| new JMenuItem( String text) | 创建一个指定文本的菜单项 |
| new JMenuItem(String text, Icon icon) | 创建一个指定文本和图标的菜单项 |
|
|
|
| 常用方法 | 功能 |
| void addActionListener( ActionListener e) | 从AbstractButton类中继承的方法,将监听对象添加到菜单项中 |
| void setIcon( Icon icon) | 设置图标 |
| void setText(String text) | 设置文本 |
-
- import javax.swing.*;
-
- public class MainClass{
- public static void main(String[] args) {
- new JMenu_14();
- }
- }
- class JMenu_14 extends JFrame {
- private JMenuBar menuBar;
- private JMenu menuFile,menuEdit,menuHelp,menuNew;
- private JMenuItem itemSave,itemExit,itemCopy,itemPost,
- itemAbout,itemClass,itemJava,itemOther;
- public JMenu_14(){
- super("下拉式菜单");
- menuBar = new JMenuBar(); // 造一个菜单栏对象
-
- menuFile = new JMenu("File"); // 造菜单
- menuEdit = new JMenu("Edit");
- menuHelp = new JMenu("Help");
- menuNew = new JMenu("New");
-
- itemSave = new JMenuItem("Save");// 造菜单项
- itemExit = new JMenuItem("Exit");
- itemCopy = new JMenuItem("Copy");
- itemPost = new JMenuItem("Post");
- itemAbout = new JMenuItem("About");
- itemClass = new JMenuItem("Class");
- itemJava = new JMenuItem("Java Script");
- itemOther = new JMenuItem("Other....");
-
- // 将菜单添加到菜单栏
- menuBar.add(menuFile);
- menuBar.add(menuEdit);
- menuBar.add(menuHelp);
-
- // 将子菜单、菜单项添加到菜单
- menuFile.add(menuNew);
- menuFile.addSeparator(); // 添加一条分割线
- menuFile.add(itemSave);
- menuFile.add(itemExit);
- menuNew.add(itemClass);
- menuNew.add(itemJava);
- menuNew.add(itemOther);
- menuEdit.add(itemCopy);
- menuEdit.add(itemPost);
- menuHelp.add(itemAbout);
- this.setJMenuBar(menuBar); // 将菜单栏添加到窗体顶部
- this.setSize(400,300);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JPopupMenu() | 创建一个默认无文本的菜单对象 |
| new JPopupMenu(String label) | 创建一个指定文本的菜单对象 |
|
|
|
| 常用方法 | 功能 |
| Component add(Component comp) | 在菜单末尾添加组件 |
| void addSeparator() | 在菜单末尾添加分割线 |
| void show(Component comp, int x, inty) | 在调用者的指定位置显示弹出菜单 |
-
- import javax.swing.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
-
- public class MainClass{
- public static void main(String[] args) {
- new JPopupMenu_15();
- }
- }
- class JPopupMenu_15 extends JFrame {
- private JPanel panel;
- private JPopupMenu popupMenu;
- private JMenuItem Undo,Copy,Post,Cut;
- public JPopupMenu_15(){
- super("JPopupMenu弹出式菜单");
- panel = new JPanel();
- popupMenu = new JPopupMenu(); // 造一个弹出式菜单对象
- Undo = new JMenuItem("Undo");
- Copy = new JMenuItem("Copy");
- Post = new JMenuItem("Post");
- Cut = new JMenuItem("Cut");
-
- // 将菜单项添加到弹出式菜单
- popupMenu.add(Undo);
- popupMenu.addSeparator();
- popupMenu.add(Copy);
- popupMenu.add(Post);
- popupMenu.add(Cut);
-
-
- // 事件监听(请看其它章节)
- // 在面板任意位置右击的时候,显示弹出式菜单
- panel.addMouseListener(new MouseAdapter(){
- @Override
- public void mouseClicked(MouseEvent e) {
- if(e.getButton() == MouseEvent.BUTTON3){
- int x = e.getX();
- int y = e.getY();
- popupMenu.show(panel,x,y);
- }
- }
- });
- this.add(panel);
- this.setSize(400,300);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JToolBar() | 创建默认方向为HORIZONTAL(水平)的工具栏 |
| new JToolBar(int orientation) | 创建一个指定方向的工具栏 |
| new JToolBar( String name) | 创建一个指定名称的工具栏 |
| new JToolBar(String name, int orientation) | 创建一个指定名称和方向的工具栏 |
|
|
|
| 常用方法 | 功能 |
| public Component add(Component c) | 在工具栏中添加组件 |
| public void addSeparator() | 将分隔线添加到工具栏的末尾 |
| public void setMargin( Insets m) | 设置工具栏边框与按钮之间的空白 |
| public Insets getMargin() | 返回工具栏边框与按钮之间的空白 |
- package tes_9;
-
- import javax.swing.*;
- import java.awt.*;
-
- public class MainClass{
- public static void main(String[] args) {
- new JToolBar_16();
- }
- }
-
- class JToolBar_16 extends JFrame {
- private JToolBar toolBar;
- private JButton btnSave,btnPreview,btnDownload,btnSelect,btnDelete;
- public JToolBar_16(){
- super("JToolBar工具栏");
- toolBar = new JToolBar(); // 造一个工具栏对象
-
- // 指定按钮的内容和图标
- btnSave = new JButton("保存",new ImageIcon("images/5.jpg"));
- btnPreview = new JButton("预览",new ImageIcon("images/3.jpg"));
- btnDownload = new JButton("下载",new ImageIcon("images/4.jpg"));
- btnSelect = new JButton("查询",new ImageIcon("images/2.jpg"));
- btnDelete = new JButton("删除",new ImageIcon("images/1.jpg"));
-
- // 设置工具提示文本
- btnSave.setToolTipText("保存");
- btnPreview.setToolTipText("预览");
- btnDownload.setToolTipText("下载");
- btnSelect.setToolTipText("查询");
- btnDelete.setToolTipText("删除");
-
- // 将按钮添加到工具栏
- toolBar.add(btnSave);
- toolBar.add(btnPreview);
- toolBar.add(btnDownload);
- toolBar.add(btnSelect);
- toolBar.add(btnDelete);
-
- this.add(toolBar, BorderLayout.NORTH);// 将工具栏添加到窗体北边
- this.setSize(400,200);
- this.setLocation(200,100);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JProgressBar() | 创建一个空的进度条 |
| new JProgressBar(BoundedRangeModel newModel) | 创建指定的保存进度条数据模型的水平进度条 |
| new JProgressBar( JProgressBar. HORIZONTAL或JProgressBar. VERTICAL) | 创建具有指定方向的进度条 |
| new JProgressBar(int min, int max) | 创建具有指定最小值和最大值的水平进度条 |
| new JProgressBar(int orient, int min, int max) | 创建使用指定方向、最小值和最大值的进度条 |
|
|
|
| 常用方法 | 功能 |
| void addChangeListener(ChangeListener) | 添加进度条值改变的监视器 |
| void setValue(int) | 设置当前值 |
| int getValue() | 返回进度条的当前值 |
| void setString(String) | 设置显示的字符串 |
| String getString() | 返回进度字符串的当前值 |
| void setStringPainted(boolean) | 设置是否显示字符串,默认为false |
| void setBorderPainted(boolean) | 设置是否显示边框,默认为true |
| void setMaximum(int n) | 将进度条的最大值(存储在进度条的数据模型中)设置为 n |
| int getMaximum() | 返回进度条的最大值,该值存储在进度条的 BoundedRangeModel 中 |
| setMinimum(int n) | 将进度条的最小值(存储在进度条的数据模型中)设置为 n |
| int getMinimum() | 返回进度条的最小值,该值存储在进度条的 BoundedRangeModel 中 |
| setModel(BoundedRangeModel newModel) | 设置 JProgressBar 使用的数据模型 |
| getModel() | 返回此进度条使用的数据模型 |
| setUI(ProgressBarUI ui) | 设置呈现此组件的外观对象 |
| setStringPainted(boolean b) | 设置 stringPainted 属性的值,该属性确定进度条是否应该呈现进度字符串 |
| setPreferredSize(new Dimension(400,50)) | 设置进度条大小 |
- package tes_5;
-
- import javax.swing.*;
-
- public class MainClass {
- public static void main(String[] args) {
- MyFrame myFrame = new MyFrame();
- }
- }
- class MyFrame extends JFrame {
- private JPanel panel;
- private JLabel label;
- private JProgressBar progressBar;
- public MyFrame(){
- super("JProgressBar进度条");
- panel = new JPanel();
- label = new JLabel("进度条:");
- progressBar = new JProgressBar(0,100);// 指定最小值、最大值的水平进度条
- progressBar.setValue(50); // 设置水平条当前值
- panel.add(label);
- panel.add(progressBar);
- this.add(panel);
- this.setLocation(300,400);
- this.setSize(300,150);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JSlider() | 创建一个空的滑块 |
| new JSlider(int orientation) | 创建指定方向的滑块 JSlider. HORIZONTAL JSlider VERTICAL... |
| new JSlider(int min, int max) | 创建具有一个最小值、最大值的滑块 |
| new JSlider(int orientation, int minValue, int maxValue, int initialValue) | 创建指定方向、最小值、最大值、初始值的滑块 指定方向: JSlider. HORIZONTAL JSlider VERTICAL...
|
|
|
|
| 常用方法 | 功能 |
| void addChangeListener(ChangeListener) | 添加滑块值改变的监视器 |
| void setValue(int) | 设置滑块的当前值 |
| int getValue() | 获取滑块的当前值 |
| void setMajorTickSpacing(int) | 设置主刻度标记间隔 |
| void setMinorTickSpacing(int) | 设置单个主刻度内的一次刻度标记间隔 |
| void setPaintTicks(boolean); //false | 设置是否绘制刻度线 |
| void setPaintLabels(boolean); //false | 设置是否绘制刻度标签(刻度值文本) |
| void setPaintTrack(boolean); //true | 设置是否绘制滑道 |
| void setSnapToTicks(boolean); //false | 设置滑块是否对齐刻度,设置为 true,则滑块最终只能在有刻度的位置取值,即滑块取值不连续。 |
| void setInverted(boolean b) | 设置是否颠倒刻度值(刻度值从大到小) |
| void setOrientation(int orientation)
| 设置滑块的方向:SwingConstants.VERTICAL SwingConstants.HORIZONTAL |
-
-
- import javax.swing.*;
-
- public class MainClass {
- public static void main(String[] args) {
- MyFrame myFrame = new MyFrame();
- }
- }
- class MyFrame extends JFrame {
- private JPanel panel;
- private JLabel label;
- private JSlider slider;
- public MyFrame(){
- super("JSlider滑块");
- panel = new JPanel();
- label = new JLabel("滑块:");
- slider = new JSlider(0,100);// 指定最小值、最大值的滑块
- slider.setValue(75); // 设置当前滑块的值
- panel.add(label);
- panel.add(slider);
- this.add(panel);
- this.setLocation(300,400);
- this.setSize(300,150);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- }

| 构造方法 | 描述 |
| new JSpinner( ) | 创建一个空的微调器 |
|
|
|
| 常用方法 | 功能 |
| void addChangeListener(ChangeListener e) | 为每次发生模型更改时要通知的列表添加监视器 |
| void setValue(Object val) | 设置当前值 |
| Object getValue () | 返回当前值 |
| Object getNextValue( ) | 返回下一个值 |
| Object getPreviousValue( ) | 返回前一个值 |
(27条消息) Swing UI——容器(一)_Stuttering Guy的博客-CSDN博客
https://blog.csdn.net/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博客
https://blog.csdn.net/Mr_Morgans/article/details/125115383?spm=1001.2014.3001.5501(27条消息) Swing UI——布局管理器(四)_Stuttering Guy的博客-CSDN博客
https://blog.csdn.net/Mr_Morgans/article/details/125115409?spm=1001.2014.3001.5501(27条消息) Swing UI——事件处理(五)_Stuttering Guy的博客-CSDN博客
https://blog.csdn.net/Mr_Morgans/article/details/125115417?spm=1001.2014.3001.5501