初学者学习Java代码

其实我学习java最根本的原因是:我是一个挺关注外在的人,虽然是个程序员,所以我很喜欢写出那些带有漂
首页 新闻资讯 行业资讯 初学者学习Java代码

其实我学习java最根本的原因是:我是一个挺关注外在的人,虽然是个程序员,所以我很喜欢写出那些带有漂亮的界面的程序,因为C总是控制台,我不是很喜欢,在这份java代码合集中,我会记录自己学习Java界面化编程的点点滴滴。

更新:因为C/C++是我主要使用语言,所有后来写界面主要用Qt写了,但我java也会继续学的。我只是给想学界面gui的同志一个思路。可以参考这篇文章Qt5  计算器的实现

可能会有java初学者,,我也是,说明,java是一个工程里可以有很多java类class,每一个类class都可以单独运行,不像C语言里只能有一个main()函数可以运行,这是我的代码合集程序结构:

 

初学者学习Java代码

helloworld:

复制

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

  • 2.

  • 3.

  • 4.

基本输入输出:

复制

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);     }}
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

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);         }    }}
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

 

初学者学习Java代码

 

初学者学习Java代码

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");     }}
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

 

初学者学习Java代码

同心圆:

复制

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)         );     } }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

 

[[341314]]

下面呢是一个常见的简陋的登陆界面,这个程序是这个两个类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);      } }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

 

复制

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);       }   } 
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

程序结果如下 :

 

初学者学习Java代码

简单的加法器:

复制

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);  //终结图形用户界面程序必须的       }   }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

结果如下:

 

初学者学习Java代码

 

初学者学习Java代码

 

初学者学习Java代码

说到这里,我其实有些感触,记得上学期,我们做课程设计,当时一个同学的题目是写一个带界面的大数乘除运算器,关于大数乘除的方法,我有时间再总结一下,但是这个界面当时同学其实是不会的,但是现在看,如果单纯实现界面还是比较简单的,首先看我修改的第一个拙劣的界面版本模板:

 

初学者学习Java代码

这样其实就好了很多,起码可以看到加数是哪些了,代码很简单,只需要在输出那行添加上n1和n2的信息就可以了。

复制

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

 

17    2020-09-08 19:03:41    Java 代码 初学者