一:可滚动窗格JScrollPane Swing中的容器JScrollPane来支持滚动条,它可用来放置任何可滚动的组件 1.JScrollPane(Component):包含指定组件的可滚动窗格 2.JScrollPane(Component,int,int):包含指定组件,并带垂直滚动条和水平滚动条的可滚动窗格 要配置滚动条,可使用接口ScrollPaneConstants的6个静态类变量之一。 对于垂直滚动条:可以使用3个变量之一 VERTICAL_SCROLLBAR_ALWAYS VERTICAL_SCROLLBAR_AS_NEEDED; VERTICAL_SCROLLBAR_NEVER 水平滚动条也一样
package com.company;
import javax.swing.*;
public class Main extends JFrame{
public Main(){
JPanel panel=new JPanel();
JTextArea area=new JTextArea(4,15);//4行15列
JScrollPane scrollPane=new JScrollPane(area,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scrollPane);
add(panel);
setVisible(true);
}
//它带垂直滚动条,但没有水平滚动条
public static void main(String[] args) {
new Main();
}
}
二.复选框JCheckBox和单选JRadioButton按钮 选中或者没有选中 1.setSelected(boolean):如果参数为true,则选中 2.isSelected();返回一个布尔值,指出组件是否被选中
构造函数 JCheckBox(String):带指定文本标签的复选框 JCheckBox(String,boolean):带指定文本标签的复选框,如果第二个参数为true,则被选中 JCheckBox(Icon):带指定图标的复选框 JCheckBox(Icon,boolean):带指定文本标签的复选框,如果第二个参数为true,则被选中 JCheckBox(String,Icon):带指定文本标签和图标的复选框 JCheckBox(String,Icon,boolean):带指定文本标签和图标的复选框,如果第三个参数为true,则被选中
为了使单选按钮互斥,必须将相关的组件分组 要将多个单选按钮组织成一组,只允许每次选中其中一个,可以创建ButtonGrounp对象
ButtonGrounp choice=new ButtonGrounp();
ButtonGroup可以调用方法add(Component)将指定的组件加入到组中
ButtonGrounp choice=new ButtonGrounp();
JRadioButton s1=new JRadioButton("JSON",false);
choice.add(s1);
JRadioButton s2=new JRadioButton("XML",false);
choice.add(s2);
使s1和s2只能一个被选中
package com.company;
import javax.swing.*;
public class Main extends JFrame{
JRadioButton[] teams=new JRadioButton[4];
public Main(){
super("Choose one");
setSize(320,120);
teams[0]=new JRadioButton("数学");
teams[1]=new JRadioButton("英语");
teams[2]=new JRadioButton("c#");
teams[3]=new JRadioButton("unity",true);//选中这个
JPanel jPanel=new JPanel();
JLabel choice=new JLabel("choose one:");
jPanel.add(choice);
ButtonGroup group=new ButtonGroup();
for(JRadioButton team:teams){
group.add(team);
jPanel.add(team);
}
add(jPanel);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
三.组合框JComboBox[第一篇也粗略的提到】
方法一:addItem()
JComboBox comboBox=new JComboBox<>();
comboBox.addItem("identity card");
comboBox.addItem("student card");
comboBox.addItem("employee.card");
方法二:
调用JComboBox(object[]),并提供一个包含列表项的数组作为参数。如果列表项为文本,则提供的参数是一个String数组
String items[]={"identity card","student card","employee.card"};
JComboBox comboBox=new JComboBox<>(items);//使用String添加下拉列表元素
1.getItemAt(int):返回位于整数参数指定的索引位置的选项文本。与数组一样,选择列表第一项的索引为0,第二项为1---- 2.getItemCount():返回列表中的选项数目 3.getSelectedIndex():返回当前选项的索引 4.getSelectedItem():返回当前选项的文本 5.setSelectedIndex(int):选中索引指定的选项 6.setSelectedIndex(object):选中索引指定的对象
package com.company;
import javax.swing.*;
public class Main extends JFrame{
String[] subject={"math","english","java","unity"};
JComboBox comboBox=new JComboBox(subject);
public Main(){
super("Choose a subject");
setSize(220,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane =new JPanel();
JLabel label=new JLabel("result:");
pane.add(label);
pane.add(comboBox);
add(pane);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
四.列表JList 让用户能够选择一个或多个值 JList()创建一个空列表 JList(object[])创建一个列表,其内容为指定类的数组 JList(Vector):创建一个列表,其内容为java.util.Vector的对象