java合集

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("請輸入一個數(shù)");

? ? ? ? int a=scanner.nextInt();

? ? ? ? System.out.printf("%d的平方是%d\n",a,a*a);

? ? }

}

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

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("用于顯示平方結(jié)果的標簽");

? ? 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本身只產(chǎn)生(0~1)之間的小數(shù),

? ? ? ? ? ? ? ? (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));? //網(wǎng)格式布局

? ? ? ? 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);

? ? }

}

程序結(jié)果如下 :

簡單的加法器:

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); //獲取輸入框中輸入數(shù)據(jù)的整數(shù)類型

? ? ? ? n2 = Integer.parseInt(input_pane2);//獲取輸入框中輸入數(shù)據(jù)的整數(shù)類型

? ? ? ? sum = n1+n2;

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

? ? ? ? //第1個參數(shù):null 顯示在中央

? ? ? ? //第2個參數(shù):要顯示的字符

? ? ? ? //第3個參數(shù):標題欄信息

? ? ? ? //第4個參數(shù):對話框類型

? ? ? ? System.exit(0);? //終結(jié)圖形用戶界面程序必須的

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容