Java窗口之文本框、按钮、菜单

文本框、按钮、文本域


import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
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 FrameDemo {
	public static void main(String[] args) {
		// 创建窗体对象
		Frame f = new Frame();
		// Frame ff = new Frame("ahahahah") 构造给窗体标题

		// 设置窗体标题
		f.setTitle("hahahaha");

		// 调节窗口大小
		f.setSize(500, 500);
		// 设置窗口位置
		// f.setLocation(400,200);

		// f.setBounds(x, y, width, height);
		// 设置窗口居中
		f.setLocationRelativeTo(null);

		// 流式布局
		f.setLayout(new FlowLayout());

		// 创建文本框
		TextField tf = new TextField(20); // 可以存20个字符

		// 创建按钮
		Button bu = new Button("数据转移");

		// 创建文本域
		TextArea ta = new TextArea(10, 40);

		// 组件添加到窗体
		f.add(tf);
		f.add(bu);
		f.add(ta);

		// 对按钮添加事件
		bu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// 获取文本框的值
				String tf_str = tf.getText().trim();
				// 清空数据
				tf.setText("");

				// 设置给文本域
				// ta.setText(tf_str);

				// 字符串比较
				if (tf_str.equals("123")) {
					// 追加和换行
					ta.append("123" + "\r\n");
				} else
					ta.append("输错了" + "\r\n");

				// 获取光标
				tf.requestFocus();
			}
		});

		// 适配器改进窗口关闭
		f.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		// 窗体可见
		f.setVisible(true);
	}
}

鼠标移动按钮更改背景颜色:

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;



public class FrameDemo2 {

	public static void main(String[] args) {
		//创建窗体
		Frame f = new Frame("按钮颜色");
		//大小
		f.setSize(500, 400);
		//居中
		f.setLocationRelativeTo(null);
		//流式布局
		f.setLayout(new FlowLayout());
		//设置按钮
		Button redButton = new Button("红色");
		Button greenButton = new Button("绿色");
		Button blueButton = new Button("蓝色");
		
		f.add(redButton);
		f.add(greenButton);
		f.add(blueButton);
		
		//窗口关闭
		f.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		
		//对按钮添加动作事件
//		redButton.addActionListener(new ActionListener() {
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				// TODO Auto-generated method stub
//				f.setBackground(Color.red);
//			}
//		});
		
		//对按钮的单击事件
//		redButton.addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseClicked(MouseEvent e) {
//				// TODO Auto-generated method stub
//				f.setBackground(Color.red);
//			}
//		});
		
		//对按钮添加鼠标进入事件
		redButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				f.setBackground(Color.red);
			}
		});
		//对按钮添加离开事件
		redButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				f.setBackground(Color.white);
			}
		});
		
		//对按钮添加鼠标进入事件
		greenButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				f.setBackground(Color.green);
			}
		});
		//对按钮添加离开事件
		greenButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				f.setBackground(Color.white);
			}
		});
		
		//对按钮添加鼠标进入事件
		blueButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				f.setBackground(Color.blue);
			}
		});
		//对按钮添加离开事件
		blueButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				f.setBackground(Color.white);
			}
		});
		//窗体可见
		f.setVisible(true);
	}
}

Java窗口之文本框、按钮、菜单_第1张图片

文本框输入加限制:


import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class JframeDemo {

	public static void main(String[] args) {
		
		// Jframe 类创建窗口
		JFrame jf = new JFrame("文本框");
		
		//大小
		jf.setSize(400, 400);
		
		//居中
		jf.setLocationRelativeTo(null);
		
		//流式布局
		jf.setLayout(new FlowLayout());
		Label label = new Label("只能输入数字,不信你试试!");
		//创建文本框
		TextField tf = new TextField(30);
		
		
		//加入窗体
		jf.add(label);
		jf.add(tf);
		
		
		//窗口可关闭
		jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		//可见
		jf.setVisible(true);
		tf.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				char ch = e.getKeyChar();
				if(!(ch>='0'&&ch<='9')) {
					e.consume();
				}
			}
		});
	}
}

菜单


import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class WIndowmenu extends JFrame{
	//菜单条
	JMenuBar menubar = new JMenuBar();
	//创建菜单
	JMenu menu = new JMenu("菜单");
	JMenu subMenu = new JMenu("软件项目");
	JMenuItem item,item2;
	public WIndowmenu() {
	}
	@SuppressWarnings("deprecation")
	public void init() {
		setTitle("带菜单的窗口");
		item = new JMenuItem("Java 话题",new ImageIcon("src\\menu\\aa.png"));
		item2 = new JMenuItem("动画话题",new ImageIcon("src\\\\menu\\\\bb.png"));
//		item.setAccelerator(KeyStroke.getKeyStroke('x'));
//		item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
		menu.add(item);
		menu.addSeparator();
		menu.add(item2);
		menu.add(subMenu);
		subMenu.add(new JMenuItem("汽车销售系统",new ImageIcon("src\\\\menu\\\\cc.png")));
		subMenu.add(new JMenuItem("农场信息系统",new ImageIcon("src\\\\menu\\\\dd.png")));
		menubar.add(menu);
		setJMenuBar(menubar);
		
		setLocationRelativeTo(null);
		setSize(400,300);
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	public static void main(String[] args) {
		WIndowmenu indowmenu = new WIndowmenu();
		indowmenu.init();
	}
}

Java窗口之文本框、按钮、菜单_第2张图片

你可能感兴趣的:(JAVA)