Swing——跨平台运行(继承JFrame)
—————为什么JFrame不能之间添加组件 需要调用getContentPane()方法?
————getContentPane()作用:初始化一个容器,用来在容器上添加一些控件
JFrame代码与 Frame 的代码十分相似 唯一的区别在于,你不能将组件加入到JFrame中。你可以将组件加入到JFrame 的contentpane 中
contentpane :包含除菜单条外所有框架的可视组件的容器
要获得一个JFrame 的contentpane,可使用getContentPane()方法
eg:Container container = getContentPane();container.add(jLabel); ———————— 标签放在容器里container.setBackground(Color.blue);——————设置背景颜色
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)-----------关闭窗口
jlabel.setHorizontalAlignment(SwingConstants.CENTER);——标签居中
Swing是AWT的进阶版 界面更好看警告:Swing 不是线程安全的。把依赖于本地平台的AWT组件称为重量级组件把不依赖于本地平台的Swing组件称为轻量级组件总框架是JFrame在框架之中添加各种组件JComponent——用于显示某一部分 eg:面板JPanel,标签JTable,文本域JTextArea等
窗口JFrame
顶级窗口——用于短文本字符串或图像或二者的显示区 还可以指定文本相对于图像的位置。默认情况下,文本位于图像的结尾边上,文本和图像都垂直对齐。提供了关闭窗口的功能 不需要添加窗口监听器 只需调用void setDefaultCloseOperation ( )——退出应用程序后的默认窗口关闭操作。
JFrame.EXIT_ON_CLOSE常量作为参数传入即可
WindowConstants——用于控制窗口关闭操作的常量
eg:jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JFrame设置背景为何不显示?
————窗口背景颜色是指直接调用JFrame的setBackgroud( )方法设置的颜色
但显示的却是JFrame.getContentPane( )的颜色——默认为白色
————解决方法
——得到一个ContetPane容器的实例container 给实例上色
Container container = getContentPane(); container.setBackground(Color.yellow);
——无标签的情况下设置背景颜色
getContentPane().setVisible(false);
public class JFrameDemo2 {public static void main(String[] args) {new MyJFrameDemo().init();}}class MyJFrameDemo extends JFrame {public void init(){setTitle("ZY的小标签");setBounds(200,200,400,400);setVisible(true);JLabel jLabel = new JLabel("一是婴儿哭啼 " +"二是学游戏 " +"三是青春物语" +" 四是刚好遇见你");add(jLabel);//setBackground(Color.blue);// 标签置中jLabel.setHorizontalAlignment(SwingConstants.CENTER);// 获得一个容器实例 上色Container container = getContentPane();container.setBackground(Color.yellow);// getContentPane().setVisible(false);}}
弹窗JDialog
定义:创建对话框窗口的主要类
JDialog对话框可分为两种:模态对话框和非模态对话框
模态对话框——用户需要等到处理完对话框后才能和其他窗口继续交流 非模态对话框——允许用户在处理对话框的同时与其他对话框进行交流,对话框是模态或非模态可以在创建Dialog对象时为构造方法传入参数而设置,也可以创建之后通过他的setModal()方法来进行设置,
需求:弹窗里点击按钮后弹出一个小弹窗
public class DialogDemo extends JFrame {public static void main(String[] args) {new DialogDemo();}// 主窗口public DialogDemo(){setVisible(true);setSize(500,400);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 关闭窗口Container container = getContentPane(); // JFrame放东西的容器container.setLayout(null); // 绝对布局JButton jbutton = new JButton("点击弹出一个小窗口");jbutton.setBounds(100,100,300,100);jbutton.addActionListener(new ActionListener() {// button上装一个监听器@Overridepublic void actionPerformed(ActionEvent e) {new MyDialogDemo();}});container.add(jbutton);}}class MyDialogDemo extends JDialog{// 弹窗弹出的小窗口public MyDialogDemo(){setVisible(true);setTitle("弹窗2");setBounds(100,100,300,200);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 关闭窗口Container container = getContentPane();container.setLayout(null);Label Label = new Label("这是一个新的弹窗");container.add(Label);}}
标签JLabel
作用:用于短文本字符串或图像或二者的显示区标签不对输入事件作出反应 —— 无法获得键盘焦点默认情况下,显示文本的标签是——开始边对齐;显示图像的标签——水平居中对齐。jlabel.setHorizontalAlignment(SwingConstants.CENTER);——标签居中
void setHorizontalTextPosition(int textPosition) ——设置标签的文本相对其图像的水平位置。
Icon:图标通常用于装饰组件 图标是一个接口 需要一个实现类
通过Icon接口来创建图标,可以在创建时 指定图标的大小、颜色等特性。继承这个借口时需要重写里面的三个方法
int getIconHeight() —— 返回图标的高度
int getIconWidth() —— 返回图标的宽度
vid paintIcon(Component c, Graphics g, int x, int y) ——在指定位置上绘制图标。
需求:设置一个图标标签
public class IconDemo extends JFrame implements Icon {private int width;private int height;public static void main(String[] args) {new IconDemo().init();}public IconDemo(){}public IconDemo(int width,int height){this.width = width;this.height = height;}public void init(){// 初始化函数IconDemo iconDemo = new IconDemo(15, 15); // 设置图标大小JLabel jLabel = new JLabel("圆圈", iconDemo, SwingConstants.CENTER); ——————这个标签就是一个名为圆圈的图标Container container = getContentPane();container.add(jLabel); ———————— 标签放在容器里container.setBackground(Color.blue);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {g.fillOval(x,y,width,height);// 画圆}@Overridepublic int getIconWidth() {return this.width;}@Overridepublic int getIconHeight() {return this.height;}}
ImageIcon:一个 Icon 接口的实现类 可以存放图片 返回URL
URL:某一资源的地址
icon是接口imageicon实现icon接口的类image是抽象类表示图形图像的所有类的超类构造方法:
ImageIcon(URL location) —— 根据指定的 URL 创建一个 ImageIcon
ImageIcon(String filename) —— 根据指定的文件名创建一个 ImageIcon
需求:设置一个带图片的图标标签
public class IconDemo2 extends JFrame {public IconDemo2() {setTitle("ZY的小图片");JLabel jLabel = new JLabel("蜡笔小小新");URL url = IconDemo2.class.getResource("timg (6).jpg"); ————————获取图片的地址ImageIcon imageIcon = new ImageIcon(url); ——————传入图标的地址 创建一个实现图标类的对象jLabel.setIcon(imageIcon); // 在标签上加上这个图标jLabel.setHorizontalAlignment(SwingConstants.CENTER); // 设置标签在容器中的位置Container container = getContentPane();container.add(jLabel);setBounds(200,200,500,500);setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 关闭窗口}public static void main(String[] args) {new IconDemo2();}}
面板JPanel
作用:JPanel 是一种中间层容器(轻量级容器) 是对窗体上的这些组件进行组合它能容纳组件并将组件组合在一起,但它本身必须添加到其他容器中使用它可以嵌套组合,在不同子容器中可包含其他组件 eg:JButton、JTextArea、JTextField 等GridLayout(int rows, int cols, int hgap, int vgap) ——创建指定行数和列数 指定间距的网格布局。
需求:在面板上添加网格布局的按钮
public class JPanelDemo extends JFrame {public JPanelDemo(){// Container container = getContentPane();// container.setLayout(new GridLayout(2,1,10,10)); // 把面板分成两行setLayout(new GridLayout(2,1,10,10));JPanel jPanel1 = new JPanel(new GridLayout(1, 2)); //一行两列JPanel jPanel2 = new JPanel(new GridLayout(2, 1)); // 两行一列JPanel jPanel3 = new JPanel(new GridLayout(2, 2)); // 两行两列jPanel1.add(new JButton("1-1"));jPanel1.add(new JButton("1-2"));jPanel2.add(new JButton("2-1"));jPanel2.add(new JButton("2-1"));jPanel3.add(new JButton("3-1"));jPanel3.add(new JButton("3-1"));jPanel3.add(new JButton("3-1"));jPanel3.add(new JButton("3-1"));Container container = getContentPane();container.setBackground(Color.black);container.add(jPanel1);container.add(jPanel2);container.add(jPanel3);setVisible(true);setBounds(200,200,500,500);}public static void main(String[] args) {new JPanelDemo();}}
JScrollPanel
定义:JScrollPanel 是带滚动条的面板,但只能放置一个组件,不能使用布局管理器。如果需要在 JScrollPanel 中放置多个组件,需要将多个组件放置在 JPanel 面板中,然后将 JPanel 作为一个整体添加到 JScrollPanel 中在窗体中创建一个带滚动条的文字编辑器
首先需要初始化编辑器 设定编译器的大小当创建带滚动条面板时,将编译器加入面板中将带滚动条的编译器放置在容器中即可
需求:设置一个带滚动条的文本面板 添加一个组件文本域
public class JScrollPanelDemo extends JFrame {public JScrollPanelDemo(){// 设置文本域JTextArea jTextArea = new JTextArea(20, 50);jTextArea.setText("欢迎来到ZY的JAVA小天地!");// 设置滚动条面板JScrollPane jScrollPane = new JScrollPane(jTextArea);// 将文本域添加到滚动条面板上Container container = getContentPane();container.add(jScrollPane);setVisible(true);setBounds(200,200,100,100);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollPanelDemo();}}
按钮JButton
不需要自己写鼠标监听器构造方法JButton(Icon icon) ——创建一个带图标的按钮。
JButton(String text) ——创建一个带文本的按钮。
JButton(String text, Icon icon) ——创建一个带初始文本和图标的按钮。
需求:创建一个带有图标的按钮
public class JButtonDemo extends JFrame{public JButtonDemo(){setTitle("ZY的图片按钮");URL url = JButtonDemo.class.getResource("timg (6).jpg"); // 路径System.out.println(url);Icon icon = new ImageIcon(url); // 将图片变成一个图标JButton jButton = new JButton();jButton.setIcon(icon); // 把图片加在按钮上Container container = getContentPane();container.add(jButton);setVisible(true);setBounds(100,100,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo();}}
ButtonGroup单选组
作用:用于为一组按钮创建一个多斥作用域。使用相同的 ButtonGroup 对象创建一组按钮意味着选择其中一个按钮时,将关闭组中的其他所有按钮void add(AbstractButton b) ——将按钮添加到组中
JRadioButton单选框按钮
作用:实现一个单选按钮,此按钮项可被选择或取消选择与 ButtonGroup 对象配合使用可创建一组按钮,一次只能选择其中的一个按钮注意:ButtonGroup 对象为逻辑分组,不是物理分组。要创建按钮面板,仍需要创建一个 JPanel 或类似的容器对象container并将 Border 添加到其中以便将面板与周围的组件分开需求:创建一个单选框按钮
public class JButtonDemo2 extends JFrame {public JButtonDemo2(){setTitle("ZY的单选框");URL url = JButtonDemo2.class.getResource("timg (5).jpg");ImageIcon imageIcon = new ImageIcon(url);// 创建单选框JRadioButton jRadioButton1 = new JRadioButton("选项A");JRadioButton jRadioButton2 = new JRadioButton("选项B");JRadioButton jRadioButton3 = new JRadioButton("选项C");// 单选框只能选择一个ButtonGroup buttonGroup = new ButtonGroup(); //分组——一个组中只能选择一个buttonGroup.add(jRadioButton1);buttonGroup.add(jRadioButton2);buttonGroup.add(jRadioButton3);Container container = getContentPane();container.add(jRadioButton1,BorderLayout.CENTER);container.add(jRadioButton2,BorderLayout.NORTH);container.add(jRadioButton3,BorderLayout.SOUTH);setVisible(true);setBounds(200,200,300,300);container.setBackground(Color.GREEN);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo2();}}
JCheckBox复选框
作用:是一个可以被选定和取消选定的项,它将其状态显示给用户 可以选定组中任意数量的复选框需求:创建一个复选框按钮 选择你的学历
public class JButtonDemo3 extends JFrame {public JButtonDemo3(){setTitle("ZY的复选框");JPanel jPanel1 = new JPanel();JPanel jPanel2 = new JPanel();// 设置标签JLabel jLabel = new JLabel("请选择你的学历");jLabel.setHorizontalAlignment(SwingConstants.CENTER);// 设置复选框按钮JCheckBox jCheckBox1 = new JCheckBox("本科");JCheckBox jCheckBox2 = new JCheckBox("博士");JCheckBox jCheckBox3 = new JCheckBox("硕士");jPanel1.add(jLabel);jPanel2.add(jCheckBox1, BorderLayout.NORTH);jPanel2.add(jCheckBox2,BorderLayout.CENTER);jPanel2.add(jCheckBox3,BorderLayout.SOUTH);Container container = getContentPane();container.setLayout(new GridLayout(2,1));————————设置两块面板的布局container.add(jPanel1);container.add(jPanel2);setVisible(true);setBounds(500,500,400,130);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo3();}}
列表JList
作用:显示对象列表并且允许用户选择一个或多个项的组件创建一个列表
public class ComboboxDemo2 extends JFrame {public ComboboxDemo2(){setTitle("ZY的列表");Vector vector = new Vector(); // 集合vector.add("清华大学");vector.add("北京大学");vector.add("麻省理工大学");JList jList = new JList(vector);// 创建一个列表 传入集合Container container = getContentPane();container.add(jList);setVisible(true);setBounds(200,200,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new ComboboxDemo2();}}
JComboBox下拉框
作用:将按钮或可编辑字段与下拉列表组合的组件。用户可以从下拉列表中选择值,下拉列表在用户请求时显示。如果使组合框处于可编辑状态,则组合框将包括用户可在其中键入值的可编辑字段需求:创建一个下拉框 选择你想看的电视剧
public class ComboboxDemo extends JFrame{public ComboboxDemo(){JPanel jPanel1 = new JPanel();JPanel jPanel2 = new JPanel();JLabel jLabel = new JLabel("请选择你想看的电视剧");JComboBox jComboBox = new JComboBox(); -------创建一个下拉框jComboBox.addItem(null);jComboBox.addItem("庆余年");jComboBox.addItem("沙海");jComboBox.addItem("德鲁纳酒店");jPanel1.add(jLabel);jPanel2.add(jComboBox);Container container = getContentPane();container.setLayout(new GridLayout(2,1));container.add(jPanel1);container.add(jPanel2);setVisible(true);setBounds(500,500,200,150);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new ComboboxDemo();}}
文本框TextField
作用:允许编辑单行文本的文本组件需求:创建一个文本框
public class TextDemo extends JFrame {public TextDemo(){TextField textField1 = new TextField("Hello");TextField textField2 = new TextField("HelloWorld",20);—————————— 20代表可输入的字符长度Container container = getContentPane();container.add(textField1,BorderLayout.NORTH);container.add(textField2,BorderLayout.SOUTH);setVisible(true);setBounds(200,200,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TextDemo();}}
密码框JPasswordField
作用:一个轻量级组件,允许编辑单行文本,其视图指示键入内容,但不显示原始字符需求:创建一个密码框 用*代替输入的文本
public class TextDemo2 extends JFrame {public TextDemo2(){JPasswordField jPasswordField = new JPasswordField();// 创建一个密码框jPasswordField.setEchoChar('*'); //输入的文本用*代替Container container = getContentPane();container.add(jPasswordField);setVisible(true);setBounds(200,200,300,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new TextDemo2();}}
文本域
作用:需求:创建一个文本域
public class JScrollDemo extends JFrame {public JScrollDemo(){JTextArea jTextArea = new JTextArea(20, 50);jTextArea.setText("欢迎来到ZY的小天地");JScrollPane jScrollPane = new JScrollPane(jTextArea);Container container = getContentPane();container.add(jScrollPane);setVisible(true);setBounds(200,200,500,500);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JScrollDemo();}}
JAVA入门————GUI图形界面管理器————Swing---窗口 弹窗 标签 面板 按钮(单选框复选框) 列表(下拉框) 文本框