Swing特点 :
-
轻量级插件
-
可插入外观组件
Swing包
常用Swing包组件
JButton | 代表Swing按钮 按钮可带图片或文字 |
---|---|
JCheckBox | 代表Swing中的复选框组件 |
JComBox | 代表下拉列表框,可以在下拉显示区域中显示多个选项 |
JFrame | 代表Swing的框架结构 |
JDialog | 代表Swing版本的对话框 |
JLabel | 代表Swing中的标签组件 |
JRadioButton | 代表Swing中的单选按钮 |
JList | 代表下拉列表框,可以在下拉显示区域中显示多个选项 |
JTextField | 代表文本框 |
JPasswordField | 代表密码框 |
JTextArea | 代表Swing中的文本区域 |
JOptionPane | 代表Swing中的一些对话框 |
1.JFrame窗体
创建窗体
public class demo {
public demo() {
JFrame jf =new JFrame("这是一个JFrame窗口");//创建一个名为JFrame的窗口
Container c=jf.getContentPane();
jf.setSize(300,200);//设置窗口尺寸,300为宽,200为高
jf.setVisible(true);//设置窗口可视化
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗口关闭模式
}
public static void main(String[]args) {
new demo();
}
类若继承了JFrame,则可以不用创建新的JFrame对象,直接添加容器Container
public class demo extends JFrame{
public demo() {
Container c=getContentPane("这是一个JFrame窗口");//创建一个名为JFrame的窗口
setVisible(true);
setSize(300,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[]args) {
new demo();
}
2.JLabel标签
public class demo extends JFrame{
JLabel jl =new JLabel("hello world");//创建一个显示hello world的标签
public demo() {
Container c=getContentPane("这是一个JFrame窗口");
c.add(jl);// 将标签添加进容器
setSize(300,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[]args) {
new demo();
}
效果:
3.JDialog窗体
public JDialog();//创建一个没有标题和父窗体的对话框
public JDialog(Frame f);//创建一个无标题,有父窗体的对话框
public JDialog(Frame f,boolean model);//创建一个指定类型的的对话框,并指定父窗体
public JDialog(Frame f,String title);//创建一个有标题,有父窗体的对话框
public JDialog(Frame f ,String title ,boolean model);
//创建一个带标题,带父窗体的对话框,并设置对话框的类型
class MyDialog extends JDialog{
public MyDialog(demo frame) {
super(frame,"第一个JDialog窗体",true);
//public JDialog(Frame f,String title,boolean model):创建一个指定标题、窗体、和模式的对话框)
Container v=getContentPane();
v.add(new JLabel("这是一个标签"));
setBounds(120,120,100,100);
setVisible(true);
}
}
public class demo extends JFrame{
JLabel jl =new JLabel("hello world");
JButton jb=new JButton("这是一个按钮");//创建一个按钮
public demo() {
Container c=getContentPane();
jb.setBounds(10, 10, 100, 21);
jb.addActionListener(new ActionListener(){ //为按钮添加一个鼠标单击事件
public void actionPerformed(ActionEvent e) {
new MyDialog(demo.this);
}
});
setSize(300,200);
setVisible(true);
c.add(jb);
c.add(jl);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[]args) {
new demo();
}
}
效果:
4.标签组件与图标
标签由JLabel类定义,它的父类为JCompoent类
public JLabel();//创建一个不带图标和文本的JLabel对象
public JLabel(Icon icon);//创建一个带图标的JLabel对象
public JLabel(Icon icon,int aligment);//创建一个带图标的JLabel对象,并设置图标水平对齐方式
public JLabel(String text,int aligment);//创建一个带文本的JLabel对象,并设置图标水平对齐方式
public JLabel(String text,Icon icon,int aligment);
//创建一个带文本,带图标的JLabel对象,并设置标签内容的水平对齐方式
图标的使用
图标可以用Java支持的图片类型进行创建,也可以用java.awt.Graphics类提供的方法来创建
创建图标
public int getIconHeight();//获取图表的长
public int getIconWidth();//获取图表的宽
public void paintIcon(Compoent arg0,Graphics arg1,int,arg2,int arg3);
//在指定坐标位置画图
public class tubiaochuangkou implements Icon {
private int width;
private int height;
@Override
public int getIconHeight() {
// TODO Auto-generated method stub
return this.height;
}
@Override
public int getIconWidth() {
// TODO Auto-generated method stub
return this.width;
}
public tubiaochuangkou(int width,int height){
this.height=height;
this.width=width;
}
@Override
public void paintIcon(Component arg0, Graphics arg1, int x, int y) {
// TODO Auto-generated method stub
arg1.fillOval(x, y, width, height);//绘制一个圆形
}
public static void main(String args[]){
tubiaochuangkou icon=new tubiaochuangkou(15,15);//x=15,y=15
JLabel j =new JLabel("测试",icon,SwingConstants.CENTER);
//创建一个标签,并设置标签上的文字在标签正中间
JFrame jf =new JFrame();
Container container =jf.getContentPane();
jf.setVisible(true);
jf.setSize(200,150);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
container.add(j);
}
}
结果: