0
点赞
收藏
分享

微信扫一扫

【Java Swing】Java组件及事件处理


图形用户接口

  • ​​1、Swing概述​​
  • ​​2、Swing顶级容器​​
  • ​​3、布局管理器​​
  • ​​4、事件处理​​
  • ​​5、Swing常用组件​​

1、Swing概述

  • Swing是一种轻量级的组件,它由Java语言开发,可以通过使用简洁的代码、灵活的功能和模块化的组件来创建优雅的用户界面
  • Swing组建的继承关系

2、Swing顶级容器

JFrame

  • JFrame是Swing最常见的一个组件,它是一个独立存在的顶级容器(也叫窗口),所以它不能放在其他的容器之中

import javax.swing.*;

public class JFrameTest {
public static void showJrame() {
//创建并且设置顶级窗口
JFrame frame = new JFrame("JFrameTest");
//设置关闭窗口的默认操作
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口的大小
frame.setSize(500,400);
//展示窗口
frame.setVisible(true);
}

public static void main(String[] args) {
//调用窗口程序
SwingUtilities.invokeLater(JFrameTest::showJrame);
}
}

JDialog

  • JDialog是Swing的另外一个顶级容器,通常用来表示对话框窗口,并且JDialog对话框可分为两种:模态对话框和非模态对话框,对话框是模态或者非模态,可以在创建JDialog对象时为构造方法传入参数来设置,也可以在创建JDialog对象后调用它的setModal()方法来进行设置。
  • 常见的构造方法

【Java Swing】Java组件及事件处理_开发语言

import javax.swing.*;

public class JDialogTest {
public static void showJDialog(){
//创建一个JFrame串口
JFrame frame = new JFrame("JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(500,400);
//设置JFrame居中对齐
frame.setLocationRelativeTo(null);
//创建一个属于frame的模态的JDialog窗口
JDialog dialog = new JDialog(frame,true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setSize(300,200);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JDialogTest::showJDialog);
}
}

3、布局管理器

BorderLayout

  • BorderLayout(边界布局管理器)是一种较为复杂的布局方式,它将容器划分为五个区域,分别是页头(PAGE_START)、页尾(PAGE_END)、行首(LINE_START)、行尾(LINE_END)、中部(CENTER)。组件可以被放置在这五个区域中的任意一个位置。

import javax.swing.*;
import java.awt.*;

public class BorderLayoutTest {
public static void showBorderLayout() {
//创建一个名为BorderLayout的顶级窗口
JFrame frame = new JFrame("BorderLayout");
//设置其布局管理器为BorderLayout
frame.setLayout(new BorderLayout());
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//创建按钮组件放到BorderLayout的五个地方上
frame.add(new JButton("按钮1"), BorderLayout.PAGE_START);
frame.add(new JButton("按钮2"), BorderLayout.PAGE_END);
frame.add(new JButton("按钮3"), BorderLayout.LINE_START);
frame.add(new JButton("按钮4"), BorderLayout.CENTER);
frame.add(new JButton("按钮5"), BorderLayout.LINE_END);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(BorderLayoutTest::showBorderLayout);
}
}

  • 效果图

FlowLayout(流式布局管理器)

  • 是最简单的布局管理器,在这种布局下,容器会将组件按照添加顺序从左向右放置,当到达容器的边界时,会自动将组件放到下一行的开始位置。这些组件可以按左对齐、居中对齐(默认方式)或右对齐的方式排列。
  • FlowLayout的构造方法

import javax.swing.*;
import java.awt.*;

public class FlowLayoutTest {
public static void showFlowLayout(){
//定义一个名为FlowLayout的顶级窗口
JFrame frame = new JFrame("FlowLayout");
//设置布局管理器为FlowLayout,且对齐方式为左对齐,水平间距为20,垂直间距为30
frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,30));
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
//向容器添加组件
for (int i = 0;i<5;i++){
frame.add(new JButton("添加第"+(i+1)+"个按钮"));
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(FlowLayoutTest::showFlowLayout);
}
}

  • 效果图

GridLayout

  • GridLayout(网格布局管理器)使用纵横线将容器分成n行m列大小相等的网格,每个网格中可以添加一个组件。
    添加到容器中的组件会从左向右、从上向下依次填充到网格中。
    与FlowLayout不同的是,放置在GridLayout布局管理器中的组件将自动占据网格的整个区域
  • 常见的构造方法

import javax.swing.*;
import java.awt.*;

public class GridLayoutTest {
public static void showGridLayout(){
//创建一个名为GridLayout的·1顶级窗口
JFrame frame = new JFrame("GridLayout");
//设置布局管理器为三行三列的GridLayout
frame.setLayout(new GridLayout(3,3));
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
for (int i = 0;i<9;i++){
frame.add(new JButton("添加第"+(i+1)+"个按钮"));
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(GridLayoutTest::showGridLayout);
}
}

  • 效果图

4、事件处理

窗体事件

import javax.swing.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class windowEventTest {
public static void showWindowEventTest(){
JFrame frame = new JFrame("Window");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//使用内部类实例化监听器
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("window opened--->窗体打开");
}

@Override
public void windowClosing(WindowEvent e) {
System.out.println("window closing--->窗体正在关闭");
}

@Override
public void windowClosed(WindowEvent e) {
System.out.println("window closed--->窗体关闭");

}

@Override
public void windowIconified(WindowEvent e) {
System.out.println("window Iconified--->窗体图标化事件");
}

@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("window Deiconified--->窗体取消图标化");
}

@Override
public void windowActivated(WindowEvent e) {
System.out.println("window Activated--->窗体激活");
}

@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("window DeActivated--->窗体停用");
}
});
}

public static void main(String[] args) {
SwingUtilities.invokeLater(windowEventTest::showWindowEventTest);
}
}

  • 效果图

鼠标事件

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseEventTest {
public static void showMouseEventTest(){
JFrame frame = new JFrame("MouseEvent");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JButton button = new JButton("按钮");
frame.add(button, BorderLayout.CENTER);
button.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton()==MouseEvent.BUTTON1){
System.out.println("鼠标左击事件");
}
if (e.getButton()==MouseEvent.BUTTON2){
System.out.println("鼠标中击事件");
}
if (e.getButton()==MouseEvent.BUTTON3){
System.out.println("鼠标右击事件");
}
}

@Override
public void mousePressed(MouseEvent e) {
System.out.println("鼠标按下事件");
}

@Override
public void mouseReleased(MouseEvent e) {
System.out.println("鼠标放开事件");
}

@Override
public void mouseEntered(MouseEvent e) {
System.out.println("鼠标进入事件");
}

@Override
public void mouseExited(MouseEvent e) {
System.out.println("鼠标离开事件");
}
});
}

public static void main(String[] args) {
SwingUtilities.invokeLater(MouseEventTest::showMouseEventTest);
}
}

键盘事件

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class keyEventTest {
public static void showKeyEvent(){
JFrame frame = new JFrame("keyEvent");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//添加单行文本框
JTextField textField = new JTextField(30);
frame.add(textField);
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获取对应的键盘字符
char keychar = e.getKeyChar();
//获取对象的键盘字符代码
int keyCode = e.getKeyCode();
System.out.println("键盘按下的字符为:"+keychar);
System.out.println("键盘按下的字符的字符代码为:"+keyCode);
}
});
}

public static void main(String[] args) {
SwingUtilities.invokeLater(keyEventTest::showKeyEvent);
}
}

  • 效果图

5、Swing常用组件

面板组件

  • JPanel:JPanel面板组件是一个无边框,不能被移动、放大、缩小或者关闭的面板,它的默认布局管理器是FlowLayout。
  • JScrollPane
  • JScrollPane是一个带有滚动条的面板容器,且只能添加一个组件;
    想向JScrollPane面板中添加多个组件,应先将这多个组件添加到某个组件中,然后再将这个组件添加到JScrollPane中
    \
  • 常用的构造方法
  • 【Java Swing】Java组件及事件处理_ide_02

  • 设置面板滚动策略的方法
  • 【Java Swing】Java组件及事件处理_ide_03

  • 面板滚动策略
  • 【Java Swing】Java组件及事件处理_java_04

import javax.swing.*;

public class ScrollPaneTest {
public static void showScrollPane(){
JFrame frame = new JFrame("ScrollPane");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//创建ScrollPane组件
JScrollPane scrollPane = new JScrollPane();
//设置水平方向在需要时显示
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//设置垂直方向上一直显示
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//定义一个JPanel组件
JPanel panel = new JPanel();
//在组件Panel中添加四个按钮
panel.add(new JButton("按钮1"));
panel.add(new JButton("按钮2"));
panel.add(new JButton("按钮3"));
panel.add(new JButton("按钮4"));
//将组件Panel加到ScrollPanel中
scrollPane.setViewportView(panel);
frame.add(scrollPane);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(ScrollPaneTest::showScrollPane);
}
}

  • 效果图

文本组件

  • JTextField:JTextField称为文本框,它只能接收单行文本的输入

import javax.swing.*;
import java.awt.*;

public class JTextFieldTest {
public static void showTextField(){
JFrame frame = new JFrame("TextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.setLocation(300,200);
JTextArea textArea = new JTextArea(12,34);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
JTextField textField = new JTextField(20);
JButton button = new JButton("发送");
button.addActionListener(d -> {
String context = textField.getText();
if (context != null && !context.trim().equals("")){
System.out.println("你输入:"+context+"\n");
}else{
System.out.println("不能输入空数据");
}
textArea.append("你输入:"+context+"\n");
textField.setText("");
});
JPanel panel = new JPanel();
JLabel label = new JLabel("聊天信息");
panel.add(label);
panel.add(textField);
panel.add(button);
frame.add(scrollPane, BorderLayout.PAGE_START);
frame.add(panel,BorderLayout.PAGE_END);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(JTextFieldTest::showTextField);
}
}

  • 效果图

JTextArea:JTextArea称为文本域,它能接收多行文本的输入,使用JTextArea构造方法创建对象时可以设定区域的行数、列数

【Java Swing】Java组件及事件处理_开发语言_05

标签组件

  • 标签组件主要用到的是JLabel,JLabel组件可以显示文本、图像,还可以设置标签内容的垂直和水平对齐方式
  • 构造方法

import javax.swing.*;
import java.awt.*;

public class IconTest {
public static void showIcon(){
JFrame frame = new JFrame("Icon");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//创建一个JLabel来存储图片
JLabel label = new JLabel();
//创建一个图片组件加到Panel中
ImageIcon imageIcon = new ImageIcon("tiger.jpg");
Image image = imageIcon.getImage();
imageIcon.setImage(image);
label.setIcon(imageIcon);
frame.add(label);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(IconTest::showIcon);
}
}

  • 效果图

按钮组件

  • JCheckBox:JCheckBox组件被称为复选框组件,它有选中和未选中两种状态,通常复选框会有多个,用户可以选中其中一个或者多个
  • CheckBox常用的构造方法

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CheckBoxTest {
public static void showCheckBox(){
JFrame frame = new JFrame("CheckBox");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JLabel label = new JLabel("HELLO WORLD",JLabel.CENTER);
label.setFont(new Font("宋体",Font.PLAIN,20));
JPanel panel = new JPanel();
JCheckBox checkBox1 = new JCheckBox("男");
JCheckBox checkBox2 = new JCheckBox("女");
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int border = 0;
if (checkBox1.isSelected())
border += Font.BOLD;
if (checkBox2.isSelected())
border+=Font.ITALIC;
label.setFont(new Font("宋体",border,20));
}
};
checkBox1.addActionListener(listener);
checkBox2.addActionListener(listener);
panel.add(checkBox1);
panel.add(checkBox2);
frame.add(label);
frame.add(panel,BorderLayout.PAGE_END);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(CheckBoxTest::showCheckBox);
}
}

  • 效果图

JRadionButton:JRadioButton组件被称为单选按钮组件,单选按钮只能选中一个

  • 常用的构造方法

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RadionButtonTest {
public static void showRadionButton(){
JFrame frame = new JFrame("RadionButton");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JLabel label = new JLabel("HELLO WORLD", JLabel.CENTER);
label.setFont(new Font("宋体", Font.PLAIN, 30));
JPanel panel = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton radioButton1 = new JRadioButton("1");
JRadioButton radioButton2 = new JRadioButton("2");
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int border = 0;
if (radioButton1.isSelected())
border+=Font.BOLD;
if (radioButton2.isSelected())
border+=Font.ITALIC;
label.setFont(new Font("宋体",border,30));
}
};
radioButton1.addActionListener(listener);
radioButton2.addActionListener(listener);
panel.add(radioButton1);
panel.add(radioButton2);
frame.add(panel,BorderLayout.PAGE_END);
frame.add(label);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(RadionButtonTest::showRadionButton);
}
}

  • 效果图

下拉框组件

  • 常用构造方法
  • 【Java Swing】Java组件及事件处理_java_06

  • 常用的方法
  • 【Java Swing】Java组件及事件处理_System_07

import javax.swing.*;

public class ComboBoxTest {
public static void showComboBox(){
JFrame frame = new JFrame("ComboBox");
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

JPanel panel = new JPanel();
JComboBox<String>comboBox = new JComboBox<>();
comboBox.addItem("请选择城市:");
comboBox.addItem("北京");
comboBox.addItem("上海");
comboBox.addItem("南京");
panel.add(comboBox);
frame.add(panel);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(ComboBoxTest::showComboBox);
}
}

  • 效果图

菜单组件

下拉式菜单

  • JMenu的常用方法

import javax.swing.*;

public class JMenuTest {
public static void showMenu(){
JFrame frame = new JFrame("JMenu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(500,300);
frame.setLocationRelativeTo(null);

JMenuBar menuBar = new JMenuBar();

JMenu menu1 = new JMenu("菜单");
JMenu menu2 = new JMenu("工具");

menuBar.add(menu1);
menuBar.add(menu2);

JMenuItem menuItem1 = new JMenuItem("新建");
JMenuItem menuItem2 = new JMenuItem("帮助");
menu1.add(menuItem1);
menu1.add(menuItem2);

JMenuItem menuItem3 = new JMenuItem("设置");
JMenuItem menuItem4 = new JMenuItem("退出");
menu2.add(menuItem3);
menu2.add(menuItem4);

frame.setJMenuBar(menuBar);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(JMenuTest::showMenu);
}
}

  • ** 效果图**

弹出式菜单

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PopupMenuTest {
public static void showPopupMenu(){
JFrame frame = new JFrame("弹出菜单");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setLocation(300,200);
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem item1 = new JMenuItem("查看");
JMenuItem item2 = new JMenuItem("刷新");
popupMenu.add(item1);
popupMenu.addSeparator();
popupMenu.add(item2);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton()==MouseEvent.BUTTON3){
popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}
});
}

public static void main(String[] args) {
SwingUtilities.invokeLater(PopupMenuTest::showPopupMenu);
}
}


举报

相关推荐

0 条评论