初學者學習Java代碼
其實我學習java最根本的原因是:我是一個挺關注外在的人,雖然是個程序員,所以我很喜歡寫出那些帶有漂亮的界面的程序,因為C總是控制臺,我不是很喜歡,在這份java代碼合集中,我會記錄自己學習Java界面化編程的點點滴滴。
更新:因為C/C++是我主要使用語言,所有后來寫界面主要用Qt寫了,但我java也會繼續學的。我只是給想學界面gui的同志一個思路。可以參考這篇文章Qt5 計算器的實現
可能會有java初學者,,我也是,說明,java是一個工程里可以有很多java類class,每一個類class都可以單獨運行,不像C語言里只能有一個main()函數可以運行,這是我的代碼合集程序結構:

helloworld:
- class Javahelloworld {
- public static void main(String args[]){
- System.out.println("hello world\n");
- }}
基本輸入輸出:
- import java.util.Scanner;
- public class ScannerTest {
- public static void main(String[] args){
- Scanner scanner=new Scanner(System.in);
- System.out.print("請輸入一個數");
- int a=scanner.nextInt();
- System.out.printf("%d的平方是%d\n",a,a*a);
- }}
Java圖形化界面求數的平方:
- import java.awt.*;import java.awt.event.*;
- import javax.swing.*;/**包含JFrame*/
- public class AppGraphInOut {
- public static void main(String args[]){
- new AppFrame();
- }} class AppFrame extends JFrame
- { JTextField in=new JTextField(10);
- JButton btn=new JButton("求平方");
- JLabel out=new JLabel("用于顯示平方結果的標簽");
- public AppFrame()
- { setLayout(new FlowLayout());
- getContentPane().add(in);
- getContentPane().add(btn);
- getContentPane().add(out);
- btn.addActionListener(new BtnActionAdapter());
- setSize(400,100);
- setDefaultCloseOperation(DISPOSE_ON_CLOSE); setVisible(true);
- } class BtnActionAdapter implements ActionListener
- { public void actionPerformed(ActionEvent e)
- { String s=in.getText();
- double d=Double.parseDouble(s);
- double sq=d*d;
- out.setText(d+"的平方是:"+sq);
- } }}


Java位運算:
- public class BitwiseOperators {
- public static void main(String args[]){
- int a=0b1100;
- int b=0b1010;
- print("a ",a);
- print("b ",b);
- print("a&b ",a&b);
- print("a|b ",a|b);
- print("a^b ",a^b);
- print("~a ",~a);
- print("a<<2 ",a<<2);
- print("a>>2 ",a>>2);
- print("a>>>2 ",a>>>2);
- } static void print(String prefix,int n){
- String s=Integer.toBinaryString(n);
- while(s.length()<4)s="0"+s;
- System.out.print(prefix+" "+s+"\n");
- }}

同心圓:
- import java.awt.*;
- import javax.swing.*;
- public class Circle99Frame extends JFrame {
- public static void main(String args[])
- {
- JFrame frame=new Circle99Frame();
- frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); frame.setSize(600,600);
- frame.setVisible(true);
- } public void paint( Graphics g)
- {
- g.drawString("circle 99",20,20);
- int x0=getSize().width/2;
- int y0=getSize().height/2;
- for(int r=0;r<getSize().height/2;r+=10)
- { g.setColor(getRandomColor()); g.drawOval(x0-r,y0-r,r*2,r*2);
- } } Color getRandomColor()
- {
- return new Color(
- (int)(Math.random()*255),//random本身只產生(0~1)之間的小數,
- (int)(Math.random()*255),
- (int)(Math.random()*255)
- );
- }
- }
下面呢是一個常見的簡陋的登陸界面,這個程序是這個兩個類class共同組成的程序,先看代碼:
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class DemoFrame extends JFrame{
- public DemoFrame(DemoPanel panel)
- {
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(300, 200);
- this.setTitle("Frame Demo");
- this.add(panel);
- this.setResizable(false);
- this.setVisible(true);
- } public static void main(String[] args)
- {
- DemoPanel panel = new DemoPanel();
- DemoFrame Frame = new DemoFrame(panel);
- } }
- import java.awt.GridLayout;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- public class DemoPanel extends JPanel{
- private JLabel labelUser, labelPassWd; //標簽 用戶名,密碼
- private JButton buttonLogin, buttonReset; //按鈕 登錄,重置
- private JTextField textFieldUserName; //文本框 用戶名輸入
- private JPasswordField passWdField; //密碼框 密碼輸入
- private JPanel panelUserName;
- private JPanel panelPassWd;
- private JPanel panelLoginButton;
- public DemoPanel(){
- this.labelUser = new JLabel("用戶名");
- this.labelPassWd = new JLabel("密 碼");
- this.buttonLogin = new JButton("登錄");
- this.buttonReset = new JButton("重置");
- this.textFieldUserName = new JTextField(10);
- this.passWdField = new JPasswordField(10);
- this.panelPassWd = new JPanel();
- this.panelUserName = new JPanel();
- this.panelLoginButton = new JPanel();
- this.setLayout(new GridLayout(3, 1)); //網格式布局
- this.panelUserName.add(this.labelUser);
- this.panelUserName.add(this.textFieldUserName);
- this.panelPassWd.add(this.labelPassWd);
- this.panelPassWd.add(this.passWdField);
- this.panelLoginButton.add(buttonLogin);
- this.panelLoginButton.add(buttonReset);
- this.add(this.panelUserName);
- this.add(this.panelPassWd);
- this.add(this.panelLoginButton);
- }
- }
程序結果如下 :

簡單的加法器:
- package TEST; import javax.swing.JOptionPane; //導入類
- public class TEST
- {
- public static void main(String args[])
- {
- String input_pane1,input_pane2;
- int n1,n2,sum; input_pane1 = JOptionPane.showInputDialog("Please input the first number"); //輸入框1
- input_pane2 = JOptionPane.showInputDialog("Please input the second number"); //輸入框2
- n1 = Integer.parseInt(input_pane1); //獲取輸入框中輸入數據的整數類型
- n2 = Integer.parseInt(input_pane2);//獲取輸入框中輸入數據的整數類型
- sum = n1+n2; JOptionPane.showMessageDialog(null, "The sum is: "+sum,"Adding Device",JOptionPane.PLAIN_MESSAGE);
- //第1個參數:null 顯示在中央
- //第2個參數:要顯示的字符
- //第3個參數:標題欄信息
- //第4個參數:對話框類型
- System.exit(0); //終結圖形用戶界面程序必須的
- }
- }
結果如下:



說到這里,我其實有些感觸,記得上學期,我們做課程設計,當時一個同學的題目是寫一個帶界面的大數乘除運算器,關于大數乘除的方法,我有時間再總結一下,但是這個界面當時同學其實是不會的,但是現在看,如果單純實現界面還是比較簡單的,首先看我修改的第一個拙劣的界面版本模板:

這樣其實就好了很多,起碼可以看到加數是哪些了,代碼很簡單,只需要在輸出那行添加上n1和n2的信息就可以了。
- JOptionPane.showMessageDialog(null, n1+"+"+n2+" The sum is: "+sum,"Adding Device",JOptionPane.PLAIN_MESSAGE);