文章目录
GUI图形用户接口
一、Swing概述
GUI就是用来提供给用户操作的图形界面接口,通过对各种图形界面元素操作,实现程序的图形界面。Java中针对GUI设计提供了基本的图形用户接口开发工具,如AWT、Swing、JavaFX。
Swing是一种轻量级的组件,以AWT为基础进行了补充与改进,可以说两者是创新与依赖并存。

由图可知,Swing组件的所有类都继承自Container类,接着根据GUI开发功能扩展了两个分支:容器分支(绿)和组件分支(红)。
返回顶部
二、Swing顶层容器
在Swing中提供了三个主要的顶层容器类:JFrame、JDialog、JApplet,其中JFrame、JDialog是最常用的,JFram是独立存在的顶级容器(也叫窗口),不能放置在其他窗体之中。
初学的时候主要有四步:
- 1.创建顶层容器
- 2.设置默认窗口关闭方式
- 3.设置窗口大小
- 4.设置窗口为可显示(必须写!!!)
2.1 JFrame容器
package Test;
import javax.swing.*;
public class Test01 {
private static void createGui(){
// 1.创建顶层容器
JFrame frame = new JFrame("这是一个顶层容器");、
// 2.设置默认窗口关闭方式
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 3.设置窗口大小
frame.setSize(550,550);
// 4.chu设置窗口为可显示
frame.setVisible(true);
}
public static void main(String[] args) {
// 使用SwingUtilities类的invokeLater方法执行GUI程序,显示GUI程序
SwingUtilities.invokeLater(Test01::createGui);
}
}

返回顶部
2.2 JDialog容器
Dialog是一个具有标题和边框的顶级窗口,用来表示对话框,通常用于从用户处采取某种形式的输入。对话框的大小包括为边框指定的任何区域。 可以使用getInsets方法获得边界区域的getInsets,但是由于这些尺寸与平台相关,所以在通过调用pack或show可显示对话框之前,无法获得有效的插入值。
由于边框区域被包含在对话框的整体大小中,边框有效地掩盖了对话框的一部分,限制了可用于将副组件呈现和/或显示到左上角位置为(insets.left,) ,以及具有width - (insets.left + insets.right)的height -( + insets.bottom) 。
JDialog对话框可分为两种:
- 模态对话框—必须在处理完对话框后才能够继续与其他窗口交互
- 非模态对话框— 可以在处理对话框的同时与其他窗口进行交互
package Test;
import javax.swing.*;
public class JDialogTest {
public static void Test01() {
// 创建JFrame窗体
JFrame frame = new JFrame("JFrame窗体");
frame.setVisible(true);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 创建JDialog窗体
JDialog jDialog = new JDialog(frame,"对话框",true);
jDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
jDialog.setSize(250,250);
jDialog.setLocation(100,100);
jDialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JDialogTest::Test01);
}
}

JDialog jDialog = new JDialog(frame,"对话框",true);
这句代码是创建了JDialog对象,并调用了他的构造方法,指定该对话框的所有者为frame,命名为对话框,选定为模态对话框。
以下是JDialog的内定构造方法:

注意:虽然JFrame和JDialog都可以创建顶层容器,但是JDialog创建的窗口右上角没有缩放功能键。
返回顶部
三、布局管理器(setLayout())
Swing组件不能够单独存在,必须放置在相应的容器中才可以使用,而组建在容器中的位置则由布局管理器决定。Swing中提供了大致有8种布局管理器。
3.1 BorderLayout
构造方法 | 说明 |
BorderLayout() | 构造一个组件之间没有间距(默认间距为0像素)的新边框布局 |
BorderLayout(int hgap, int vgap) | 构造一个具有指定组件(hgap为横向间距,vgap为纵向间距)间距的边框布局 |
在向BorderLayout布局管理器中添加组件的时候,要使用add(Component comp,Object constraints),comp表示要添加的组件,constraints指定将组件加入到布局中的位置,内置有五个参数PAGE_START、PAGE_END、LINE_START、LINE_END、CENTER. (还可以使用方位参数—东西南北来指定)
package Test;
import javax.swing.*;
import java.awt.*;
public class BorderLayOutTest {
private static void Test03() {
JFrame frame = new JFrame("布局"); //创建顶级容器
frame.setLayout(new BorderLayout()); //设置容器布局管理样式
frame.setLocation(100,100); //设置窗体显示位置
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //设置窗体关闭时的默认操作
frame.setSize(1000,800); //设置窗体大小
frame.setVisible(true); //设置窗体显示
frame.add(new JButton("PAGE_START"),BorderLayout.PAGE_START); //设置并添加按钮组件,同时设置按钮所在区域
frame.add(new JButton("PAGE_END"),BorderLayout.PAGE_END);
frame.add(new JButton("LINE_START"),BorderLayout.LINE_START);
frame.add(new JButton("CENTER"),BorderLayout.CENTER);
frame.add(new JButton("LINE_END"),BorderLayout.LINE_END);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(BorderLayOutTest::Test03);
}
}

注意
// 方法一
frame.add(new JButton("PAGE_END"),BorderLayout.PAGE_END);
// 方法二
JButton jButton = new JButton();
frame.add(jButton,BorderLayout.PAGE_END)
1.方法一是直接在add时传入创建的button组件对象,以这种方法不可以实现对button组件的复用,
方法二在创建button组件对象的时候,同时创建了引用指向该button组件,在后期可以直接使用该引用调用按钮组件。
2.BorderLayout布局的优点是可以限定各个区域的边界,大小根据窗体的大小相对改变。当添加组件的时候,如果不指定添加位置的参数,将会默认添加到CENTER位置,当重复添加的时候,会像叠罗汉一样重叠,后加的覆盖掉之前的。
返回顶部
3.2FlowLayout
FlowLayout布局下的组件添加,将会按照添加顺序,从左往右依次添加。当到达窗口边界的时候就会换一行继续添加。这些组件可以按照左对齐、居中、右对齐的方式进行排列,如下图所示(这里有点像web)。

package Test;
import javax.swing.*;
import java.awt.*;
public class FlowLayOutTest {
private static void Test04(){
JFrame frame = new JFrame("FLOW");
frame.setLayout(new FlowLayout(FlowLayout.RIGHT,20,20));
frame.setSize(300,300);
frame.setLocation(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new JButton("1"));
frame.add(new JButton("2"));
frame.add(new JButton("3"));
frame.add(new JButton("4"));
frame.add(new JButton("5"));
frame.add(new JButton("1"));
frame.add(new JButton("2"));
frame.add(new JButton("3"));
frame.add(new JButton("4"));
frame.add(new JButton("5"));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(FlowLayOutTest::Test04);
}
}
对齐方式:
java frame.setLayout(new FlowLayout(FlowLayout.RIGHT,20,20));

java frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));

java frame.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));

返回顶部
3.3 GridLayOut
GridLayOut网格布局管理器将容器分为n行m列大小相等的网格,每个网格中可以添加一个组件。该布局的特点是组件的相对位置不随窗口的大小改变,但是组件的大小会随之改变。
常用的构造方法:

package Test;
import javax.swing.*;
import java.awt.*;
public class GridLayOutTest {
private static void Test04(){
JFrame frame = new JFrame("GridLayOutTest");
frame.setLayout(new GridLayout(3,3)); //设定网格布局管理,窗体为(3*3)
frame.setSize(600,600);
frame.setLocation(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//遍历创建、添加按钮组件
for (int i=0;i<9;i++){
JButton jButton = new JButton("按钮"+(i+1));
frame.add(jButton);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(GridLayOutTest::Test04);
}
}

注意:添加的组件方式与FlaotLayout相似,当添加的组件超过预定的数目时,会自动扩充,没有的地方不显示。假设将上述代码中的循环创建按钮组件遍历提高至10,结果如下图所示:

返回顶部
四、事件处理
4.1 事件处理机制
Swing组件中的事件处理就是用于相应客户的请求操作,就比如说客户端用户点击按钮发出了一个请求,利用Swing事件处理机制对该请求进行具体处理,比如点击登录按钮实现登陆成功显示等等。
事件处理流程

Swing事件的三大主要对象:
- 事件源— Event Source
- 事件对象— Event
- 监听器— Listener
基本处理步骤:
- 1.创建事件源,一般的组件都可以作为事件源,同时顶层窗口也可以作为事件源进行处理。
- 2.自定义事件监听器,根据事件源创建的指定类型的监听器进行事件处理。监听器是特殊的类,实现了XxxListener接口。
- 3、为事件源注册监听器,使用addXxxListener(为指定事件源添加特定类型的监听器),当事件源发生监听事件后,就会触发绑定的监听器,由监听其中的方法进行响应处理。
package Test;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionListenerTest {
private static void Test05(){
JFrame jFrame = new JFrame();
jFrame.setSize(500,500);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JButton but = new JButton("点击"); //1.创建按钮组件作为事件源
but.addActionListener(new click()); //3.为事件源注册自定义监听器绑定
jFrame.add(but);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ActionListenerTest::Test05);
}
}
//2.自定义事件监听类
class click implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) { //4.实现监听器方法,对监听事件进行处理
System.out.println("点击按钮");
}
}

返回顶部
4.2 Swing常用事件处理
在Swing中常用的事件主要有:窗体事件(WindowEvent)、鼠标事件(MouseEvent)、键盘事件(KeyEvent)、动作事件(ActionEvent)。
4.2.1 窗体事件
WindowEvent作为最外层的容器,是所有GUI应用程序的基础,应用程序中通常都是将其他组件直接或间接的加到窗体中。Java中提供了一个WindowEvent类用于表示窗体事件。在应用程序的时候当窗体事件进行处理的时候,首先需要定义一个实现WindowListener的接口类作为事件的监听器,一般是通过内部类来进行实现。然后,通过addWindowListener方法l实现窗体监听器的绑定。以下代码就是窗体事件的主要几种不同方法。
package Test;
import javax.swing.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowsEventTest {
private static void Windows(){
JFrame frame = new JFrame("窗口");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗体打开事件");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗体正在关闭事件");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("窗体关闭事件");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("窗体图表化事件");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("窗体取消图表化事件");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗体激活事件");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("窗体停用事件");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(WindowsEventTest::Windows);
}
}

返回顶部
4.2.2 鼠标事件
同样的,java提供了一个MouseEvent类用于表示鼠标事件。基本使用方法步骤与窗体事件一。
package Test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MouseEventTest {
private static void Mouse() {
JFrame frame = new JFrame("鼠标事件");
frame.setLayout(new FlowLayout());
frame.setSize(300, 200);
frame.setLocation(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("点击");
frame.add(button);
button.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
System.out.println("左击");
}
if (e.getButton() == MouseEvent.BUTTON3) {
System.out.println("右击");
}
if (e.getButton()==MouseEvent.BUTTON2){
System.out.println("中击");
}
}
public void mousePressed(MouseEvent e) {
System.out.println("长按");
}
public void mouseReleased(MouseEvent e) {
System.out.println("松开");
}
public void mouseEntered(MouseEvent e) {
System.out.println("进入");
}
public void mouseExited(MouseEvent e) {
System.out.println("出去");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(MouseEventTest::Mouse);
}
}

补充:
在实际操作中,我们需要通过判定鼠标具体的点击方式来判定,比如:右击、左击、滑动滑轮。GUI中同样可以实现:
// 从左往右
e.getButton()==MouseEvent.BUTTON1 //左击
e.getButton()==MouseEvent.BUTTON2 //滑动滑轮
e.getButton()==MouseEvent.BUTTON3 //右击
e是方法参数,代表当前事件对象(引用)。通过对象得到点击事件发生时触发的按钮,实际得到的是对应的鼠标事件常量,之后再进行比较。
返回顶部
4.2.3 键盘事件
package Test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class keyEventTest {
private static void key() {
JFrame frame = new JFrame("key");
frame.setLayout(new FlowLayout());
frame.setLocation(300, 200);
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField jt = new JTextField( "B",15);
// JTextField jt2 = new JTextField("A",15);
// jt2.add(jt);
// jt.setEditable(false);
frame.add(jt);
jt.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
char a = e.getKeyChar();
int keycode = e.getKeyCode();
System.out.println("键盘输入" + a);
System.out.println("字符代码" + keycode);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(keyEventTest::key);
}
}

注意:
在鼠标事件和键盘事件中,除了通过实现XxxListener接口来实现定义监听器,也可以通过与其对应的XxxAdapter类来实现,最后都通addXxxListener()方法将监听器绑定到事件源对象进一步操作。
返回顶部
五、Swing常用组件
5.1 JPanel
JPanel面板组件是一个无边框,不能被移动、方爱的、缩小或者关闭的的面板,他的默认布局管理器是FlowLayout,当然后续也可以对其进行更改。介于JPanel面板组建的特殊性,通常把它作为中间容器,也就是将其他面板组件先加入到JPanel中,再将其加入到顶层容器中。
5.2 JScrollPane
JScrollPane是一个带有滚动条的面板容器,而且这个面板只能够添加一个组件。如要添加多个组件我们就可以借助JPanel面板组件实现。

设置面板滚动条策略的基本方法:
方法 | 说明 |
setHorizontalScrollBarPolicy(int policy) | 确定水平滚动条何时出现在滚动条中。 |
setVerticalScrollBarPolicy(int policy) | 确定滚动条中何时出现垂直滚动条。 |
setViewportView(Component view) | 如果需要,创建一个视口,然后设置它的视图。 |
设置面板滚动条策略的基本常量属性:


package Test;
import javax.swing.*;
import java.awt.*;
public class JScrollPaneTest {
private static void JScrollPane(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,300);
frame.setLocation(200,300);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JPanel panel = new JPanel();
panel.add(new JButton("按钮1"));
panel.add(new JButton("按钮2"));
panel.add(new JButton("按钮3"));
panel.add(new JButton("按钮4"));
panel.add(new JButton("按钮5"));
panel.add(new JButton("按钮6"));
panel.add(new JButton("按钮7"));
panel.add(new JButton("按钮8"));
scrollPane.setViewportView(panel);
frame.add(scrollPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JScrollPaneTest::JScrollPane);
}
}

返回顶部
5.3 JTextArea、JTextField
JTextField 成为文本框,它只能够接受单行文本的输入。

JTextField有一个子类JPasswordField,他表示一个密码框,不显示用户输入的信息,默认显示为"*",用法与JTextField相似。
JTextArea是文本域,他可以接受多行文本的输入,使用其构造方法的时候可以传入行列。

package MenuTest;
import javax.swing.*;
import java.awt.*;
public class Talking {
public static void createAndShowGUI(){
//创建一个聊天JFrame窗口
JFrame frame = new JFrame("聊天窗口");
frame.setLayout(new BorderLayout());
frame.setSize(400,300);
frame.setLocation(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
//创建文本区,用来显示聊天
JTextArea textArea = new JTextArea(12,34);
//创建滚动面板组件
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
//创建单行输入框,用来聊天
JTextField inputField = new JTextField(20);
JButton btn = new JButton("SEND");
//为按钮添加监听事件
btn.addActionListener(e -> {
String content = inputField.getText();
//判断输入的信息是否为空
if(content!=null && !content.trim().equals("")){
//如果不为空将聊天信息追加到聊天框
textArea.append("本人输入信息"+content+"\n");
}else{
//如果为空则提示聊天信息不能为空
textArea.append("聊天信息不能为空!"+"\n");
}
inputField.setText(""); //设置输入的文本框内容置空
});
//创建一个JPanel面板组件
JPanel jPanel = new JPanel();
//创建一个聊天标签
JLabel jLabel = new JLabel("聊天信息");
//将标签\输入\按钮组件加入到面板组件中
jPanel.add(jLabel);
jPanel.add(inputField);
jPanel.add(btn);
//向聊天窗口的顶部和尾部添加到JScrollPane和JPanel组件中
frame.add(scrollPane,BorderLayout.PAGE_START);
frame.add(jPanel,BorderLayout.PAGE_END);
}
public static void main(String[] args) {
//使用工具类调用方法显示GUI程序
SwingUtilities.invokeLater(Talking::createAndShowGUI);
}
}

返回顶部
5.4 JLabel、ImageIcon
JLable标签组件常用于仅供展示内容,可以显示文本、图像。

ImageIcon组件主要用于设置面板中的图片信息。

package Test;
import javax.swing.*;
import java.awt.*;
public class ImgTest {
private static void ImgTest(){
JFrame frame = new JFrame("图片");
frame.setSize(400,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(300,200);
frame.setVisible(true);
JLabel label1 = new JLabel();
ImageIcon icon = new ImageIcon("捕获3.png");
Image image =icon.getImage();
icon.setImage(image);
label1.setIcon(icon);
frame.add(label1);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ImgTest::ImgTest);
}
}

返回顶部
5.5JRadioButton、JCheckBox
JRadioButton、JCheckBox组件主要的区别就是选项框的形式不同,用法基本与其他组件一致。JRadioButton被称为单选按钮组件,按照道理就想收音机电台按钮一样,按下一个时,另一个就会自动弹起,但是JRadioButton组件本身不具备这种功能,需要使用ButtonGroup类来实现。ButtonGroup组件是一个不可见的组件,不需要将其添加到容器中显示,只是逻辑上表示一个单选按钮,将多个JRadioButton添加到同一个ButtonGroup组件中就可以实现单选功能。

JCheckBox组件被称为复选框组件,具有选中与未选中两种状态,通常复选框会有多个,就类似于我们所具有的兴趣爱好一样。

下面以单选框为案例,复选框只要修改为JCheckBox即可~
package Test;
import javax.swing.*
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RadionButtonTest {
private static void ButtonTest() {
//添加顶层容器
JFrame frame = new JFrame("单选框");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setLocation(300, 400);
//添加标签组件
JLabel label = new JLabel("HELLO WORLD", JLabel.CENTER);
label.setFont(new Font("宋体", Font.PLAIN, 30));
//添加中间容器
JPanel panel = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup(); // 添加 ButtonGroup组件
JRadioButton radioButton1 = new JRadioButton("1"); // 将多个JRadioButton组件添加到ButtonGroup中,实现单选效果
JRadioButton radioButton2 = new JRadioButton("2");
//组合
panel.add(radioButton1);
panel.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::ButtonTest);
}
}


返回顶部
5.6JPopupMenu、JMenu
以记事本为例来介绍菜单栏、弹出菜单栏:


package Test;
import com.sun.jmx.mbeanserver.JmxMBeanServer;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PopupMenuTest {
private static void PopupTest(){
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::PopupTest);
}
}

返回顶部
5.7 JComboBox
JComboBox被称为下拉框或组合框,她将所有的选项折叠在一起,默认显示的是第一个选项。
JComboBox可以分为可编辑与不可编辑两种。不可编辑就是通常在一些注册面中遇到的自定义模式。

常用方法:
void addItem(E item) ----------------- 将项目添加到项目列表。
E getItemAt(int index) ---------------- 返回指定索引处的列表项。
int getItemCount() --------------------- 返回列表中的项目数。
int getSelectedIndex() -----------------返回列表中与给定项目匹配的第一个项目。
Object getSelectedItem() -------------返回当前所选项目。
Object[] getSelectedObjects() -------返回一个包含所选项目的数组。
void insertItemAt(E item, int index) - 在给定索引的项目列表中插入项目。
boolean isEditable() --------------------如果JComboBox是可编辑的,则返回true。
boolean isPopupVisible() -------------确定弹出窗口的可见性。
void removeAllItems() ------------------从项目列表中删除所有项目。
void removeItem(Object anObject) --从项目列表中删除一个项目。
void removeItemAt(int anIndex) anIndex 删除该项目此方法仅在 JComboBox使用可变数据模型时有效。
package Test;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
public class ComboBoxTest {
private static void ComTest(){
JFrame frame = new JFrame("下拉");
frame.setSize(400,500);
frame.setLocation(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JComboBox<String>comboBox = new JComboBox<String>();
comboBox.addItem("选择城市");
comboBox.addItem("北京");
comboBox.addItem("上海");
comboBox.addItem("广州");
comboBox.addItem("深圳");
JTextField textField = new JTextField(20);
comboBox.addActionListener(e -> {
String city = (String) comboBox.getSelectedItem();
if ("选择城市".equals(city)){
textField.setText("");
}else {
textField.setText("你选择:"+city);
}
});
panel.add(comboBox);
panel.add(textField);
frame.add(panel, BorderLayout.PAGE_START);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ComboBoxTest::ComTest);
}
}

返回顶部