java_Swing桌面程序开发
1.按钮动作事件
单击按钮时触发动作事件,ActionEvent
(动作事件)
需要为按钮添加事件监听器——编写动作事件监听器通过实现 ActionListener
监听接口来完成。
ActionListener中只需要实现actionPerformed
方法,将触发时间后要执行的程序都写在actionPerformed
中。
import javax.swing.*;
import java.awt.event.*;
public class Swing1 extends JFrame
{
int i=0;
JPanel jp=new JPanel();
JButton jb= new JButton("按钮");
public Swing1()
{
this.setTitle("创建面板");
jp.add(jb);
jb.setMnemonic('F');
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Swing1.this.jb.setText("次数:"+(++i));
}
}
);
this.add(jp);
this.setBounds(300,250,500,200);
this.setVisible(true);
}
public static void main(String[] args)
{
Swing1 s1=new Swing1();
}
}
运行结果:
2.1. 同一个事件注册多个监听器
import javax.swing.*;
import java.awt.event.*;
public class Swing1 extends JFrame
{
int i=0;
JPanel jp=new JPanel();
JButton jb= new JButton("按钮");
public Swing1()
{
this.setTitle("创建面板");
jp.add(jb);
jb.setMnemonic('F');
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Swing1.this.jb.setText("次数1:"+(++i));
}
}
);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Swing1.this.jb.setText("次数2:"+(++i));
}
}
);
this.add(jp);
this.setBounds(300,250,500,200);
this.setVisible(true);
}
public static void main(String[] args)
{
Swing1 s1=new Swing1();
}
}
运行结果:
2.2. 同一个监听器注册给多个事件源
import javax.swing.*;
import java.awt.event.*;
public class Swing2 extends JFrame implements ActionListener
{
int i=0;
int j=0;
JPanel jp=new JPanel();
JButton jb1= new JButton("按钮1");
JButton jb2= new JButton("按钮2");
public Swing2()
{
this.setTitle("创建面板");
jp.add(jb1);
jp.add(jb2);
jb1.setMnemonic('N');
jb2.setMnemonic('F');
jb1.addActionListener(this);
jb2.addActionListener(this);
this.add(jp);
this.setBounds(300,250,500,200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb2)
{
Swing2.this.jb2.setText("按钮2次数:"+(++j));
}
else if(e.getSource()==jb1)
{
Swing2.this.jb1.setText("按钮1次数:"+(++i));
}
}
public static void main(String[] args)
{
Swing2 s1=new Swing2();
}
}
3. 窗体获取和失去焦点事件
通过WindowFocusListener
监听接口实现
import javax.swing.*;
import java.awt.event.*;
public class Swing2 extends JFrame implements WindowFocusListener
{
int i=0;
int j=0;
JPanel jp=new JPanel();
JButton jb1= new JButton("按钮1");
public Swing2()
{
this.setTitle("创建面板");
this.addWindowFocusListener(this); //注册窗体获取和失去焦点事件
this.setBounds(300,250,500,200);
this.setVisible(true);
jp.add(jb1);
jb1.setMnemonic('N');
this.add(jp);
}
public void windowGainedFocus(WindowEvent e)
{
System.out.println("窗体获取焦点");
}
public void windowLostFocus(WindowEvent e)
{
System.out.println("窗体失去焦点");
}
public static void main(String[] args)
{
Swing2 s1=new Swing2();
}
}
4. 窗体打开、关闭和**事件
WindowListener接口
import javax.swing.*;
import java.awt.event.*;
public class Swing2 extends JFrame implements WindowListener
{
int i=0;
int j=0;
JPanel jp=new JPanel();
JButton jb1= new JButton("按钮1");
public Swing2()
{
this.setTitle("创建面板");
this.setBounds(300,250,500,200);
this.setVisible(true);
this.addWindowListener(this);
jp.add(jb1);
jb1.setMnemonic('N');
this.add(jp);
}
public void windowOpened(WindowEvent e)
{
System.out.println("窗体首次变为可见");
}
public void windowClosing(WindowEvent e)
{
System.out.println("关闭窗体");
}
public void windowClosed(WindowEvent e)
{
System.out.println("关闭窗体后");
}
public void windowIconified(WindowEvent e)
{
System.out.println("从正常状态变为最小化状态");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("从最小化状态变为正常状态");
}
public void windowActivated(WindowEvent e)
{
System.out.println("窗体被**");
}
public void windowDeactivated(WindowEvent e)
{
System.out.println("窗体变为非**");
}
public static void main(String[] args)
{
Swing2 s1=new Swing2();
}
}
运行结果:
窗体被**
窗体首次变为可见
从正常状态变为最小化状态
窗体变为非**
从最小化状态变为正常状态
窗体被**
关闭窗体
窗体变为非**
5. 密码框
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KongJian extends JFrame implements ActionListener
{
JTextField jtf=new JTextField(10);
JPasswordField jpf =new JPasswordField(10);
JLabel jl1=new JLabel("用户名:");
JLabel jl2=new JLabel("密码:");
JButton jb=new JButton("提交");
JLabel j1=new JLabel();
JPanel jp=new JPanel();
public KongJian()
{
this.setTitle("创建文本框");
jp.setLayout(null);
jl1.setBounds(30,20,80,30);
jp.add(jl1);
jl2.setBounds(30,70,80,30);
jp.add(jl2);
jtf.setBounds(80,20,180,30);
jp.add(jtf);
jpf.setBounds(80,70,180,30);
jp.add(jpf);
jb.setBounds(50,130,80,30);
jp.add(jb);
j1.setBounds(10,180,300,30);
jp.add(j1);
jb.addActionListener(this);
this.add(jp);
this.setBounds(300,250,350,250);
this.setVisible(true);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
String s1=jtf.getText();
String s2=jpf.getText();
j1.setText("你的用户名为:"+s1+" 密码为:"+s2);
}
public static void main(String[] args)
{
KongJian kj= new KongJian();
}
}
6.多行文本框
import javax.swing.*;
//import java.*;
//import java.awt.event.*;
public class DuoHang extends JFrame
{
JTextArea jta=new JTextArea(6,10);
JLabel j1=new JLabel("多行文本框");
JPanel jp=new JPanel();
public DuoHang()
{
this.setTitle("创建文本框");
jp.add(jta);
jp.add(j1);
this.add(jp);
this.setBounds(300,250,300,200);
this.setVisible(true);
}
public static void main(String[] args)
{
DuoHang dh=new DuoHang();
}
}