0
点赞
收藏
分享

微信扫一扫

java可视化编程之Swing的使用

yundejia 2022-01-31 阅读 20

Swing的使用

  1. 构建JFrame程序主要有两种方式:

    • 创建JFrame类,通过实例化对象设置窗口的属性并添加组件,适合于小项目,窗口较少的情况
    • 创建一个类并继承JFrame,通过在构造方法中初始化窗口属性,该种方式适合于大项目,可以针对不同界面自定义一个Frame类,来实现不同的功能

    添加组件的方式:首先需要获取当前的窗口的内容面板,除菜单栏外,所有其余的组件都应该被添加到内容面板上

  2. 给组件注册事件的方式

    • 实现匿名内部类ActionListener

    • 创建内部类实现ActionListener接口,并重写其中的actionPerformed(ActionEvent e)方法

    • 当该类实现ActionListener接口并重写actionPerformed方法时,可以通过 组件.addActionListener(this) 的方式

      package gui.eric;
      
      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      
      
      public class MyFrame extends JFrame implements ActionListener {
      	public JLabel jLabel;
      	
      	public MyFrame() {}
      	public MyFrame(String title) {
      		super(title);
      		
      		//分别将标签组件、两个按钮添加到该页面的内容面板
      		this.jLabel = new JLabel("Hello Swing");
      		getContentPane().add(jLabel, BorderLayout.SOUTH);
      		
      		JButton jButton1 = new JButton("按钮1");
      		getContentPane().add(jButton1, BorderLayout.NORTH);
      		
      		JButton jButton2 = new JButton("按钮2");
      		getContentPane().add(jButton2);
      		
      		setSize(500, 500);
      		setVisible(true);
      		
      		//给两个按钮注册事件
      		jButton1.addActionListener(this);
      		jButton2.addActionListener(new ActionListener() {
      
      			@Override
      			public void actionPerformed(ActionEvent e) {
      				// TODO Auto-generated method stub
      				jLabel.setText("按钮2的处理程序");
      			}
      			
      		});
      	}
      	
      	public static void main(String[] args) {
      		MyFrame frame = new MyFrame("Hello Swing程序");
      	}
      	@Override
      	public void actionPerformed(ActionEvent e) {
      		// TODO Auto-generated method stub
      		this.jLabel.setText("按钮1的处理程序");
      	}
      
      }	
      
    • 使用适配器

      事件监听器都是接口,在java接口中定义的抽象方法必须全部实现,有时候因此会导致代码臃肿,因为可能很多方法我们并不关心,没有必要实现,所以java提供了一些与监听器相配套的适配器。监听器是接口,而适配器是类,命名通常采用XXX Adapter

      在使用时通过继承所对应的适配器类覆盖所需要的方法,无关方法无须实现,这大大简化了相关代码

      注:

      当需要多种监听器或此类已有父类时,无法采用事件适配器的方式,因为java的单继承性

      主要的适配器:

      • ComponentAdapter
      • ContainerAdapter
      • FocusAdapter
      • KeyAdapter
      • MouseAdapter
      • MouseMotionAdapter
      • WindowAdapter
  3. 布局管理

    java SE提供了7种布局管理器,包括:

    • FlowLayout
    • BorderLayout
    • GridLayout
    • BoxLayout
    • CardLayout
    • SpringLayout
    • GridLayout

    1. FlowLayout

    特点:从上到下,从左到右,第一个组件先摆放到第一行的最左边,后续的组件依次添加到上一个组件的右边,如果当前行摆放不下该组件,则摆放到下一行的最左边。

    package gui.eric;
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class FlowLayoutTest extends JFrame{
    	
    	public JLabel jLabel;
    	public FlowLayoutTest() {
    		super();
    	}
    	public FlowLayoutTest(String title) {
    		super(title);
    		jLabel = new JLabel("Hello FlowLayout");
    		getContentPane().add(jLabel);
    		setLayout(new FlowLayout(FlowLayout.LEADING));
    		JButton jButton1 = new JButton("按钮1");
    		getContentPane().add(jButton1);
    		JButton jButton2 = new JButton("按钮2");
    		getContentPane().add(jButton2);
    		
    		jButton1.addActionListener(new ActionListener() {
    
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				jLabel.setText("按钮一的处理程序");
    			}
    			
    		});
    		
    		jButton2.addActionListener(new ActionListener() {
    
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				jLabel.setText("按钮2的处理程序");
    			}
    			
    		});
    		
    		setSize(500, 500);
    		setVisible(true);
    	}
    	
    	
    	public static void main(String[] args) {
    		FlowLayoutTest flt = new FlowLayoutTest("FlowLayout程序");
    	}
    	
    }
    

    2.BorderLayout

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZEG2fh1u-1643472441638)(C:\Users\Eric\AppData\Roaming\Typora\typora-user-images\image-20220129233229920.png)]

BorderLayout布局是窗口的默认布局管理器,把整个容器分为5个区域:North West Center East South

实例程序:

package gui.eric;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class BorderLayoutTest extends JFrame{
	public JLabel jLabel;
	public BorderLayoutTest() {
		
	}
	public BorderLayoutTest(String title) {
		super(title);
		setLayout(new BorderLayout(10, 10));
		
		jLabel = new JLabel("标签组件");
		getContentPane().add(jLabel, BorderLayout.CENTER);
		
		JButton jButton1 = new JButton("按钮1");
		getContentPane().add(jButton1, BorderLayout.WEST);
		//按钮3 会把 按钮1 覆盖住
//		JButton jButton3 = new JButton("按钮3");
//		getContentPane().add(jButton3, BorderLayout.WEST);
		
		JButton jButton2 = new JButton("按钮2");
		getContentPane().add(jButton2, BorderLayout.EAST);
		
		jButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jLabel.setText("按钮1改变的标签的内容");
			}
			
		});
		
		jButton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jLabel.setText("按钮2改变的标签的内容");
			}
			
		});
		
		setSize(500, 500);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new BorderLayoutTest("BorderLayoutTest程序");
	}
}

4. GridLayout

特点:GridLayout将容器分成 raw*cols 的网格

示例代码:

package gui.eric;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class GridLayoutTest extends JFrame{
	
	public JLabel jLabel;
	public GridLayoutTest() {
		super();
	}
	public GridLayoutTest(String title) {
		super(title);
		jLabel = new JLabel("Hello FlowLayout");
		getContentPane().add(jLabel);
		setLayout(new GridLayout(4,4));
		JButton jButton1 = new JButton("按钮1");
		getContentPane().add(jButton1);
		JButton jButton2 = new JButton("按钮2");
		getContentPane().add(jButton2);
		getContentPane().add(new JButton("按钮3"));
		getContentPane().add(new JButton("按钮4"));
		getContentPane().add(new JButton("按钮5"));
		getContentPane().add(new JButton("按钮6"));
		getContentPane().add(new JButton("按钮7"));
		getContentPane().add(new JButton("按钮8"));
		getContentPane().add(new JButton("按钮9"));
		jButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jLabel.setText("按钮一的处理程序");
			}
			
		});
		
		jButton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jLabel.setText("按钮2的处理程序");
			}
			
		});
		
		setSize(500, 500);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		GridLayoutTest glt = new GridLayoutTest("GridLayout程序");
	}
	
}

在这里插入图片描述
不断学习中,未完待续……

举报

相关推荐

0 条评论