您的位置:首页 > 编程语言 > Java开发

Java自主学习

2016-03-03 21:16 543 查看

Java自主学习

Java自主学习
3-3

3-4

3-6

3-8
读取控制台输入
1 使用标准输入串Systemin

2 使用Scanner取得一个字符串或一组数字

3 使用BufferedReader取得含空格的输入

最近在系统的自学java,在这里记录每天的学习进程

3-3

今天学习swing组件:窗体组件 JFrame是Frame的子类,术语容器类组件,顶层容器。其中,容器类可以加入其它主键。

布局管理器有五种:流式布局管理器(FlowLayout) 边界布局管理器(BorderLayout)网格布局管理器(GridLayout)卡片布局管理器(CardLayout)网格包布局管理器(GridBagLayout) 前三种最常见。

//边界布局
package test01;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Demo_01 extends JFrame{

JButton jb1, jb2, jb3, jb4, jb5;

public static void main(String[] args) {
// TODO Auto-generated method stub
Demo_01 demo_01 = new Demo_01();
}

public Demo_01(){
jb1 = new JButton("中部");
jb2 = new JButton("北部");
jb3 = new JButton("东部");
jb4 = new JButton("南部");
jb5 = new JButton("西部");

// 添加组件
this.add(jb1, BorderLayout.CENTER);
this.add(jb2, BorderLayout.NORTH);
this.add(jb3, BorderLayout.EAST);
this.add(jb4, BorderLayout.SOUTH);
this.add(jb5, BorderLayout.WEST);

//设置窗体属性
this.setTitle("边界布局案例");
this.setSize(300, 200);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;

//show windows
this.setVisible(true);

}

}


3-4

// 网格布局
package com.text01;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Demo8_1 extends JFrame{
int size = 9;
JButton jbs[] = new JButton[size];
public static void main(String[] args) {
// TODO 自动生成的方法存根
Demo8_1 demo8_1 = new Demo8_1();
}

public Demo8_1(){
for(int i = 0; i < size; i++){
jbs[i] = new JButton(String.valueOf(i));
}

this.setLayout(new GridLayout(3, 3));

for(int i = 0; i < size; i++){
this.add(jbs[i]);
}

this.setTitle("GridLayout");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200, 200);

this.setVisible(true);
}

}


JPanel 是 Java图形用户界面(GUI)工具包swing中的面板容器类,包含在javax.swing 包中,是一种轻量级容器,可以加入到JFrame窗体中。JPanel默认的布局管理器是FlowLayout,其自身可以嵌套组合,在不同子容器中可包含其他组件(component),如JButton、JTextArea、JTextField 等,功能是对对窗体上的这些控件进行组合,相当于C++和C#中的Panel类。



package com.text01;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo8_2 extends JFrame{

JPanel jp1, jp2;
JButton jbs[] = new JButton[6];
public static void main(String[] args) {
// TODO 自动生成的方法存根
Demo8_2 demo8_2 = new Demo8_2();
}

public Demo8_2(){
//JPanel布局默认是FlowLayout
jp1 = new JPanel();
jp2 = new JPanel();

jbs[0] = new JButton("1");
jbs[1] = new JButton("2");
jbs[2] = new JButton("3");
jbs[3] = new JButton("4");
jbs[4] = new JButton("5");
jbs[5] = new JButton("6");

jp1.add(jbs[0]);
jp1.add(jbs[1]);
jp2.add(jbs[2]);
jp2.add(jbs[3]);
jp2.add(jbs[4]);

this.add(jp1, BorderLayout.NORTH);
this.add(jbs[5], BorderLayout.CENTER);
this.add(jp2, BorderLayout.SOUTH);

this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200, 200);
this.setVisible(true);
}

}


登陆框界面编写:



package com.text01;
import java.awt.*;
import javax.swing.*;

public class Demo8_3 extends JFrame{
JPanel jp1, jp2, jp3;
JLabel jlb1, jlb2;
JButton jb1, jb2;
JTextField jtf1;
JPasswordField jpf1;

public static void main(String[] args){
Demo8_3 demo8_3 = new Demo8_3();
}

public Demo8_3(){
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();

jlb1 = new JLabel("user");
jlb2 = new JLabel("Psw");
jb1 = new JButton("Login");
jb2 = new JButton("Cancel");;

jtf1 = new JTextField(10);
jpf1 = new JPasswordField(10);
this.setLayout(new GridLayout(3, 1));

jp1.add(jlb1);
jp1.add(jtf1);

jp2.add(jlb2);
jp2.add(jpf1);

jp3.add(jb1);
jp3.add(jb2);

this.add(jp1);
this.add(jp2);
this.add(jp3);

this.setSize(300, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200, 200);
this.setVisible(true);
}
}


3-6

ButtonGroup:This class is used to create a multiple-exclusion scope for a set of buttons. Creating a set of buttons with the same ButtonGroup object means that turning “on” one of those buttons turns off all other buttons in the group.

e.g.



/*
* 复选框与单选框案例
*/
package com.text01;
import java.awt.GridLayout;

import javax.swing.*;
public class Demo8_4 extends JFrame{
JPanel jp1, jp2, jp3;
JLabel jl1, jl2;
JButton jb1, jb2;
JCheckBox jcb1, jcb2, jcb3;
JRadioButton jrb1, jrb2;
ButtonGroup bg;

public static void main(String[] args) {
// TODO 自动生成的方法存根
Demo8_4 demo8_4 = new Demo8_4();
}

public Demo8_4(){
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();

jl1 = new JLabel("The sport you like ");
jl2 = new JLabel("The sex of you ");
jb1 = new JButton("cancel");
jb2 = new JButton("register");

jcb1 = new JCheckBox("football");
jcb2 = new JCheckBox("basketball");
jcb3 = new JCheckBox("baseball");

jrb1 = new JRadioButton("male");
jrb2 = new JRadioButton("female");

ButtonGroup bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
this.setLayout(new GridLayout(3, 1));

jp1.add(jl1);
jp1.add(jcb1);
jp1.add(jcb2);
jp1.add(jcb3);

jp2.add(jl2);
jp2.add(jrb1);
jp2.add(jrb2);

jp3.add(jb1);
jp3.add(jb2);

this.add(jp1);
this.add(jp2);
this.add(jp3);

this.setSize(300, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}




/*
* 1. 下拉框组件:JComboBox
* 2. 列表框组件: JList
* 3. 滚动窗格组件: JScrollPane
*/
package com.text01;

import java.awt.GridLayout;

import javax.swing.*;

public class Demo8_5 extends JFrame{
//define
JPanel jp1, jp2;
JLabel jl1, jl2;
JComboBox jcb1;
JList jList;
JScrollPane jsp;

public static void main(String[] args) {
// TODO 自动生成的方法存根
Demo8_5 demp8_5 = new Demo8_5();
}

// constructed function
public Demo8_5(){
jp1 = new JPanel();
jp2 = new JPanel();

jl1 = new JLabel("birth place:");
jl2 = new JLabel("travel place:");

String birthPlace[] = {"Beijing", "Shanghai", "Market"};
jcb1 = new JComboBox(birthPlace);

String travelPlace[] = {"jiuzaigou", "gugong", "wudangshan"};
jList = new JList(travelPlace);

// set the num of you want to display
jList.setVisibleRowCount(2);
jsp = new JScrollPane(jList);
// add primary key, 3 rows and 1 col
this.setLayout(new GridLayout(3, 1));

jp1.add(jl1);
jp1.add(jcb1);

jp2.add(jl2);
jp2.add(jsp);

this.add(jp1);
this.add(jp2);

this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

}


3-8

Java 流(Stream)、文件(File)和IO

Java.io包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。 Java.io包中的流支持很多种格式,比如:基本类型、对象、本地化字符集等等。 一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。 Java为I/O提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。

1. 读取控制台输入

1.1 使用标准输入串System.in

//System.in.read()一次只读入一个字节数据,而我们通常要取得一个字符串或一组数字
//System.in.read()返回一个整数
//必须初始化
//int read = 0;
char read = '0';
System.out.println("输入数据:");
try {
//read = System.in.read();
read = (char) System.in.read();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("输入数据:"+read);


1.2 使用Scanner取得一个字符串或一组数字

System.out.print("输入");
Scanner scan = new Scanner(System.in);
String read = scan.nextLine();
System.out.println("输入数据:"+read);

/*在新增一个Scanner对象时需要一个System.in对象,因为实际上还是System.in在取得用户输入。Scanner的next()方法用以取得用户输入的字符串;nextInt()将取得的输入字符串转换为整数类型;同样,nextFloat()转换成浮点型;nextBoolean()转换成布尔型。*/


1.3 使用BufferedReader取得含空格的输入

//Scanner取得的输入以space, tab, enter 键为结束符,
//要想取得包含space在内的输入,可以用java.io.BufferedReader类来实现
//使用BufferedReader的readLine( )方法
//必须要处理java.io.IOException异常
BufferedReader br = new BufferedReader(new InputStreamReader(System.in ));
//java.io.InputStreamReader继承了Reader类
String read = null;
System.out.print("输入数据:");
try {
read = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("输入数据:"+read);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java