- swing组件
按钮类:
JButton
jToggButton
JCheckBox
JRadioButton
JButton普通按钮,可以设置文字和图标Icon
JButton()JButton(Icon)JButton(stirng)JButton(icon,string)
JButton b = new JButton(“this is button”, new ImageIcon(“icon.gif”));
System.exit(0);
import javax.swing.JButton;
import javax.swing.JCheckBox;
class a {
public static void main(String[] args) {
System.out.println("系统退出");
System.exit(0);
System.out.println("这句话看不到");
}
}
windowclosing方法,窗口关闭的方法,可以重写
public void windowClosing(WindowEvent e)
{/这里添加重写代码/}
import java.awt.event.*;
import java.awt.*;
import java.security.Key;
import java.time.chrono.ThaiBuddhistChronology;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SpringLayout.Constraints;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
class a extends WindowAdapter implements ActionListener {
JButton tf;
JButton b;
public static void main(String[] args) {
a test1 = new a();
test1.anotherfunction();
}
public void anotherfunction() {
b = new JButton("this is button");
// 设置快捷键(貌似没成功)
b.setMnemonic(KeyEvent.VK_S);
// 设置一个命令
b.setActionCommand("Sample");
// 添加监听程序
b.addActionListener(this);
tf = new JButton();
tf.setText("this is text");
JFrame jf = new JFrame("test");
jf.getContentPane().add(b, "South");
jf.getContentPane().add(tf, "North");
jf.addWindowListener(this);
jf.setSize(100, 100);
jf.setVisible(true);
// 设置不可用
tf.setEnabled(false);
// 设置jf的命令也叫sample
tf.setActionCommand("Sample");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Sample") {
// 设置tf上的字符串
tf.setText("successful");
b.setText("successful");
}
}
}
选择框
// 设置不可用
tf.setEnabled(false);
// 设置tf上的字符串
tf.setText(“successful”);
// 设置jf触发事件的命令叫sample
tf.setActionCommand(“Sample”);
// 设置快捷键(貌似没成功)
b.setMnemonic(KeyEvent.VK_S);
复选框 JToggButton
他有两个子类JCheckbox,JRadioButton(一个圆选框一个方选框)
构造方法都一样
isSelected()方法,被选中返回true
有三个可选参数,string(描述文字),boolean(是否选中),icon(图标)
标签JLabel
JLabel()参数可以选,Icon,string
int 对齐方式:JLabel.LEFT,JLabel.RIGHT,JLabel.CENTER
int JLable.BOTTOM,JLable.TOP,JLabel.CENTER
也可通过setHorizontalAlignment(int)和SetVerticalAlignment(int)设置垂直水平位置
对于字符串标签
setHorizontalTextPostion(int),setverticalTextPosisition()
setText(),setIcon(),改变图标或者文字
void setFont(Font font)设置文字类型
后续都是组件的操作和使用了将简单快速掠过
组合框JComboBox
就是下拉框嘛,和MFC一样
构造函数可以不加入任何值,也可以加入一个string数组
addItem(object)//读文字都知道了
insertItemAt(object,int)
removeAllItem()
removeItem(object) //删除指定选项
removeItemAt(int)
列表JList
可选参数:数组,容器vector,ListModel(也是一种列表),空
listModel的方法
构造:DefaultListModel listModel=new DefaultListModel();
addElement(“TEXT”);//添加列表项
JList方法
setModel(listModel)//更换列表内容
JScorollPane设置滚动功能
setSelectionMode(int)//设置列表的模式
int参数
ListSetSelectionModel.SINGLE_SELECTION //列表框内容可以单选
ListSetSelectionModel.SINGLE_INTERVAL_SELECTION// 多选
ListSetSelectionModel.MULTIPLE_INTERVAL_SELECTION// 多选,且可以有间隔
选择列表将触发ListSelectionEvent事件
addListSelectionListener(ListSelectionListener) //监听方法