自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

初學(xué)者學(xué)習(xí)Java代碼

開發(fā) 后端
其實(shí)我學(xué)習(xí)java最根本的原因是:我是一個(gè)挺關(guān)注外在的人,雖然是個(gè)程序員,所以我很喜歡寫出那些帶有漂亮的界面的程序,因?yàn)镃總是控制臺(tái),我不是很喜歡,在這份java代碼合集中,我會(huì)記錄自己學(xué)習(xí)Java界面化編程的點(diǎn)點(diǎn)滴滴。

其實(shí)我學(xué)習(xí)java最根本的原因是:我是一個(gè)挺關(guān)注外在的人,雖然是個(gè)程序員,所以我很喜歡寫出那些帶有漂亮的界面的程序,因?yàn)镃總是控制臺(tái),我不是很喜歡,在這份java代碼合集中,我會(huì)記錄自己學(xué)習(xí)Java界面化編程的點(diǎn)點(diǎn)滴滴。

更新:因?yàn)镃/C++是我主要使用語(yǔ)言,所有后來(lái)寫界面主要用Qt寫了,但我java也會(huì)繼續(xù)學(xué)的。我只是給想學(xué)界面gui的同志一個(gè)思路??梢詤⒖歼@篇文章Qt5 計(jì)算器的實(shí)現(xiàn)

可能會(huì)有java初學(xué)者,,我也是,說明,java是一個(gè)工程里可以有很多java類class,每一個(gè)類class都可以單獨(dú)運(yùn)行,不像C語(yǔ)言里只能有一個(gè)main()函數(shù)可以運(yùn)行,這是我的代碼合集程序結(jié)構(gòu):

 

初學(xué)者學(xué)習(xí)Java代碼

helloworld:

  1. class Javahelloworld { 
  2.  public static void main(String args[]){ 
  3.         System.out.println("hello world\n"); 
  4.     }} 

基本輸入輸出:

  1. import java.util.Scanner; 
  2. public class ScannerTest { 
  3.  public static void main(String[] args){ 
  4.         Scanner scanner=new Scanner(System.in); 
  5.         System.out.print("請(qǐng)輸入一個(gè)數(shù)"); 
  6.  int a=scanner.nextInt(); 
  7.         System.out.printf("%d的平方是%d\n",a,a*a); 
  8.     }} 

Java圖形化界面求數(shù)的平方:

  1. import java.awt.*;import java.awt.event.*; 
  2. import javax.swing.*;/**包含JFrame*/ 
  3. public class AppGraphInOut { 
  4.  public static void main(String args[]){ 
  5.  new AppFrame(); 
  6.     }} class AppFrame extends JFrame 
  7. {    JTextField in=new JTextField(10); 
  8.     JButton btn=new JButton("求平方"); 
  9.     JLabel out=new JLabel("用于顯示平方結(jié)果的標(biāo)簽"); 
  10.  public AppFrame() 
  11.     {        setLayout(new FlowLayout()); 
  12.         getContentPane().add(in); 
  13.         getContentPane().add(btn); 
  14.         getContentPane().add(out); 
  15.         btn.addActionListener(new BtnActionAdapter()); 
  16.         setSize(400,100); 
  17.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);        setVisible(true); 
  18.     }  class BtnActionAdapter implements ActionListener 
  19.     { public void actionPerformed(ActionEvent e) 
  20.         {            String s=in.getText(); 
  21.  double d=Double.parseDouble(s); 
  22.  double sq=d*d; 
  23.             out.setText(d+"的平方是:"+sq); 
  24.         }    }} 

 

初學(xué)者學(xué)習(xí)Java代碼

 

初學(xué)者學(xué)習(xí)Java代碼

Java位運(yùn)算:

  1. public class BitwiseOperators { 
  2.  public static void main(String args[]){ 
  3.  int a=0b1100; 
  4.  int b=0b1010; 
  5.         print("a    ",a); 
  6.         print("b    ",b); 
  7.         print("a&b  ",a&b); 
  8.         print("a|b  ",a|b); 
  9.         print("a^b  ",a^b); 
  10.         print("~a   ",~a); 
  11.         print("a<<2 ",a<<2); 
  12.         print("a>>2 ",a>>2); 
  13.         print("a>>>2    ",a>>>2); 
  14.     } static void print(String prefix,int n){ 
  15.         String s=Integer.toBinaryString(n); 
  16.  while(s.length()<4)s="0"+s; 
  17.         System.out.print(prefix+" "+s+"\n"); 
  18.     }} 

 

初學(xué)者學(xué)習(xí)Java代碼

同心圓:

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3.  public class Circle99Frame extends JFrame { 
  4.  public static void main(String args[]) 
  5.     { 
  6.         JFrame frame=new Circle99Frame(); 
  7.         frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);        frame.setSize(600,600); 
  8.         frame.setVisible(true); 
  9.     } public void paint(  Graphics g) 
  10.     { 
  11.         g.drawString("circle 99",20,20); 
  12.  int x0=getSize().width/2; 
  13.  int y0=getSize().height/2; 
  14.  for(int r=0;r<getSize().height/2;r+=10) 
  15.         {            g.setColor(getRandomColor());            g.drawOval(x0-r,y0-r,r*2,r*2); 
  16.         }    } Color getRandomColor() 
  17.     { 
  18.  return new Color( 
  19.                 (int)(Math.random()*255),//random本身只產(chǎn)生(0~1)之間的小數(shù), 
  20.                 (int)(Math.random()*255), 
  21.                 (int)(Math.random()*255) 
  22.         ); 
  23.     } 

 

[[341314]]

下面呢是一個(gè)常見的簡(jiǎn)陋的登陸界面,這個(gè)程序是這個(gè)兩個(gè)類class共同組成的程序,先看代碼:

  1. import javax.swing.JFrame; 
  2.  import javax.swing.JPanel; 
  3.  public class DemoFrame extends JFrame{ 
  4.   public DemoFrame(DemoPanel panel) 
  5.   
  6.     { 
  7.   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  8.   this.setSize(300, 200); 
  9.   this.setTitle("Frame Demo"); 
  10.   this.add(panel); 
  11.   this.setResizable(false); 
  12.   this.setVisible(true); 
  13.      }    public static void main(String[] args) 
  14.   
  15.     { 
  16.          DemoPanel panel = new DemoPanel(); 
  17.          DemoFrame Frame = new DemoFrame(panel); 
  18.      } }   

 

  1. import java.awt.GridLayout; 
  2.    import javax.swing.JButton; 
  3.  import javax.swing.JLabel; 
  4.  import javax.swing.JPanel; 
  5.  import javax.swing.JPasswordField; 
  6.  import javax.swing.JTextField; 
  7.    public class DemoPanel extends JPanel{ 
  8.     private JLabel labelUser, labelPassWd;            //標(biāo)簽      用戶名,密碼 
  9.   
  10.  private JButton buttonLogin, buttonReset;         //按鈕      登錄,重置 
  11.   
  12.  private JTextField textFieldUserName;             //文本框  用戶名輸入 
  13.   
  14.  private JPasswordField passWdField;               //密碼框  密碼輸入 
  15.   
  16.  private JPanel panelUserName; 
  17.   
  18.  private JPanel panelPassWd; 
  19.   
  20.  private JPanel panelLoginButton; 
  21.   
  22.   
  23.   
  24.  public DemoPanel(){   
  25.  this.labelUser = new JLabel("用戶名");   
  26.  this.labelPassWd = new JLabel("密    碼");   
  27.  this.buttonLogin = new JButton("登錄");   
  28.  this.buttonReset = new JButton("重置");   
  29.  this.textFieldUserName = new JTextField(10);   
  30.  this.passWdField = new JPasswordField(10);   
  31.  this.panelPassWd = new JPanel();   
  32.  this.panelUserName = new JPanel();   
  33.  this.panelLoginButton = new JPanel();   
  34.   
  35.   
  36.  this.setLayout(new GridLayout(3, 1));  //網(wǎng)格式布局   
  37.   
  38.  this.panelUserName.add(this.labelUser);   
  39.  this.panelUserName.add(this.textFieldUserName);   
  40.  this.panelPassWd.add(this.labelPassWd);   
  41.  this.panelPassWd.add(this.passWdField);   
  42.  this.panelLoginButton.add(buttonLogin);   
  43.  this.panelLoginButton.add(buttonReset); 
      
  44.   
  45.  this.add(this.panelUserName);   
  46.  this.add(this.panelPassWd);   
  47.  this.add(this.panelLoginButton);   
  48.     }   

程序結(jié)果如下 :

 

初學(xué)者學(xué)習(xí)Java代碼

簡(jiǎn)單的加法器:

  1. package TEST;   import javax.swing.JOptionPane;  //導(dǎo)入類 
  2.    public class TEST 
  3.   
  4.   
  5.  public static void main(String args[]) 
  6.   
  7.     { 
  8.   
  9.         String input_pane1,input_pane2; 
  10.   int n1,n2,sum;         input_pane1 = JOptionPane.showInputDialog("Please input the first number");  //輸入框1 
  11.          input_pane2 = JOptionPane.showInputDialog("Please input the second number"); //輸入框2 
  12.          n1 = Integer.parseInt(input_pane1); //獲取輸入框中輸入數(shù)據(jù)的整數(shù)類型 
  13.          n2 = Integer.parseInt(input_pane2);//獲取輸入框中輸入數(shù)據(jù)的整數(shù)類型 
  14.          sum = n1+n2;         JOptionPane.showMessageDialog(null"The sum is: "+sum,"Adding Device",JOptionPane.PLAIN_MESSAGE); 
  15.   //第1個(gè)參數(shù):null 顯示在中央 
  16.   //第2個(gè)參數(shù):要顯示的字符 
  17.   
  18.  //第3個(gè)參數(shù):標(biāo)題欄信息 
  19.   
  20.  //第4個(gè)參數(shù):對(duì)話框類型 
  21.   
  22.         System.exit(0);  //終結(jié)圖形用戶界面程序必須的 
  23.   
  24.     } 
  25.   

結(jié)果如下:

 

初學(xué)者學(xué)習(xí)Java代碼

 

初學(xué)者學(xué)習(xí)Java代碼

 

初學(xué)者學(xué)習(xí)Java代碼

說到這里,我其實(shí)有些感觸,記得上學(xué)期,我們做課程設(shè)計(jì),當(dāng)時(shí)一個(gè)同學(xué)的題目是寫一個(gè)帶界面的大數(shù)乘除運(yùn)算器,關(guān)于大數(shù)乘除的方法,我有時(shí)間再總結(jié)一下,但是這個(gè)界面當(dāng)時(shí)同學(xué)其實(shí)是不會(huì)的,但是現(xiàn)在看,如果單純實(shí)現(xiàn)界面還是比較簡(jiǎn)單的,首先看我修改的第一個(gè)拙劣的界面版本模板:

 

初學(xué)者學(xué)習(xí)Java代碼

這樣其實(shí)就好了很多,起碼可以看到加數(shù)是哪些了,代碼很簡(jiǎn)單,只需要在輸出那行添加上n1和n2的信息就可以了。

  1. JOptionPane.showMessageDialog(null, n1+"+"+n2+" The sum is: "+sum,"Adding Device",JOptionPane.PLAIN_MESSAGE); 

 

責(zé)任編輯:未麗燕 來(lái)源: 今日頭條
相關(guān)推薦

2009-09-28 09:45:00

CCNA學(xué)習(xí)經(jīng)驗(yàn)CCNA

2015-07-20 13:56:59

SDN

2023-02-19 15:31:09

架構(gòu)軟件開發(fā)代碼

2020-08-04 08:42:10

Python開發(fā)工具

2009-09-28 10:34:43

NET初學(xué)者學(xué)習(xí)建議

2011-08-24 17:05:01

Lua

2009-07-08 09:32:40

ScalaScala與Java

2011-09-16 09:38:19

Emacs

2022-04-24 15:21:01

MarkdownHTML

2011-04-12 10:13:24

2011-07-04 14:14:54

java

2021-05-06 09:00:00

JavaScript靜態(tài)代碼開發(fā)

2009-06-12 15:16:53

Hibernate學(xué)習(xí)

2009-12-08 09:45:50

調(diào)用WCF

2011-07-26 17:55:16

iPhone Runtime

2010-01-15 19:05:42

學(xué)習(xí)C++

2024-12-25 08:00:00

機(jī)器學(xué)習(xí)ML管道人工智能

2021-03-14 18:22:23

套接字網(wǎng)絡(luò)通信

2010-06-13 11:13:38

UML初學(xué)者指南

2022-07-22 13:14:57

TypeScript指南
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)