GUI编程
简介
- GUI核心技术:Swing AWT
- 缺点:1.界面不美观 2.需要jre环境
优点:1.可以写小工具 2.工作时,可能需要维护Swing界面 3.了解MVC
AWT(抽象的窗口程序)
AWT介绍
- 包含很多类和接口!
- 元素:窗口,按钮,面板,文本框,按钮,图片,监听事件,鼠标,键盘事件
- java.awt
组件和容器
Frame
Frame frame = new Frame("我的第一个GUI界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口宽高
frame.setSize(400,400);
//设置背景颜色
Color color = new Color(91, 128, 120);
frame.setBackground(color);
//弹出初始化位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
关闭窗口需要关闭程序
- 封装:
public class Test2Frame {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100,100,400,400,new Color(128, 6, 0));
MyFrame myFrame2 = new MyFrame(500,100,400,400,new Color(0, 116, 128));
MyFrame myFrame3 = new MyFrame(100,500,400,400,new Color(87, 128, 0));
MyFrame myFrame4 = new MyFrame(500,500,400,400,new Color(62, 0, 128));
}
}
class MyFrame extends Frame{
static int id = 0; //多个窗口,需要计算
public MyFrame(int x,int y,int h,int w,Color color) throws HeadlessException {
super("MyFrame"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
面板Panel
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(255,255,255));
//Panel坐标
panel.setBounds(50,50,200,200);
panel.setBackground(new Color(255,255,0));
frame.add(panel);
frame.setVisible(true);
//监听窗口关闭事件 system.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
布局管理器
- 流式布局
Frame frame = new Frame();
//组件 按钮
Button button1=new Button("button1");
Button button2=new Button("button2");
Button button3=new Button("button3");
//设置流式布局
// frame.setLayout(new FlowLayout());
// frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setBounds(300,300,500,500);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
- 东西南北中
Frame frame = new Frame();
Button east=new Button("East");
Button west=new Button("West");
Button north=new Button("North");
Button south=new Button("south");
Button center=new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.add(center,BorderLayout.CENTER);
frame.setBounds(300,300,500,500);
frame.setVisible(true);
- 表格布局Grid
Frame frame = new Frame();
//组件 按钮
Button button1=new Button("button1");
Button button2=new Button("button2");
Button button3=new Button("button3");
Button button4=new Button("button4");
Button button5=new Button("button5");
Button button6=new Button("button6");
frame.setLayout(new GridLayout(3,2));
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack(); //java函数 找到合适的布局,并且显示 使得frame、按钮大小不用设置
frame.setVisible(true);
小结
1.Frame是一个顶级窗口
2.Panel无法单独使用
3.布局管理
4.大小、定位、背景颜色、可见性,监听
事件监听
- 事件监听:
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
//按下按钮触发一些事情
Button button = new Button("button");
//因为,addActionListener 需要一个ActionListener,所以写一个方法去实现该接口
button.addActionListener(new MyActionListener());
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
//关闭窗体事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.printf("ActionListener");
}
}
文本框监听
public class TestText1 {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame() throws HeadlessException {
TextField textField = new TextField();
add(textField);
//监听文本框输入文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下回车键,触发此事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
//事件监听
class MyActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource();
System.out.println("输入了:"+ textField.getText());
}
}
画笔Paint
public class TestPaint {
public static void main(String[] args) {
MyPaint myPaint = new MyPaint();
myPaint.loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,400,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//画笔所需的颜色,画笔画画
g.setColor(Color.BLACK);
g.drawOval(50,100,100,100);
g.setColor(Color.RED);
g.fillOval(200,150,100,100);//实心圆
g.setColor(Color.GREEN);
g.fillRect(150,200,100,100);
//养成习惯,用完画笔,记得还原为原来的颜色
super.paint(g);
}
}
鼠标监听
- 目的:实现鼠标画画
public class TestMouseListener {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame("画画");
}
}
//
class MyFrame extends Frame {
//画笔、监听鼠标位置、存放画画的点
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,400,400);
//存鼠标的点
points = new ArrayList<>();
setVisible(true);
//监听鼠标
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.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面上
public void addPoint(Point point){
points.add(point);
}
private class MyMouseListener extends MouseAdapter {
//鼠标按下
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
//点击在界面点击一个点,画出来
myFrame.addPoint( new Point(e.getX(),e.getY()));
//每次点击重写画画
myFrame.repaint();
}
}
}
窗口监听
- 常用的是关闭和激活
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
// setBackground(Color.blue);
setBounds(100, 100, 400, 400);
setVisible(true);
addWindowListener(
//匿名内部类
new WindowAdapter() {
//关闭
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
//激活
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
});
}
// class MyWindowListener extends WindowAdapter {
// @Override
// public void windowClosing(WindowEvent e) {
// setVisible(false); //隐藏窗口
// System.exit(0); //正常退出
// }
// }
}
键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() throws HeadlessException {
setBounds(100,100,400,400);
setVisible(true);
addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if(keyCode == KeyEvent.VK_UP){
System.out.println("上");
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
}
}
Swing
窗口、面板
public class JFrameDemo2 {
public static void main(String[] args) {
new MyJfram().init();
}
}
class MyJfram extends JFrame {
public void init(){
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.orange);
JLabel lable = new JLabel("GUI学习");
this.add(lable,BorderLayout.CENTER);
//标签居中
lable.setHorizontalAlignment(SwingConstants.CENTER);
setBounds(100,100,500,500);
setVisible(true);
}
}
弹窗
public class DialogDemo extends JFrame {
public DialogDemo() throws HeadlessException {
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//容器放东西
Container container = this.getContentPane();
//设置绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("打开对话框");
button.setBounds(30,30,100,50);
//点击按钮的时候,弹出弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//打开弹窗窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container = new Container();
container.add(new Label("Swing学习"));
}
}
标签
- label
new JLable("XXXX")
- icon
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo() {
}
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(5,5);
//图标可u放在 标签 按钮上面
JLabel lable = new JLabel("icon", iconDemo, SwingConstants.CENTER);
Container contentPane = this.getContentPane();
contentPane.add(lable);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
public static void main(String[] args) {
new IconDemo().init();
}
}
- imageIcon
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片地址
JLabel lable = new JLabel("ImageIcon");
//找到指定类下的,指定名称图片的路径
URL resource = ImageIconDemo.class.getResource("xy.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
lable.setIcon(imageIcon);
lable.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = this.getContentPane();
contentPane.add(lable);
setVisible(true);
setBounds(100,100,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
- Jpanel
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = getContentPane();
container.setLayout(new GridLayout(2,1,10,10)); //两行一列,间距为10
JPanel jPanel = new JPanel(new GridLayout(1, 3));;
JPanel jPanel2 = new JPanel(new GridLayout(1, 2));;
JPanel jPanel3 = new JPanel(new GridLayout(2, 1));;
JPanel jPanel4 = new JPanel(new GridLayout(3, 1));;
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
container.add((jPanel));
container.add((jPanel2));
container.add((jPanel3));
container.add((jPanel4));
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
- JScroll
public class JScrollDemo extends JFrame {
public JScrollDemo() throws HeadlessException {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,30);
textArea.setText("GUI学习");
//scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
按钮
- 图片按钮
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() throws HeadlessException {
Container container = this.getContentPane();
//获取icon
URL resource = JButtonDemo01.class.getResource("tp.png");
JButton jButton = new JButton();
jButton.setIcon(new ImageIcon(resource));
jButton.setToolTipText("点赞");
container.add(jButton);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
- 单选按钮
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() throws HeadlessException {
Container container = this.getContentPane();
//单选框
JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton01);
buttonGroup.add(jRadioButton02);
buttonGroup.add(jRadioButton03);
container.add(jRadioButton01,BorderLayout.CENTER);
container.add(jRadioButton02,BorderLayout.WEST);
container.add(jRadioButton03,BorderLayout.EAST);
this.setVisible(true);
this.setBounds(100,100,400,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
- 复选按钮
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() throws HeadlessException {
Container container = this.getContentPane();
//多选框
JCheckBox checkBox01 = new JCheckBox("CheckBox01");
JCheckBox checkBox02 = new JCheckBox("CheckBox02");
JCheckBox checkBox03 = new JCheckBox("CheckBox03");
container.add(checkBox01,BorderLayout.EAST);
container.add(checkBox02,BorderLayout.CENTER);
container.add(checkBox03,BorderLayout.WEST);
this.setVisible(true);
this.setBounds(100,100,400,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
列表
- 下拉框
public class ComboBoxDemo01 extends JFrame {
public ComboBoxDemo01() throws HeadlessException {
Container contentPane = this.getContentPane();
JComboBox<Object> objectJComboBox = new JComboBox<>();
objectJComboBox.addItem(null);
objectJComboBox.addItem("即将上映");
objectJComboBox.addItem("正在热映");
objectJComboBox.addItem("即将下架");
objectJComboBox.addItem("已下架");
contentPane.add(objectJComboBox);
this.setVisible(true);
this.setBounds(100,100,500,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ComboBoxDemo01();
}
}
- 列表框
public class ComboBoxDemo02 extends JFrame {
public ComboBoxDemo02() throws HeadlessException {
Container contentPane = this.getContentPane();
//生成列表的内容
// String[] contents = {"1111","2222","3333"};
Vector contents = new Vector();
//列表中需要放内容
JList jList = new JList(contents);
contents.add("zhangshan");
contents.add("lixi");
contents.add("wangwu");
contentPane.add(jList);
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ComboBoxDemo02();
}
}
- 应用场景
1.选择地区,或者一些单个选项
2.列表,展示信息,一般是动态扩容的
文本框
public class TextDemo01 extends JFrame {
public TextDemo01() throws HeadlessException {
Container contentPane = this.getContentPane();
JTextField jTextField1 = new JTextField("hello");
JTextField jTextField2 = new JTextField("world",20); //限制的行
contentPane.add(jTextField1,BorderLayout.NORTH);
contentPane.add(jTextField2,BorderLayout.SOUTH);
//密码框
JPasswordField jPasswordField = new JPasswordField(); //默认输入为*
jPasswordField.setEchoChar('#'); //将*改为#
contentPane.add(jPasswordField);
this.setVisible(true);
this.setBounds(100,100,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextDemo01();
}
}