JFrame简单使用
发布日期:2021-06-30 21:07:59 浏览次数:2 分类:技术文章

本文共 4811 字,大约阅读时间需要 16 分钟。

1.

import java.awt.Color;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import javax.swing.JFrame;public class MyFrame {	public static void main(String[] args) {		JFrame frame = new JFrame("我的第一个Frame");		frame.setSize(500 , 1000);		frame.getContentPane().setBackground(Color.RED);		frame.setVisible(true);				BufferedReader intemp = new BufferedReader(new InputStreamReader(System.in));		System.out.println("Press return key to exit.");		try{			String s = intemp.readLine();		} catch(IOException e){			System.out.println("IOException");		}				System.exit(0);				}//main 结束}

2.

import java.awt.Color;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import javax.swing.JFrame;import javax.swing.JPanel;public class FrameWithPanelTest {	public static void main(String[] args) {		JFrame frame = new JFrame("My Frame");		frame.setSize(500 , 500);		frame.getContentPane().setBackground(Color.BLACK);		frame.setVisible(true);				JPanel contentPane = new JPanel();		contentPane.setSize(100 , 100);		contentPane.setBackground(Color.yellow);		frame.add(contentPane);		frame.setLayout(null); //不加上这一句设置布局,contentPanel会铺满整个frame				BufferedReader intemp = new BufferedReader(new InputStreamReader(System.in));		System.out.println("Press return key to exit.");		try{			String s = intemp.readLine();		} catch(IOException e){			System.out.println("IOException");		}		System.exit(0);							}}

3.

import java.awt.Color;import java.awt.FlowLayout;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class FrameWithPanelTest {	public static void main(String[] args) {		JFrame frame = new JFrame("My Frame");		frame.setSize(500 , 500);		frame.getContentPane().setBackground(Color.BLACK);		frame.setLayout(new FlowLayout(FlowLayout.CENTER , 50 , 50)); //为JFrame顶层容器设置FlowLayout布局管理器						JPanel contentPane = new JPanel();		contentPane.setSize(100 , 100);		//内部按钮对齐方式,水平和垂直间距		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER , 50 , 50)); //为Japnel设置布局管理器		contentPane.setBackground(Color.yellow);		JButton btn1 , btn2 , btn3 ; //定义3个按钮		btn1 = new JButton("打开");		btn2 = new JButton("关闭");		btn3 = new JButton("返回");		contentPane.add(btn1);		contentPane.add(btn2);		contentPane.add(btn3);				frame.add(contentPane);		frame.setVisible(true); //显示JFrame						BufferedReader intemp = new BufferedReader(new InputStreamReader(System.in));		System.out.println("Press return key to exit.");		try{			String s = intemp.readLine();		} catch(IOException e){			System.out.println("IOException");		}		System.exit(0);							}}

4.在上面代码加入下列语句

contentPane.add(new JLabel()); //空标签站位用contentPane.add(new JLabel()); //空标签站位用contentPane.add(new JLabel()); //空标签站位用

5.鼠标按键测试代码

import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import javax.swing.JFrame;import javax.swing.JLabel;//鼠标及按键public class MouseControl implements MouseMotionListener , KeyListener{	private JFrame frame;	private JLabel tf;	String ch = ""; //放置待显示的字符		public static void main(String[] args){		MouseControl two = new MouseControl();		two.go();	}			private void go() {		frame = new JFrame("鼠标按键测试");		Container contentPane = frame.getContentPane();		contentPane.add(new JLabel("get mouse and keyboard event") , BorderLayout.NORTH);		tf = new JLabel();		contentPane.add(tf, BorderLayout.SOUTH);		//注册监听程序		frame.addMouseMotionListener(this);		frame.addKeyListener(this);				frame.setSize(300 , 300);		frame.setVisible(true);			}	//接下来为实现接口中的方法	@Override	public void keyTyped(KeyEvent e) { //顺序 keypress keytype keyrelease		// TODO Auto-generated method stub		System.out.println("2");		int charcode = e.getKeyCode();		ch = "有键按下2";		if(charcode == KeyEvent.VK_SHIFT) {ch = "shift2";}		if(charcode == KeyEvent.VK_CONTROL) ch = "control2";	}	@Override	public void keyPressed(KeyEvent e) { //看不到显示,因为keytype覆盖了		// TODO Auto-generated method stub		System.out.println("1");		int charcode = e.getKeyCode();		ch = "有键按下1";		if(charcode == KeyEvent.VK_SHIFT) ch = "shift1";		if(charcode == KeyEvent.VK_CONTROL) ch = "control1";	}	@Override	public void keyReleased(KeyEvent e) {		// TODO Auto-generated method stub		//ch = "有键松开";		System.out.println("3");	}	@Override	public void mouseDragged(MouseEvent e) { //按住鼠标拖动才会调用		// TODO Auto-generated method stub		String s = "鼠标拖动的坐标: X =" +e.getX() + " Y =" + e.getY() + " KEY:" + ch;		tf.setText(s);	}	@Override	public void mouseMoved(MouseEvent e) {		// TODO Auto-generated method stub		String s = "鼠标移动时的坐标: X =" +e.getX() + " Y =" + e.getY() + " KEY:" + ch;		tf.setText(s);	}	}

 

 

 

 

 

 

转载地址:https://liuyan.blog.csdn.net/article/details/81487872 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:String.format()格式化字符
下一篇:简单工厂和工厂方法模式-代码分析

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月24日 05时22分07秒