图形界面
java使用到的图形类主要在java.awt 与javax.swing包中。
java.awt 与 javax.swing包的区别:
java.awt中使用的图形类都是依赖于系统 的图形库的。
javax.swing包使用到的图形类都是sun自己实现,不需要依赖系统的图形库。在java中所有的图形类都被称作组件类。
组件的类别:
———-| 容器组件
———-| 非容器组件
案例
public static void main(String[] args) {
JFrame frame = new JFrame("这个是我第一个图形化界面的例子");
//设置窗体的大小
// frame.setSize(300,400);
//设置窗体(左上角)出现的位置
//frame.setBounds((1366-300)/2, (768-400)/2, 300,400); // 第一个参数是左上角的x轴坐标, 第二参数是左上角y的坐标。 第三个窗体宽, 第四窗体的高。
initFrame(frame, 300,400);
frame.setVisible(true); //setVisible 设置窗体的可见性。
//设置窗体关闭事件
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//获取屏幕的分辨率 设置窗体在屏幕的居中位置。
public static void initFrame(JFrame frame,int width , int height){
Toolkit toolkit = Toolkit.getDefaultToolkit(); //获取一个与系统相关工具类对象
//获取屏幕的分辨率
Dimension d = toolkit.getScreenSize();
int x = (int) d.getWidth();
int y = (int) d.getHeight();
frame.setBounds((x-width)/2, (y-height)/2, width, height);
}
对话框类(Dialog):
JDialog(Dialog owner, String title, boolean modal)
owner: 所有者
title : 标题
modal : modal
JOptionPane(对话框)
消息对话框
警告对话框
错误对话框
输入对话框
确认对话框使用
这里面的FrameUtil是提取案例中设置的共同部分形成的工具类public static void main(String[] args) { /*JFrame frame = new JFrame("窗体"); //创建对话框 JDialog dialog = new JDialog(frame, "对话框",true); //使用我自定义的 窗体工具类 FrameUtil.initFrame(frame, 300, 400); dialog.setBounds(500,300, 100, 200); dialog.setVisible(true); //设置对话框的可见性 */ JFrame frame = new JFrame("窗体"); //显示一个对话框 FrameUtil.initFrame(frame, 300, 400); //消息对话框 /*JOptionPane.showMessageDialog(frame, "明天不用上课", "通知",JOptionPane.INFORMATION_MESSAGE); //警告对话框 JOptionPane.showMessageDialog(frame,"警告某位同学晚上晚自习不要看动漫","警告",JOptionPane.WARNING_MESSAGE);*/ //错误对话框 // JOptionPane.showMessageDialog(frame,"扣6分","错误",JOptionPane.ERROR_MESSAGE); //输入框 /* String moeny = JOptionPane.showInputDialog("请输入你要给我的金额($)"); System.out.println("money:"+ moeny);*/ //确认框 int num = JOptionPane.showConfirmDialog(frame, "你确认要卸载吗?"); System.out.println(num);
文件对话框(FileDialog):
FileDialog(Dialog parent, String title, int mode)
parent: 对话框的所有者
tiltle : 对话框的标题
mode: load(打开) 、 save(保存)public static void main(String[] args) { JFrame frame = new JFrame("窗体"); //创建一个文件对话框(初始也是不可见) //FileDialog fileDialog = new FileDialog(frame, "请选择打开的文件", FileDialog.LOAD); FileDialog fileDialog = new FileDialog(frame,"请选择保存的路径",FileDialog.SAVE); FrameUtil.initFrame(frame, 300,400); fileDialog.setVisible(true); System.out.println("文件所在的目录:"+ fileDialog.getDirectory()); System.out.println("文件名:"+ fileDialog.getFile()); }
Jpanel
public static void main(String[] args) { JFrame frame = new JFrame("窗体"); //创建一个面板 JPanel panel = new JPanel(); //设置面板的背景颜色 panel.setBackground(Color.RED); //把面板添加到窗体 frame.add(panel); FrameUtil.initFrame(frame, 400, 300); }
非容器组件
使用
public static void main(String[] args) { JFrame frame= new JFrame("注册"); //创建一个面板 JPanel panel = new JPanel(); frame.add(panel); //用户名 JLabel nameLabel = new JLabel("用户名"); //用户名的输入框 JTextField nameField = new JTextField(12); //把用户名的组件添加到面板上 panel.add(nameLabel); panel.add(nameField); //密码 JLabel passLabel= new JLabel("密码"); //密码框 JPasswordField passField = new JPasswordField(12); //把密码的组件添加到面板 panel.add(passLabel); panel.add(passField); //性别--单选框 JLabel sexLabel = new JLabel("性别"); JRadioButton man = new JRadioButton("男",true); JRadioButton woman = new JRadioButton("女"); //如果是单选框必须要进行分组,同一个组的单选框只能选择其中的一个 ButtonGroup group = new ButtonGroup(); group.add(woman); group.add(man); //把性别组件添加到面板上 panel.add(sexLabel); panel.add(man); panel.add(woman); //来自城市--->下拉框 JLabel cityLabel = new JLabel("来自的城市"); Object[] arr = {"北京","上海","广州","深圳","湛江"}; JComboBox citys = new JComboBox(arr); panel.add(cityLabel); panel.add(citys); //兴趣爱好---->复选框 JLabel hobitLabel = new JLabel("兴趣爱好:"); JCheckBox checkBox1 = new JCheckBox("篮球",true); JCheckBox checkBox2 = new JCheckBox("java",true); JCheckBox checkBox3 = new JCheckBox("javascript"); JCheckBox checkBox4 = new JCheckBox("android"); panel.add(hobitLabel); panel.add(checkBox1); panel.add(checkBox2); panel.add(checkBox3); panel.add(checkBox4); //个人简介 JLabel jLabel = new JLabel("个人简介"); JTextArea area = new JTextArea(20, 15); area.setLineWrap(true); //设置自动换行 panel.add(jLabel); panel.add(area); FrameUtil.initFrame(frame, 500, 400); }
菜单组件
- 使用
JFrame frame = new JFrame("记事本");
//菜单条
JMenuBar bar = new JMenuBar();
//文件菜单
JMenu fileMenu = new JMenu("文件");
JMenu editMenu = new JMenu("编辑");
JMenu switchMenu = new JMenu("切换工作目录");
//菜单项
JMenuItem openMenu = new JMenuItem("打开");
JMenuItem saveMenu = new JMenuItem("保存");
JMenuItem aboutMenu = new JMenuItem("关于");
JMenuItem closeMenu = new JMenuItem("关闭");
JMenuItem workMenu1 = new JMenuItem("0910project");
JMenuItem workMenu2 = new JMenuItem("1208project");
JMenuItem workMenu3 = new JMenuItem("1110project");
JTextArea area = new JTextArea(20,30);
public void initNotepad(){
//菜单添加菜单项目
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
editMenu.add(aboutMenu);
editMenu.add(closeMenu);
//复选菜单
switchMenu.add(workMenu1);
switchMenu.add(workMenu2);
switchMenu.add(workMenu3);
//菜单添加菜单就是复选菜单
fileMenu.add(switchMenu);
//菜单条添加菜单
bar.add(fileMenu);
bar.add(editMenu);
//添加菜单条
frame.add(bar,BorderLayout.NORTH);
frame.add(area);
FrameUtil.initFrame(frame, 500, 600);
}
public static void main(String[] args) {
new Demo2().initNotepad();
}
布局管理器
布局管理器:布局管理就是用于指定组件的 摆放位置的。
每种布局管理器都有自己的摆放风格
BorderLayout(边框布局管理器)
摆放的风格: 上北 、 下南 、 左西、 右东 , 中Borderlayout 要注意的事项:
- 使用Borderlayout添加组件的时候,如果没有指定组件的方位,那么默认添加到中间的位置上。
- 使用BorderLayout的时候,如果东南西北那个方向没有对应 的组件,那么中间位置的组件就会占据其空缺的位置。
- 窗体默认的布局管理器就是Borderlayout.
使用
public static void main(String[] args) { JFrame frame = new JFrame("边框局部管理器"); //创建一个边框布局管理器 BorderLayout borderLayout = new BorderLayout(); //让borderlayout管理frame窗体。 frame.setLayout(borderLayout); frame.add(new JButton("北"),BorderLayout.NORTH); frame.add(new JButton("南"),BorderLayout.SOUTH); frame.add(new JButton("西"),BorderLayout.WEST); frame.add(new JButton("东"),BorderLayout.EAST); frame.add(new JButton("中"),BorderLayout.CENTER); //初始化窗体 FrameUtil.initFrame(frame, 300, 300);
- 流式布局管理器(FlowLayout)
流式布局管理器要注意的事项:
- 流式布局管理器默认情况是居中对齐的。
- panel默认的局部管理器就是FlowLayout.
public static void main(String[] args) { JFrame frame = new JFrame("窗体"); //创建面板 JPanel panel = new JPanel(); frame.add(panel); //创建一个流式布局管理器 FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 0, 30);// FlowLayout.LEFT 指定对齐的方式。 //让流式布局管理器管理frame窗体 panel.setLayout(flowLayout); panel.add(new JButton("按钮1")); panel.add(new JButton("按钮2")); panel.add(new JButton("按钮3")); panel.add(new JButton("按钮4")); //初始化窗体 FrameUtil.initFrame(frame, 300, 300); }
表格布局管理器(GridLayout)
注意的事项: 如果表格数量不够使用时,默认会多加一列。public static void main(String[] args) { JFrame frame = new JFrame("计算器"); //创建表格布局管理器 GridLayout gridLayout = new GridLayout(4, 4, 1, 2); //让窗体交给表格布局管理器管理 frame.setLayout(gridLayout); for(int i = 0 ; i<10; i++){ frame.add(new JButton(i+"")); } frame.add(new JButton("+")); frame.add(new JButton("-")); frame.add(new JButton("*")); frame.add(new JButton("/")); frame.add(new JButton("=")); frame.add(new JButton(".")); // frame.add(new JButton("aa")); //初始化窗体 FrameUtil.initFrame(frame, 300, 300); }
卡片布局管理器(CardLayout)
public static void main(String[] args) { JFrame frame = new JFrame("卡片布局管理器"); final JPanel panel = new JPanel(); frame.add(panel); //创建一个卡片布局管理器 final CardLayout cardLayout = new CardLayout(); panel.setLayout(cardLayout); //往面板添加数据 JButton button = new JButton("黑桃A"); panel.add(button); panel.add(new JButton("红桃K")); panel.add(new JButton("梅花6")); panel.add(new JButton("方块2")); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cardLayout.next(panel); //下一张 // cardLayout.previous(parent); 上一张 } }); //初始化窗体 FrameUtil.initFrame(frame,300, 300); }
事件
案例
public static void main(String[] args) { JFrame frame = new JFrame("窗体"); JButton button = new JButton("点我啊"); frame.add(button); //给按钮添加动作监听器 动作时间监听器对于鼠标点击以及空格键都会起作用的。 button.addActionListener(new ActionListener() { //当按钮被点击的时候,就会调用actionPerformed的方法。 @Override public void actionPerformed(ActionEvent e) { // ActionEvent 当前按钮被点击的时候,jvm就会把对应 的时间传递ActionEvent,并且调用actionPerformed方法。 //System.out.println("哎呀,被点了..."); JButton button =(JButton) e.getSource(); //getSource() 获取到事件源 if(button.getText().equals("点我啊")){ button.setText("点他吧"); }else{ button.setText("点我啊"); } } });
鼠标事件监听器
public static void main(String[] args) { JFrame frame = new JFrame("鼠标事件监听器"); JButton button = new JButton("按钮"); frame.add(button); //给按钮添加鼠标事件监听器 /*button.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { System.out.println("鼠标松开..."); } @Override public void mousePressed(MouseEvent e) { System.out.println("鼠标按下.."); } @Override public void mouseExited(MouseEvent e) { System.out.println("鼠标移出..."); } @Override public void mouseEntered(MouseEvent e) { System.out.println("鼠标进入..."); } @Override public void mouseClicked(MouseEvent e) { System.out.println("鼠标单击.."); } }); 添加鼠标监听器的时候我只使用 到单击事件,但是目前要我实现所有的方法?? 解决方案: 适配器。适配器是实现了MouseListener方法的所有方法,但是实现的方法全部都是空实现。 */ button.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { // System.out.println("鼠标单击了.."); if(e.getClickCount()==2){ System.out.println("双击了.."); } } }); FrameUtil.initFrame(frame, 300, 300); }
键盘事件监听器
public static void main(String[] args) { JFrame frame = new JFrame("键盘事件监听器"); JButton button = new JButton("按钮"); frame.add(button); //给按钮添加键盘事件监听器 /*button.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { System.out.println("键入某个键"); } @Override public void keyReleased(KeyEvent e) { // System.out.println("释放某个键"); } @Override public void keyPressed(KeyEvent e) { System.out.println("按下某个键.."); } });*/ button.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { // System.out.println("按下的字符:"+e.getKeyChar()); //System.out.println("获取键对应的数值:"+ e.getKeyCode()); int code = e.getKeyCode(); switch (code) { case 38: System.out.println("上"); break; case 40: System.out.println("下"); break; case 37: System.out.println("左"); break; case 39: System.out.println("右"); break; default: break; } } }); FrameUtil.initFrame(frame,300, 300); }
4.