AWT简介
- 组件:Component
- 容器:Container
- 布局管理器:LayoutManager
组件
- 在整个AWT包中所有的组件类都是从Component和MenuComponent扩展而来的,这些类会继承这两个类的公共操作
容器
布局管理器
Swing简介
- Swing使用java语言实现,是以AWT平台为基础构建起来的新组件,直接使用Swing可以更加轻松地构建用户界面,它保存在javax.swing包中,它所有的组件都是从JComponent扩展出来的.JComponent类几乎是所有Swing组件的公共超类,从各个组件的定义来看所有的Swing组件只是比AWT组件前多增加了一个字母"J"而已
基本容器:JFrame
- JFrame类常用操作方法
- JFrame中使用Dimension类设置窗体的大小,此类封装的是组件显示宽度和高度
- Point类针对组件的显示位置即封装的是坐标
public class demo {
public static void main(String[] args) {
JFrame f=new JFrame("第一个Swing窗体");
Dimension d=new Dimension();
d.setSize(230,80);
f.setSize(d);
f.setBackground(Color.WHITE);
Point p=new Point(300,200);
f.setLocation(p);
f.setVisible(true);
}
}
标签组件:JLabel
认识JLable
- 此组件是一个标签,本身是用于显示信息的,一般情况下是不能直接更改其显示内容的,通过Container类中的add()方法加入到容器中
- JLable类的常用方法和常量
public class demo {
public static void main(String[] args) {
JFrame f=new JFrame("土豆向对你弹出窗口");
JLabel lab=new JLabel("我真的不想动啊啊啊啊啊啊啊啊啊啊啊啊啊啊",JLabel.CENTER);
f.add(lab);
Dimension dim=new Dimension();
dim.setSize(200,100);
f.setSize(dim);
f.setBackground(Color.WHITE);
Point p=new Point(300,200);
f.setLocation(p);
f.setVisible(true);
}
}
更改JLabel的文字样式
- 使用Component类定义的setFont(Font f)方法
- Font类的常用操作方法及常量
public class demo {
public static void main(String[] args) {
JFrame f=new JFrame("土豆向对你弹出窗口");
JLabel lab=new JLabel("我真的不想动啊啊啊啊啊啊啊啊啊啊啊啊啊啊",JLabel.CENTER);
Font word=new Font("Serief",Font.ITALIC+Font.BOLD,28);
lab.setFont(word);
lab.setForeground(Color.pink);
f.add(lab);
Dimension dim=new Dimension();
dim.setSize(200,100);
f.setSize(dim);
f.setBackground(Color.WHITE);
Point p=new Point(300,200);
f.setLocation(p);
f.setVisible(true);
}
}
在JLabel中设置图片
- 使用Icon接口以及ImageIcon子类即可
- Imagelcon类的构造方法
public class demo {
public static void main(String[] args) {
JFrame f=new JFrame("土豆向对你弹出窗口");
String picPath="f:"+File.separator+"109951163541891985.jpg";
File file=new File(picPath);
InputStream input=null;
byte b[]=new byte[(int)file.length()];
try{
input=new FileInputStream(file);
input.read(b);
input.close();
}catch (Exception e){
e.printStackTrace();
}
Icon icon=new ImageIcon(b);
JLabel lab=new JLabel("我真的不想动啊啊啊啊啊啊啊啊啊啊啊啊啊啊",icon,JLabel.CENTER);
Font word=new Font("Serief",Font.ITALIC+Font.BOLD,28);
lab.setFont(word);
lab.setForeground(Color.pink);
lab.setBackground(Color.lightGray);
f.add(lab);
f.setSize(200,300);
f.setBackground(Color.WHITE);
f.setLocation(300,200);
f.setVisible(true);
}
}
按钮组件:JButton
- JButton组件表示一个普通的按钮,使用此类可以直接在窗体中增加一个按钮
- JButton类常用方法
public class demo {
public static void main(String[] args) {
JFrame f = new JFrame("你好");
String picpath = "f:" + File.separator + "109951163541891985.jpg";
Icon icon = new ImageIcon(picpath, "你好");
JButton jb = new JButton(icon);
Font font = new Font("Serief", Font.BOLD, 25);
jb.setFont(font);
f.add(jb);
f.setSize(230, 80);
f.setLocation(100, 100);
f.setBackground(Color.WHITE);
f.setVisible(true);
}
}