Java自主學習
最近在系統的自學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);