JAVA(图形界面)

1 容器

1.1 JFrame

 public static void main(String[] args){
    	JFrame f = new JFrame("LoL");
        f.setSize(400, 300);//设置容器尺寸
        f.setLocation(200, 200);//设置容器位置
        f.setLayout(null);//设置布局。
        JButton b = new JButton("一键秒对方基地挂");
        b.setBounds(50, 50, 280, 30);//设置按钮在容器中的位置
 
        f.add(b);//将按钮加在容器上
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//界面关闭后程序结束
 
        f.setVisible(true);//界面可视化。
    }

JFrame可以在创建之时命名,而JDialog则需要使用方法setText()

1.2 JDialog

public static void main(String[] args) {
    	JDialog a = new JDialog();
    	a.setTitle("Demo");
    	a.setSize(500, 600);
    	a.setLocation(200, 400);
    	a.setLayout(null);
    	a.setVisible(true);
    }	

JDialog也是窗口类型的容器,只不过右上角没有最大最小化。

public static void main(String[] args) {
    	JFrame b = new JFrame();
    	JDialog a = new JDialog(b);//实例化外部窗体JDialog
    	b.setSize(500, 600);
    	b.setLocation(500, 600);
    	b.setLayout(null);
    	a.setModal(true);//设为模态,背后的父窗体不被激活,当关闭外部窗口后才显示
    	a.setTitle("Demo");
    	a.setSize(400, 200);
    	a.setLocation(200, 400);
    	a.setLayout(null);
    	a.setVisible(true);
    	b.setVisible(true);
    }

1.3 JFrame+JDialog

public static void main(String[] args){
    	JFrame a = new JFrame("lol");//创建父窗体。
    	a.setSize(800, 600);//设置父窗体。
    	a.setLocation(100, 100);//设置父窗体位置。
   
    	JDialog d = new JDialog(a);//创建子窗体,添加在父窗体之上。
    	d.setModal(true);//父窗体设为模态,不能点击。
    	d.setTitle("yingx");//设置子窗体标题。
    	d.setLocation(200, 200);//设置子窗体位置。
    	d.setSize(400, 300);//设置子窗体大小。
    	d.setLayout(null);//解除子窗体布局填充。
    	
    	JButton b = new JButton("1");
    	b.setBounds(50, 50, 280, 30);
    	d.add(b);
    	a.setVisible(true);//父窗体
    	d.setVisible(true);//子窗体。
    }

1.4 JFrame+JDialog+事件监听器

public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(800, 600);
    	a.setLayout(null);
    	
    	JButton b = new JButton("打开一个模态窗口");
    	b.setLocation(150,200);
    	b.setSize(300, 300);
    	b.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			JDialog c = new JDialog(a);
    			c.setTitle("这是一个模态窗口");
    			c.setSize(200, 200);
    			c.setLocation(300, 300);
    			c.setLayout(null);
    			
    			JButton jb = new JButton("解锁大小");
    			jb.setBounds(50,50,280,30);
    			jb.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					if(c.isResizable()){
    						jb.setText("解锁大小");
    						c.setResizable(false);
    					}else{
    						jb.setText("锁定大小");
    						c.setResizable(true);
    					}
    				}
    			});
    			c.add(jb);
    			c.setResizable(false);
    			c.setVisible(true);
    		}
    	});
    	a.add(b);
    	a.setVisible(true);   
    }

setVisible()放在最后,会对所有组件渲染
1.5 监听器
按钮监听器

 public static void main(String[] args) {
    	JFrame a = new JFrame("QQ");
    	a.setSize(500, 300);//窗口的尺寸
    	a.setLocation(500,600);//窗口的位置
    	a.setLayout(null);//组件释放
    	JLabel k = new JLabel();//标签类实例化一个对象
    	ImageIcon i = new ImageIcon("f:/Googledowload/shana.png");//在图标类中实例化一个对象并链接上绝对地址
    	k.setIcon(i);//定义此组间将要显示的图标
    	k.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());//设置图片大小
    	a.add(k);//将标签加到容器中
    	JButton b = new JButton("琴女");//设置一个按钮
    	b.setBounds(300, 200, 100, 30);//设置这个按钮的位置
    	a.add(b);//把琴女的按钮加到容器中
    	b.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){//事件响应器,当点击按钮后会执行方法中的代码
    			k.setVisible(false);//隐藏图片
    		}
    	}); 	    	
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭后程序结束
    	a.setVisible(true);//窗口可视化
    }	

键盘监听器

package gui;
  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
  
public class TestGUI {
    public static void main(String[] args) {
    	JFrame a = new JFrame("QQ");
    	a.setSize(500, 300);//窗口的尺寸
    	a.setLocation(500,600);//窗口的位置
    	a.setLayout(null);//组件释放
    	JLabel k = new JLabel();//标签类实例化一个对象
    	ImageIcon i = new ImageIcon("f:/Googledowload/shana.png");//在图标类中实例化一个对象并链接上绝对地址
    	k.setIcon(i);//定义此组间将要显示的图标
    	k.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());//设置图片大小
    	a.add(k);//将标签加到容器中
    	JButton b = new JButton("琴女");//设置一个按钮
    	b.setBounds(300, 200, 100, 30);//设置这个按钮的位置
    	a.add(b);//把琴女的按钮加到容器中
    	b.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){//事件响应器,当点击按钮后会执行方法中的代码
    			k.setVisible(false);//隐藏图片
    		}
    	}); 
    	b.addKeyListener(new KeyListener(){//键盘监听器
    		public void keyReleased(KeyEvent e){
    			if(e.getKeyChar() == 'e'  ){//点击e实现方法中的功能
    				k.setLocation(k.getX()+10, k.getY());
    			}
    		}
    		public void keyPressed(KeyEvent e){//键盘按下
    			
    		}
    		public void keyTyped(KeyEvent e){//弹起按下的组合
    			
    		}
    	});
    	b.setMnemonic('i');//设置一个快捷键ALT+i 相当于点击按钮
    	b.setToolTipText("点我");//设置一个信息,当鼠标移动到按钮上会显示出来字体
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭后程序结束
    	a.setVisible(true);//窗口可视化
    }	
    
}

1.6 JLabel
用标签显示文字。

public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(null);
    	
    	JLabel b = new JLabel("xxx");
    	b.setForeground(Color.red);
    	b.setBounds(50,50,280,30);
    	
    	a.add(b);
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

用标签显示图片

    public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(null);
    	
    	JLabel b = new JLabel("xxx");
    	b.setBounds(50,50,280,30);
    	
    	ImageIcon i = new ImageIcon("e:/");
    	b.setIcon(i);
    	b.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
    	
    	a.add(b);
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

1.7 JCheckBox
复选框

    public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(null);
    	
    	JCheckBox c = new JCheckBox("1");
    	c.setSelected(true);
    	c.setBounds(50,50,130,30);
    	
    	JCheckBox d = new JCheckBox("2");
    	d.setSelected(true);
    	d.setBounds(50, 100, 130, 30);
    	
    	a.add(c);
    	a.add(d);
    	
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

1.8 JRadioButton+ButtonGroup
单选框:
将按钮放在同一个用户组里面,同一时间只能选一个按钮。

   public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(null);
    	
    	JRadioButton c = new JRadioButton("1");
    	c.setSelected(true);
    	c.setBounds(50,50,130,30);
    	
    	JRadioButton d = new JRadioButton("2");
    	d.setBounds(50, 100, 130, 30);
    	
    	ButtonGroup bg = new ButtonGroup();
    	bg.add(c);
    	bg.add(d);
    	
    	a.add(c);
    	a.add(d);
    	
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

1.9 JComboBox
复选框

    public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(null);
    	
    	String[] c = new String[]{"1","2"};
    	JComboBox d = new JComboBox(c);
    	d.setBounds(50, 50, 80, 30);
      	
    	a.add(d);
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

1.10 JOptionPane
对话框

    public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(null);
    	
      	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    	
    	int option = JOptionPane.showConfirmDialog(a, "打开?");
    	if(JOptionPane.OK_OPTION == option){
    		String answer = JOptionPane.showInputDialog(a,"输入yes");
    		if("yes".equals(answer))
    			JOptionPane.showMessageDialog(a, "ok");
    	}

1.11 JTextField
文本框

    public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(new FlowLayout());
    	
    	JLabel b = new JLabel("账号:");
    	JTextField c = new JTextField("");
    	c.setText("请输入账号:");
    	c.setPreferredSize(new Dimension(80,30));
    	
    	JLabel d = new JLabel("密码:");
    	JTextField f = new JTextField("");
    	f.setText("请输入密码:");
    	f.setPreferredSize(new Dimension(80,30));
    	
    	a.add(b);
    	a.add(c);
    	a.add(d);
    	a.add(f);
    	
      	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true); 
    }

1.11 JPasswadField
密码框,返回一个字符数组而非字符串。

   public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(new FlowLayout());
    	
    	JLabel b = new JLabel("账号:");
    	JTextField c = new JTextField("");
    	c.setText("请输入账号:");
    	c.setPreferredSize(new Dimension(80,30));
    
    	JLabel d = new JLabel("密码:");
    	JPasswordField f = new JPasswordField("");
    	f.setText("请输入密码:");
    	f.setPreferredSize(new Dimension(80,30));
    	char [] password = f.getPassword();
    	String p = String.valueOf(password);
    	System.out.println(p);
    	
    	a.add(b);
    	a.add(c);
    	a.add(d);
    	a.add(f);
    	
      	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true); 
    }

1.12

2 布局

2.1绝对定位(自定义定位)

    public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(800, 600);
    	a.setLayout(null);
    	
    	JButton b1 = new JButton("1");
    	b1.setBounds(50,50,80,30);//将按钮的位置和尺寸一并设置,相当于setlocation和setsize的合体。
    	JButton b2 = new JButton("2");
    	b2.setBounds(150,50,80,30);
    	JButton b3 = new JButton("3");
    	b3.setBounds(250,50,80,30);
    	
    	a.add(b1);
    	a.add(b2);
    	a.add(b3);
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

2.2 FlowLayout(流式布局)

public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(800, 600);
    	a.setLayout(new FlowLayout());
    	
    	JButton b1 = new JButton("1");
    	JButton b2 = new JButton("2");
    	JButton b3 = new JButton("3");
    	
    	a.add(b1);
    	a.add(b2);
    	a.add(b3);
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

BorderLayout(边框布局)

public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(800, 600);
    	a.setLayout(new BorderLayout());
    	
    	JButton b1 = new JButton("1");
    	JButton b2 = new JButton("2");
    	JButton b3 = new JButton("3");
    	JButton b4 = new JButton("4");
    	JButton b5 = new JButton("5");
    	
    	
    	a.add(b1,BorderLayout.NORTH);
    	a.add(b2,BorderLayout.EAST);
    	a.add(b3,BorderLayout.SOUTH);
    	a.add(b4,BorderLayout.WEST);
    	a.add(b5, BorderLayout.CENTER);
    	
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

GridLayout(网格布局)

  public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(800, 600);
    	a.setLayout(new GridLayout(2,3));//两行三列
    	
    	JButton b1 = new JButton("1");
    	JButton b2 = new JButton("2");
    	JButton b3 = new JButton("3");
    	JButton b4 = new JButton("4");
    	JButton b5 = new JButton("5");
    	
    	
    	a.add(b1);
    	a.add(b2);
    	a.add(b3);
    	a.add(b4);
    	a.add(b5);
    	
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

占个坑,还缺卡片布局,以后补上。

setPreferredSize

  public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(800, 600);
    	a.setLayout(new FlowLayout());
    	
    	JButton b1 = new JButton("7");
    	JButton b2 = new JButton("8");
    	JButton b3 = new JButton("9");
    	JButton b4 = new JButton("/");
    	JButton b5 = new JButton("sq");
    	b3.setPreferredSize(new Dimension(180,40));//使用布局管理器后再改变大小
    	
    	a.add(b1);
    	a.add(b2);
    	a.add(b3);
    	a.add(b4);
    	a.add(b5);
    	
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }

1.0 JLabel

public static void main(String[] args){
    	JFrame a = new JFrame("lol");
    	a.setLocation(100, 100);
    	a.setSize(500, 400);
    	a.setLayout(null);
    	
    	JLabel b = new JLabel("xxx");
    	b.setForeground(Color.red);
    	b.setBounds(50,50,280,30);
    	
    	a.add(b);
    	a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	a.setVisible(true);
    }
  • 49
    点赞
  • 220
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值