一、文本框
文本框是接受用户输入的种组件 ,Swing将文本组件分为三大类
(1)文本字段(text fields),包括类JTexrField和JPasswordField用于显示和编辑较短的,不带格式的一行文本。
(2)纯文本区域(plain text areas),包括类JTextArea,用于显示和编辑较长的、不带格式的多行文本,通常用于显示没有格式的帮助信息。纯文本区域的使用基于文档事件(即主要监听文档类型的事件DocumentEvent),程序通常使用文档事件监听器监听用户对纯文本区域的改变。
(3)带样式文本区域(styled text areas) ,包括类JEditorPane和JTextPane,可用于显示和编辑复杂的、带样式的文本。JEditorPane 类支持纯文本、HTML和RTF的文本编辑;JTextPane类进一步扩 展了JEditorPane 类的功能.允许文本中嵌人图像或其他组件。
JTexrField构造方法:
(1)JTextFieldO):无参数的构造方法,创建一 个初始为空、可显示字符列数为0的文本框对象。
(2)JTextField(String text): 创建-一个 初始内容为text的文本框对象。
(3)JTextField(String text,int col):创建一个初始内容为 text、 可显示字符列数为col的文本框对象。
(4)JTextField(int col):创建个 初始内容为空、可显示字符列数为 col的文本框对象。
JTexrField类中常见的成员方法:
(1)String getText():返回文本框中的文本信息。
(2)void setText(String text):将文本框中的内容设置为text。
(3)boolean isEditable():检查文本框是否可编辑。如果返回true, 表示该文本框可编辑。
(4)void setEditable( boolean editable):设置文本框的可编辑性。如果editable为true,表示该文本框设置为可编辑。
(5)int getColunmns():返回文本框所显示的字符列数。
(6)void setColumns(int col) :将文本框能够显示字符的列数设置为col。
public class TestTextDemo01 extends JFrame{
public TestTextDemo01() {
Container container =this.getContentPane();
JTextField textfiled = new JTextField("hello world");
JTextField textfiled2 = new JTextField("大数据五班");
container.add(textfiled,BorderLayout.NORTH);
container.add(textfiled2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
public class TestTextDemo02 extends JFrame{
public TestTextDemo02() {
Container container =this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
二、事件
事件分为低级事件和语义事件
低级事件:焦点事件,鼠标事件,键盘事件和窗口事件都属于低级事件他们的处理方式对于编写正确的程序至关重要。
事件名称 | 事件描述 |
---|---|
FocusEvent | 再组件获得焦点或失去焦点时产生的事件 |
MouseEvnt | 用户对鼠标操作所产生的事件 |
KeyEvnt | 用户对键盘操作所产生的事件 |
WindowEvnt | 用户对窗口操作所产生的事件 |
键盘事件KeyEvnt
public class TestKeyListener {
public static void main(String[] args) {
}
}
class KeyFrame extends JFrame{
public KeyFrame() {
this.setBounds(10,10,300,400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addKeyListener(
new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keycode=e.getKeyCode();
if(keycode==KeyEvent.VK_UP) {
}if(keycode==KeyEvent.VK_UP) {
}
}
});
}
}
窗口事件WindowEvnt
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends JFrame{
public WindowFrame() {
this.setBackground(Color.blue);
this.setBounds(100,100,500,300);
this.setVisible(true);
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.addWindowListener(new MyWindowListener());//匿名类
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
super.windowClosing(e);
setVisible(false);//设置不可见
System.out.println("我要关闭");
//System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
// TODO 自动生成的方法存根
super.windowActivated(e);
System.out.println("windowActivated");
}
@Override
public void windowClosed(WindowEvent e) {
// TODO 自动生成的方法存根
super.windowClosed(e);
System.out.println("windowClosed");
}
@Override
public void windowOpened(WindowEvent e) {
// TODO 自动生成的方法存根
super.windowOpened(e);
System.out.println("windowOpened");
}
}
);
}
/*
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
// TODO 自动生成的方法存根
super.windowClosing(e);
setVisible(false);//设置不可见
System.out.println("我要关闭");
System.exit(0);
}
}*/
}
鼠标事件MouseEvnt
//鼠标监听
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame(“我的画图”);
}
}
class MyFrame extends JFrame{
ArrayList points;
public MyFrame(String title){
super(title);
this.setBounds(200,200,400,400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
points = new ArrayList<>();
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.cyan);
g.fillOval(point.x,point.y,10,10);
}
}
public void addPaint(Point point){
points.add(point);
}
private class MyMouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
System.out.println("x左标:"+e.getX()+"y左标"+e.getY());
myFrame.addPaint(new Point(e.getX(),e.getY()));
myFrame.repaint();
}
}
}
语义事件:语义事件是与组件有关事件
事件类名 | 事件描述 |
---|---|
ActionEvent | 激活组件事件 |
ItemEvent | 选项事件 |
AdjustmentEvent | 调节事件 |
ComponentEvent | 组件事件 |
ContainerEvent | 窗口事件 |
TextEvent | 文本框事件 |
public class TextActionEvent {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(400,400);
frame.locate(400, 300);
MyaddActionListener myaddActionListener=new MyaddActionListener();
Button button =new Button();
button.addActionListener(myaddActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class MyaddActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("大数据五班");
}
}
public class TextActionEventTwo {
public static void main(String[] args) {
JFrame frame=new JFrame("开始--停止");
frame.setSize(400,400);
frame.locate(400, 300);
Button button1 =new Button("start");
Button button2 =new Button("stop");
MyMonitor myMonitor=new MyMonitor();
Button button =new Button();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button,BorderLayout.CENTER);
frame.add(button,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
e.getActionCommand();
System.out.println("大数据五班->"+e.getActionCommand());
if(e.getActionCommand().equals("stop")) {
System.out.println("我要下班了");
}else {
System.out.println("我要开始工作了");
}
}
}