欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java GUI

程序员文章站 2024-01-27 09:07:34
...

Frame

package gui.lesson01;

import java.awt.Color;
import java.awt.Frame;

public class FrameTest {
	public static void main(String[] args) {
		Frame frame=new Frame("Java窗口");
		//设置可见性
		frame.setVisible(true);
		//设置窗口大小
		frame.setSize(400, 400);
		//设置背景颜色
		frame.setBackground(new Color(200,200,200));
		//弹出的初始位置
		frame.setLocation(100,100);
		//设置大小固定
		frame.setResizable(false);
		
	}
}

封装

package gui.lesson01;

import java.awt.Color;
import java.awt.Frame;

public class FrameTest {
	public static void main(String[] args) {
		Frame frame=new Frame("Java窗口");
		//设置可见性
		frame.setVisible(true);
		//设置窗口大小
		frame.setSize(400, 400);
		//设置背景颜色
		frame.setBackground(new Color(200,200,200));
		//弹出的初始位置
		frame.setLocation(100,100);
		//设置大小固定
		frame.setResizable(false);
		
	}
}

Panel

package gui.lesson01;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class PanelTest {
	public static void main(String[] args) {
		Frame frame=new Frame();
		Panel panel=new Panel();
		//设置布局
		frame.setLayout(null);
		frame.setBounds(200, 200, 300, 300);
		frame.setBackground(Color.BLUE);
		//Panel设置坐标
		panel.setBounds(50, 50, 200, 200);
		panel.setBackground(Color.lightGray);
		
		frame.add(panel);
		frame.setVisible(true);
		//监听事件
		//适配器模式
		frame.addWindowListener(new WindowAdapter() {
			//窗口点击关闭的时候要做的事情
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
}

常用布局

流式布局

package gui.layout;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//流式布局
public class FlowLayoutTest {
	public static void main(String[] args) {
		Frame frame=new Frame();
		Button button1=new Button("one");
		Button button2=new Button("two");
		Button button3=new Button("three");
		//设置为流式布局
		frame.setLayout(new FlowLayout(FlowLayout.LEFT));
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.add(button1);
		frame.add(button2);
		frame.add(button3);
		frame.addWindowListener(new WindowAdapter() {
			//窗口点击关闭的时候要做的事情
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
}

东西南北中

package gui.layout;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class BorderLayoutTest {
	public static void main(String[] args) {
		Frame frame=new Frame("BorderLayoutTest");
		Button button1=new Button("east");
		Button button2=new Button("west");
		Button button3=new Button("south");
		Button button4=new Button("north");
		Button button5=new Button("center");
		frame.add(button1,BorderLayout.EAST);
		frame.add(button2,BorderLayout.WEST);
		frame.add(button3,BorderLayout.SOUTH);
		frame.add(button4,BorderLayout.NORTH);
		frame.add(button5,BorderLayout.CENTER);
		frame.setSize(200, 200);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
}

表格布局

package gui.layout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GirdLayoutTest {
	public static void main(String[] args) {
		Frame frame=new Frame("GirdrLayoutTest");
		Button button1=new Button("btn1");
		Button button2=new Button("btn2");
		Button button3=new Button("btn3");
		Button button4=new Button("btn4");
		Button button5=new Button("btn5");
		Button button6=new Button("btn6");
		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();
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
}

嵌套

package gui.layout;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class LayoutTest {
	public static void main(String[] args) {
		Frame frame=new Frame();
		frame.setSize(500,500);
		frame.setLocation(300, 300);
		frame.setLayout(new GridLayout(2,1));
		frame.setVisible(true);
		Panel panel1=new Panel(new BorderLayout());
		Panel panel2=new Panel(new GridLayout(2,1));
		Panel panel3=new Panel(new BorderLayout());
		Panel panel4=new Panel(new GridLayout(2,2));
		panel1.add(new Button("aaa"),BorderLayout.EAST);
		panel1.add(new Button("bbb"),BorderLayout.WEST);
		panel2.add(new Button());
		panel2.add(new Button());
		panel1.add(panel2,BorderLayout.CENTER);
		panel3.add(new Button("ccc"),BorderLayout.EAST);
		panel3.add(new Button("ddd"),BorderLayout.WEST);
		for (int i = 0; i < 4; i++) {
			panel4.add(new Button());
		}
		panel3.add(panel4,BorderLayout.CENTER);
		frame.add(panel1);
		frame.add(panel3);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
}

监听器

事件监听器

package gui.listener;

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ActionTest {
	public static void main(String[] args) {
		Frame frame=new Frame();
		Button button=new Button("hello");
		frame.add(button);
		frame.pack();
		frame.setVisible(true);
		MyActionListener myActionListener=new MyActionListener();
		button.addActionListener(myActionListener);
		windowsClose(frame);
	}
	private static void windowsClose(Frame frame) {
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
}
class MyActionListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e.getActionCommand());
	}
	
}

文字输入

package gui.listener;

import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TextTest {
	public static void main(String[] args) {
		new MyFrame();
		
	}
}

class MyFrame extends Frame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MyFrame() {
		// TODO Auto-generated constructor stub
		TextField textField=new TextField();
		add(textField);
		//监听文本框输入的文字
		MyActionListenerTwo myActionListener=new MyActionListenerTwo();
		textField.addActionListener(myActionListener);
		//设置替换编码
		textField.setEchoChar('*');
		setVisible(true);
		pack();
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
}
class MyActionListenerTwo implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//获得一些资源
		TextField field=(TextField)e.getSource();
		System.out.println(field.getText());//获得输入框中的文本
		field.setText("");//null
	}
	
}

计算器

package gui.listener;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CalaTest {
	public static void main(String[] args) {
		new Calulator().loadFrame();
	}
}
class Calulator extends Frame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	TextField num1,num2,num3;
	public void loadFrame() {
		num1=new TextField(10);//字符数
		num2=new TextField(10);//字符数
		num3=new TextField(20);//字符数
		Button button=new Button("=");
		Label label=new Label("+");
		button.addActionListener(new MyCalulatorListener());
		
		setLayout(new FlowLayout());
		add(num1);
		add(label);
		add(num2);
		add(button);
		add(num3);
		pack();
		setVisible(true);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
	public Calulator() {
		// TODO Auto-generated constructor stub
	}
	/*
	 * 内部类
	private class MyCalulatorListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			int n1=Integer.parseInt(num1.getText());
			int n2=Integer.parseInt(num2.getText());
			num3.setText(""+(n1+n2));
			num1.setText("");
			num2.setText("");
		}
		
	}*/
}

class MyCalulatorListener implements ActionListener{
	//在一个类中组合另外一个类
	Calulator calulator=null;
	public MyCalulatorListener() {
		// TODO Auto-generated constructor stub
	}
	public MyCalulatorListener(Calulator calulator) {
		// TODO Auto-generated constructor stub
		this.calulator=calulator;
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		int num1=Integer.parseInt(calulator.num1.getText());
		int num2=Integer.parseInt(calulator.num2.getText());
		calulator.num3.setText(""+(num1+num2));
		calulator.num1.setText("");
		calulator.num2.setText("");
	}
	
}

鼠标监听

package gui.listener;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class MouseListener {
	public static void main(String[] args) {
		new MyMouseFrame("画图");
	}
}
class MyMouseFrame extends Frame{
	ArrayList<Object> points;
	private static final long serialVersionUID = 1L;
	
	public MyMouseFrame() {
		// TODO Auto-generated constructor stub
	}
	public MyMouseFrame(String title) {
		// TODO Auto-generated constructor stub
		super(title);
		setBounds(200,200,400,300);
		//存储鼠标点击的点
		points=new ArrayList<Object>();
		//鼠标监听器
		this.addMouseListener(new MyMouseListener());
		setVisible(true);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//super.windowClosing(e);
				System.exit(0);
			}
		});
	}
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		//super.paint(g);
		Iterator<Object> 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 addPaint(Point point) {
		points.add(point);
	}
	//适配器模式
	private class MyMouseListener extends MouseAdapter{
		//鼠标按下,弹起,按住不放
		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			//super.mousePressed(e);
			MyMouseFrame frame=(MyMouseFrame)e.getSource();
			//鼠标的点
			frame.addPaint(new Point(e.getX(),e.getY()));
			//每次点击鼠标需要重新画一遍
			frame.repaint();
		}
	}
}

窗口监听

package gui.listener;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class WindowTest {
	public static void main(String[] args) {
		new WindowFrame();
	}
}
class WindowFrame extends Frame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public WindowFrame() {
		pack();
		setVisible(true);
		//addWindowListener(new MyWindowListener());
		//匿名内部类
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);//正常退出
			}
		});
	}
	
	/*class MyWindowListener extends WindowAdapter{
		@Override
		public void windowClosing(WindowEvent e) {
			// TODO Auto-generated method stub
			//super.windowClosing(e);
			System.exit(0);//正常退出
		}
	}*/
}

键盘监听

package gui.listener;

import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class KeyTest {
	public static void main(String[] args) {
		new KeyFrame();
	}
}
class KeyFrame extends Frame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public KeyFrame() {
		setBounds(100,200,300,400);
		setVisible(true);
		
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_UP) {
					System.out.println("上");
				}
			}
		});
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);//正常退出
			}
		});
	}
}

JFrame

package gui.swing;

import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class JFrameDemo {
	public static void main(String[] args) {
		new MyJFrame().init();
	}
	
}
class MyJFrame extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void init() {
		this.setBounds(200, 200, 200, 200);
		this.setVisible(true);
		Container container=this.getContentPane();
		container.setBackground(Color.cyan);
		//设置文字
		JLabel jLabel=new JLabel("hi");
		this.add(jLabel);
		//设置水平对齐
		jLabel.setHorizontalAlignment(SwingConstants.CENTER);
		//关闭事件
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	public MyJFrame() {
		
	}
}

弹窗

package gui.swing;

import java.awt.Container;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class DialogTest extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static void main(String[] args) {
		new DialogTest();
	}
	public DialogTest() {
		this.setVisible(true);
		this.setSize(500, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		Container container=this.getContentPane();
		//绝对布局
		container.setLayout(null);
		JButton jButton=new JButton("this");
		jButton.setBounds(30,30,200,50);
		jButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//弹窗
				new MyDialog();
			}
		});
		container.add(jButton);
		
	}
}
class MyDialog extends JDialog{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public MyDialog() {
		this.setVisible(true);
		this.setBounds(100,100,300,300);
		Container container=this.getContentPane();
		//container.setLayout(null);
		container.add(new Label("hhh"));
	}
}