2016年5月18日 星期三

Java Programming Tutorial

Java Programming Tutorial
https://www.youtube.com/watch?v=Hl-zzrqQoSE&list=PLFE2CE09D83EE3E28

All my videos - https://thenewboston.com/videos.php
Support my tutorials - https://www.patreon.com/thenewboston
Forum - https://thenewboston.com/forum/

Bucky Roberts - https://thenewboston.com/profile.php?...
Facebook - https://www.facebook.com/TheNewBoston...
GitHub - https://github.com/buckyroberts
Google+ - https://plus.google.com/+BuckyRoberts
LinkedIn - https://www.linkedin.com/in/bucky-rob...
Reddit - https://www.reddit.com/r/thenewboston/
Twitter - https://twitter.com/bucky_roberts

**

5 - Variables

class  apples {
public static void main (String args[]){
double tuna = 5.28;

System.out.print("I want ");
System.out.print(tuna);
System.out.println(" movies");
System.out.print("apples");
}
}
**

6 - Getting User Input

import java.util.Scanner;

class apples{
public static void main(String args[]){
Scanner james = new Scanner(System.in);
System.out.println(james.nextLine());
}
}
**

7 - Building a Basic Calculator

import java.util.Scanner;

class apples{
public static void main(String args[]){
Scanner james = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first num: ");
fnum = james.nextDouble();
System.out.println("Enter second num: ");
snum = james.nextDouble();
answer =  fnum + snum;
System.out.println(answer);


}
}
**

14 - Using Multiple Classes


class apples{
public static void main(String args[]){
tuna tunaObject = new tuna();
tunaObject.simpleMessage();
}
}

tuna.java

public class tuna {
public void simpleMessage(){
System.out.println("This is another class");
}
}

**

15 - Use Methods with Parameters

import java.util.Scanner;

class apples{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
tuna tunaObject = new tuna();
System.out.println("Enter you name here: ");
String name = input.nextLine();
tunaObject.simpleMessage(name);
}
}

tuna.java

public class tuna {
public void simpleMessage(String name){
System.out.println("This is my name");
System.out.println(name);
}
}

**

16 - Many Methods and Instances

import java.util.Scanner;

class apples{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
tuna tunaObject = new tuna();
System.out.println("Enter name of first name here: ");
String temp = input.nextLine();
tunaObject.setName(temp);
tunaObject.saying();
}
}
tuna.java

public class tuna {
private String girlName;

public void setName(String name){
girlName = name;
}

public String getName(){
return girlName;
}

public void saying(){
System.out.printf("your first gf was %s", getName());
}
}


**

17 - Constructors

class apples{
public static void main(String args[]){

tuna tunaObject = new tuna("Alien");
tuna tunaObject2 = new tuna("Nicole");
tunaObject.saying();
tunaObject2.saying();

}
}

tuna.java

public class tuna {
private String girlName;

public tuna(String name){
girlName = name;
}

public void setName(String name){
girlName = name;
}

public String getName(){
return girlName;
}

public void saying(){
System.out.printf("your first gf was %s", getName());
}
}

**

20 - Conditional Operators


class apples{
public static void main(String args[]){
int age = 21;
System.out.println(age > 50 ? "You are old" : "You are young");
}
}
**

23 - Compound Interest Program

class apples{
public static void main(String args[]){
double amount;
double principal = 10000;
double rate = .01;

for (int day = 1 ; day < 20 ; day++){
amount = principal*Math.pow(1+rate, day);
System.out.println(day + "  " + amount);
}
}
}

**

24 - do while Loops

class apples{
public static void main(String args[]){
int counter =15 ;

do{
System.out.println(counter);
counter++;
}while(counter <= 10);
}
}

**

25 - Math Class Methods


class apples{
public static void main(String args[]){
System.out.println(Math.abs(-26.7));
System.out.println(Math.ceil(7.4));
System.out.println(Math.floor(7.4));
System.out.println(Math.max(1,5));
System.out.println(Math.min(1,5));
System.out.println(Math.pow(5,3));
System.out.println(Math.sqrt(9));

}
}
**

26 - Random Number Generator

import java.util.*;

class apples{
public static void main(String args[]){
Random dice = new Random();
int number;

for (int counter = 1 ; counter <=10 ; counter ++){
number = 1 + dice.nextInt(6);
System.out.println(number + " ");
}

}
}

**

27 - Introduction to Arrays

class apples{
public static void main(String args[]){
int bucky[] = new int[10];

bucky[0] = 87;
bucky[1] = 5;

System.out.println(bucky[0]);
}
}
**

 28 - Creating an Array Table

class apples{
public static void main(String args[]){
System.out.println("Index\tValue");
int bucky[]={32,12,18,54,2};

for (int counter = 0 ; counter <bucky.length;counter++){
System.out.println(counter + "\t" + bucky[counter]);
}
}
}

**

29 - Summing Elements of Arrays

class apples{
public static void main(String args[]){
System.out.println("Index\tValue\tSum");
int bucky[]={32,12,18,54,2};
int sum=0;
for (int counter = 0 ; counter <bucky.length;counter++){
sum+=bucky[counter];
System.out.println(counter + "\t" + bucky[counter] +"\t" + sum);
}
}
}

**

30 - Array Elements as Counters


import java.util.Random;

class apples{
public static void main(String args[]){
Random rand = new Random();
int freq[] = new int [7];

for(int roll=1;roll<1000;roll++){
++freq[1+rand.nextInt(6)];//1~6
}
System.out.println("Face\tFrequency");

for(int face=0;face<freq.length;face++){
System.out.println(face+"\t"+freq[face]);
}
}
}
**

31 - Enhanced for Loop


import java.util.Random;

class apples{
public static void main(String args[]){
int james[]={3,4,5,6,7};
int total=0;

for(int x:james ){
total+=x;
System.out.println(total);
}

System.out.println(total);
}
}
**

32 - Arrays in Methods


import java.util.Random;

class apples{
public static void main(String args[]){
int james[]={3,4,5,6,7};
change(james);

for(int y:james){
System.out.println(y);
}
}

public static void change(int x[]){
for(int counter=0;counter<x.length;counter++){
x[counter]+=5;
}
}
}
**

33 - Multidimensional Arrays

34 - Table for Multi Arrays

import java.util.Random;

class apples{
public static void main(String args[]){
int firstarray[][]={{8,9,10,11},{12,13,14,15}};
int secondarray[][]={{30,31,32,33},{43},{4,5,6}};

System.out.println("This is the first array");
display(firstarray);

System.out.println("This is the second array");
display(secondarray);
}

public static void display(int x[][]){
for(int row=0;row<x.length;row++){
for(int column = 0;column<x[row].length;column++){
System.out.print(x[row][column]+"\t");
}
System.out.println();
}
}
}
**

35 - Variable Length Arguments


class apples{
public static void main(String args[]){
System.out.println(average(43,56,78,6,41,55,2));
}

public static int average(int...numbers){
int total=0;
for(int x:numbers){
total+=x;
}
return total/numbers.length;
}
}
**

36 - Time Class

class apples{
public static void main(String args[]){
tuna tunaObject = new tuna();
System.out.println(tunaObject.toMilitary());
tunaObject.setTime(13, 27, 6);
System.out.println(tunaObject.toMilitary());
}
}
**

37 - Display Regular time


class apples{
public static void main(String args[]){
tuna tunaObject = new tuna();

System.out.println(tunaObject.toMilitary());
System.out.println(tunaObject.toString());

tunaObject.setTime(13, 27, 6);

System.out.println(tunaObject.toMilitary());
System.out.println(tunaObject.toString());

}
}

tuna.class

public class tuna {
private int hour;
private int minute;
private int second;

public void setTime(int h, int m, int s){
hour = ((h>=0 && h<24) ? h : 0);
minute = ((m>=0 && m<60) ? m : 0);
second = ((s>=0 && s<60) ? s : 0);
}

public String toMilitary(){
return String.format("%02d:%02d:%02d",hour,minute,second);
}

public String toString(){
return String.format("%d:%02d:%02d %s", (((hour==0)||hour==12) ? 12 : hour%12),minute,second, (hour<12 ? "AM" : "PM"));
}
}

**

38 - Public, Private and this


class apples{
public static void main(String args[]){
tuna tunaObject = new tuna();

System.out.println(tunaObject.toMilitary());
System.out.println(tunaObject.toString());

tunaObject.setTime(13, 27, 6);

System.out.println(tunaObject.toMilitary());
System.out.println(tunaObject.toString());

}
}

tuna.java

public class tuna {
private int hour=1;
private int minute=2;
private int second=3;

public void setTime(int h, int m, int s){
this.hour = ((h>=0 && h<24) ? h : 0);
this.minute = ((m>=0 && m<60) ? m : 0);
this.second = ((s>=0 && s<60) ? s : 0);
}

public String toMilitary(){
return String.format("%02d:%02d:%02d",hour,minute,second);
}

public String toString(){
return String.format("%d:%02d:%02d %s", (((hour==0)||hour==12) ? 12 : hour%12),minute,second, (hour<12 ? "AM" : "PM"));
}
}

**

39 - Multiple Constructors


public class tuna {
private int hour;
private int minute;
private int second;

public tuna(){
this(0,0,0);
}

public tuna(int h){
this(h,0,0);
}

public tuna(int h, int m){
this(h,m,0);
}

public tuna(int h, int m, int s){
setTime(h,m,s);
}

private void setTime(int h, int m, int s) {
setHour(h);
setMinute(m);
setSecond(s);

}
}
**

40 - Set and Get Methods


public class tuna {
private int hour;
private int minute;
private int second;

public tuna(){
this(0,0,0);
}

public tuna(int h){
this(h,0,0);
}

public tuna(int h, int m){
this(h,m,0);
}

public tuna(int h, int m, int s){
setTime(h,m,s);
}

private void setTime(int h, int m, int s) {
setHour(h);
setMinute(m);
setSecond(s);
}

private void setSecond(int s) {
second = ((s>=0&&s<60) ? s:0  );

}

private void setMinute(int m) {
minute = ((m>=0&&m<60) ? m:0  );

}

private void setHour(int h) {
hour = ((h>=0&&h<24) ? h:0  );

}
public int getHour(){
return hour;
}
public int getMinute(){
return minute;
}
public int getSecond(){
return second;
}
public String toMilitary(){
return String.format("%02d:%02d:%02d",getHour(),getMinute(),getSecond())
}
}
**

41 - Building Objects for Constructors

class apples{
public static void main(String args[]){
tuna tunaObject = new tuna();
tuna tunaObject2 = new tuna(5);
tuna tunaObject3 = new tuna(5,13);
tuna tunaObject4 = new tuna(5,13,43);



System.out.printf("%s\n",tunaObject.toMilitary());
System.out.printf("%s\n",tunaObject2.toMilitary());
System.out.printf("%s\n",tunaObject3.toMilitary());
System.out.printf("%s\n",tunaObject4.toMilitary());


}
}

**

42 - toString

class apples{
public static void main(String args[]){
potpie potObject = new potpie(4,5,6);
}
}

potpie.java
public class potpie {
private int month;
private int day;
private int year;

public potpie(int m, int d, int y){
month = m;
day = d;
year = y;

System.out.printf("The contrtuctor for this is %s \n",this);
}

public String toString(){
return String.format("%d/%d/%d", month,day,year);
}
}


**

43 - Composition

class apples{
public static void main(String args[]){
potpie potObject = new potpie(4,5,6);
tuna tunaObject = new tuna("Bucky",potObject);

System.out.println(tunaObject);
}
}

tuna.java

public class tuna {
private String name;
private potpie birthday;

public tuna(String theName, potpie theDate){
name=theName;
birthday=theDate;
}

public String toString(){
return String.format("My name is %s, my birthday is %s", name,birthday);
}
}


**

44 - Enumeration

class apples{
public static void main(String args[]){
for(tuna people:tuna.values())
System.out.printf("%s\t%s\t%s\n",people, people.getDesc(), people.getYear());

}
}

tuna.java

public enum tuna {
bucky("nice","22"),
kelsey("cuite","10"),
julia("bigmistake","12");

private final String desc;
private final String year;

tuna(String description, String birthday){
desc = description;
year = birthday;
}

public String getDesc(){
return desc;
}
public String getYear(){
return year;
}
}

**

45 - EnumSet range


import java.util.EnumSet;
class apples{
public static void main(String args[]){
for(tuna people:tuna.values())
System.out.printf("%s\t%s\t%s\n",people, people.getDesc(), people.getYear());

System.out.println("\nAnd now for the range of constants\n");

for(tuna people: EnumSet.range(tuna.kelsey, tuna.candy)){
System.out.printf("%s\t%s\t%s\n",people, people.getDesc(), people.getYear());
}
}
}

tuna.java

public enum tuna {
bucky("nice","22"),
kelsey("cuite","10"),
julia("bigmistake","12"),
nicole("italian","13"),
candy("different","14"),
erin("iwish","16");


private final String desc;
private final String year;

tuna(String description, String birthday){
desc = description;
year = birthday;
}

public String getDesc(){
return desc;
}
public String getYear(){
return year;
}
}

**

46 - Static

import java.util.EnumSet;
class apples{
public static void main(String args[]){
tuna member1 = new tuna("Megan","Fox");
tuna member2 = new tuna("Natalie","portman");
tuna member3 = new tuna("Taylor","Swift");


}
}

tuna.java

public class tuna {
private String first;
private String last;
private static int member = 0;

public tuna(String fn, String ln){
first = fn;
last = ln;
member++;

System.out.printf("Contrusctor for %s %s, members in the club: %d\n",first, last, member);
}
}

**

47 - More on Static

import java.util.EnumSet;
class apples{
public static void main(String args[]){
tuna member1 = new tuna("Megan","Fox");
tuna member2 = new tuna("Natalie","portman");
tuna member3 = new tuna("Taylor","Swift");

System.out.println();
System.out.println(member1.getFirst());
System.out.println(member1.getLast());
System.out.println(member1.getMembers());
System.out.println();
System.out.println(member2.getFirst());
System.out.println(member2.getLast());
System.out.println(member2.getMembers());
System.out.println();
System.out.println(tuna.getMembers());
}
}

tuna.java

public class tuna {
private String first;
private String last;
private static int member = 0;

public tuna(String fn, String ln){
first = fn;
last = ln;
member++;

System.out.printf("Contrusctor for %s %s, members in the club: %d\n",first, last, member);
}

public String getFirst(){
return first;
}
public String getLast(){
return last;
}

public static int getMembers(){
return member;
}
}

//static 用class.method就好,不用各自宣告


**

48 - final

class apples{
public static void main(String args[]){
tuna tunaObject = new tuna(10);

for(int i=0;i<5;i++){
tunaObject.add();
System.out.printf("%s",tunaObject);
}

}
}

tuna.java

public class tuna {
private int sum;
private final int NUMBER;

public tuna(int x){
NUMBER = x;
}

public void add(){
sum += NUMBER;
}

public String toString(){
return String.format("sum = %d\n", sum);
}
}

**

49 - Inheritance

class apples{
public static void main(String args[]){
tuna tunaObject = new tuna();
potpie potObject = new potpie();

tunaObject.eat();
potObject.eat();
}
}

tuna.java

public class tuna extends food {
public void eat(){
System.out.println("I am the new method of tuna");
}
}

potpie.java

public class potpie extends food {
}

food.java

public class food {
public void eat(){
System.out.println("I am the eat methods");
}
}


**

50 - Graphical User Interface GUI

import javax.swing.JOptionPane;

class apples{
public static void main(String args[]){
String fn = JOptionPane.showInputDialog("Enter first number");
String sn = JOptionPane.showInputDialog("Enter second number");

int num1 = Integer.parseInt(fn);
int num2 = Integer.parseInt(sn);

int sum = num1 + num2;

JOptionPane.showMessageDialog(null, "The answer is "+sum,"the title",JOptionPane.PLAIN_MESSAGE);
}
}

**

51 - GUI with JFrame

import javax.swing.JFrame;

class apples{
public static void main(String args[]){

tuna james = new tuna();
james.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
james.setSize(275,180);
james.setVisible(true);
}
}

tuna.java
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class tuna extends JFrame{
private JLabel item1;

public tuna(){
super("The title bar");
setLayout(new FlowLayout());

item1 = new JLabel("This is a sentecew");
item1.setToolTipText("This is gonna show ip on hover");
add(item1);
}

}

**

52 - Event Handling

import javax.swing.JFrame;

class apples{
public static void main(String args[]){

tuna james = new tuna();
james.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
james.setSize(275,180);
james.setVisible(true);
}
}

tuna.java
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class tuna extends JFrame{

private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;

public tuna(){
super("The title");
setLayout(new FlowLayout());

item1 = new JTextField(10);
add(item1);

item2 = new JTextField("Enter text here");
add(item2);

item3 = new JTextField("uneditable",20);
item3.setEditable(false);
add(item3);

passwordField = new JPasswordField("mypassword");
add(passwordField);
}
}

**

 53 - ActionListner


tuna.java
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class tuna extends JFrame{

private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;

public tuna(){
super("The title");
setLayout(new FlowLayout());

item1 = new JTextField(10);
add(item1);

item2 = new JTextField("Enter text here");
add(item2);

item3 = new JTextField("uneditable",20);
item3.setEditable(false);
add(item3);

passwordField = new JPasswordField("mypassword");
add(passwordField);

thehandler handler = new thehandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
passwordField.addActionListener(handler);

}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string ="";

if(event.getSource()==item1)
string=String.format("filed 1: %s", event.getActionCommand());
else if(event.getSource()==item2)
string=String.format("filed 2: %s", event.getActionCommand());
else if(event.getSource()==item3)
string=String.format("filed 3: %s", event.getActionCommand());
else if(event.getSource()==passwordField)
string=String.format("password field is : %s", event.getActionCommand());

}
}
}

**

54 - Event Handler Program


tuna.java
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class tuna extends JFrame{

private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;

public tuna(){
super("The title");
setLayout(new FlowLayout());

item1 = new JTextField(10);
add(item1);

item2 = new JTextField("Enter text here");
add(item2);

item3 = new JTextField("uneditable",20);
item3.setEditable(false);
add(item3);

passwordField = new JPasswordField("mypassword");
add(passwordField);

thehandler handler = new thehandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
passwordField.addActionListener(handler);

}
private class thehandler implements ActionListener{

public void actionPerformed(ActionEvent event){

String string ="";

if(event.getSource()==item1)
string=String.format("filed 1: %s", event.getActionCommand());
else if(event.getSource()==item2)
string=String.format("filed 2: %s", event.getActionCommand());
else if(event.getSource()==item3)
string=String.format("filed 3: %s", event.getActionCommand());
else if(event.getSource()==passwordField)
string=String.format("password field is : %s", event.getActionCommand());

JOptionPane.showMessageDialog(null, string);
}
}
}


**

55 - Intoduction to Polymorphism


import javax.swing.JFrame;

class apples{
public static void main(String args[]){

food james[]=new food[2];

james[0]=new potpie();
james[1]=new tuna();

for(int x=0 ; x<2 ; ++x){
james[x].eat();
}
}
}



public class tuna extends food{
void eat(){
System.out.println("This tuna is great.");
}
}



public class potpie extends food {
void eat(){
System.out.println("This potpie is great.");
}
}


public class food {
void eat(){
System.out.println("This food is great.");
}
}

**

56 - Polymorphic Arguements

import javax.swing.JFrame;

class apples{
public static void main(String args[]){

fatty james=new fatty();
food fo = new food();
food po = new potpie();

james.digest(fo);
james.digest(po);

}
}


public class potpie extends food {

void eat(){
System.out.println("This potpie is great.");
}
}



public class tuna extends food{
void eat(){
System.out.println("This tuna is great.");
}
}


public class food {
void eat(){
System.out.println("This food is great.");
}
}


public class fatty {

public void digest(food x){
x.eat();
}
}

**

 57 - Overriding Rules


**

69 - Drop Down List Program

import javax.swing.JFrame;
class apples{
public static void main(String args[]){

Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;


public class Gui extends JFrame {

private JComboBox box;
private JLabel picture;

private static String[] filename = {"g.png","r.png"};
private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])),new ImageIcon(getClass().getResource(filename[1]))};

public Gui(){
super("The title");
setLayout(new FlowLayout());

box = new JComboBox(filename);

box.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
if(event.getStateChange()==ItemEvent.SELECTED)
picture.setIcon(pics[box.getSelectedIndex()]);
}
}
);

add(box);
picture = new JLabel(pics[0]);
add(picture);
}

}

**

74 - Mouse Events

import javax.swing.JFrame;
class apples{
public static void main(String args[]){

Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Gui extends JFrame {
private JPanel mousepanel;
private JLabel statusbar;

public Gui(){
super("Title");

mousepanel = new JPanel();
mousepanel.setBackground(Color.WHITE);
add(mousepanel, BorderLayout.CENTER);

statusbar = new JLabel("default");
add(statusbar, BorderLayout.SOUTH);
}
}

**

76 - MouseMotionListener interface

import javax.swing.JFrame;
class apples{
public static void main(String args[]){

Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Gui extends JFrame {
private JPanel mousepanel;
private JLabel statusbar;

public Gui(){
super("Title");

mousepanel = new JPanel();
mousepanel.setBackground(Color.WHITE);
add(mousepanel, BorderLayout.CENTER);

statusbar = new JLabel("default");
add(statusbar, BorderLayout.SOUTH);

Handlerclass handler = new Handlerclass();
mousepanel.addMouseListener(handler);
mousepanel.addMouseMotionListener(handler);
}

private class Handlerclass implements MouseListener, MouseMotionListener{
public void mouseClicked(MouseEvent event){
statusbar.setText(String.format("Clicked at %d %d", event.getX(),event.getY()));

}
public void mousePressed(MouseEvent event){
statusbar.setText("YOU PRESSED DOWN THE MOUSE");
}
public void mouseReleased(MouseEvent event){
statusbar.setText("YOU RELEASED THE BUTTON");
}
public void mouseEntered(MouseEvent event){
statusbar.setText("you entered the area");
mousepanel.setBackground(Color.RED);
}
public void mouseExited(MouseEvent event){
statusbar.setText("the mouse has left the window");
mousepanel.setBackground(Color.ORANGE);
}
//these are mouse motion event
public void mouseDragged(MouseEvent event){
statusbar.setText("you are dragging the mouse");
}
public void mouseMoved(MouseEvent event){
statusbar.setText("you are moved the mouse");
}
}
}

**

77 - Adapter Classes

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Gui extends JFrame {
private String details;
private JLabel statusbar;

public Gui(){
super("title");

statusbar = new JLabel("This is default");
add(statusbar, BorderLayout.SOUTH);
addMouseListener(new Mouseclass());
}

private class Mouseclass extends MouseAdapter{
public void mouseClicked(MouseEvent event){
details = String.format("You clicked %d", event.getClickCount());

if(event.isMetaDown())
details += "with right mouse button";
else if (event.isAltDown())
details += "with center mouse button";
else
details += "with left mouse button";

statusbar.setText(details);

}
}
}

**

78 - File Class

import java.io.File;

class apples{
public static void main(String args[]){
File x = new File("C:\\test\\james.txt");

if(x.exists())
System.out.println(x.getName() + " exist!");
else
System.out.println("This thing does not exist!");
}
}
**

79 - Creating Files

import java.io.File;
import java.util.*;

class apples{
public static void main(String args[]){
final Formatter x;

try{
x = new Formatter("fred.txt");
System.out.println("You created a file");
}catch(Exception e){
System.out.println("You got an error");
}
}
}
**

80 - Writing to Files

class apples{
public static void main(String args[]){

createfile g = new createfile();
g.openFile();
g.addRecords();
g.closeFile();
}
}
import java.io.*;
import java.lang.*;
import java.util.*;

public class createfile {

private Formatter x;
public void openFile(){
try{
x = new Formatter("chinese.txt");
}catch(Exception e){
System.out.println("you have an error");
}
}

public void addRecords(){
x.format("%s%s%s", "20 ","Bucky ", "roberts");
}

public void closeFile(){
x.close();
}
}

**

81 - Reading from Files

class apples{
public static void main(String args[]){

readfile r = new readfile();
r.openFile();
r.readFile();
r.closeFile();

}

}
import java.io.*;
import java.lang.*;
import java.util.*;

public class readfile {
private Scanner x;

public void openFile(){
try{
x = new Scanner(new File("chinese.txt"));
}catch(Exception e){
System.out.println("could not find file");
}
}

public void readFile(){
while(x.hasNext()){
String a = x.next();
String b = x.next();
String c = x.next();

System.out.printf("%s %s %s\n",a,b,c);
}
}

public void closeFile(){
x.close();
}
}

**

82 - Exception Handling

import java.util.*;

public class sample {
public static void main(String[] args){
Scanner input = new Scanner(System.in);

try{
System.out.println("Enter first num: ");
int n1 = input.nextInt();
System.out.println("Enter second num: ");
int n2 = input.nextInt();
int sum = n1/n2;
System.out.println(sum);
}catch(Exception e){
System.out.println("You enter a wrong number");
}
}

}

**

83 - FlowLayout

import javax.swing.*;

public class bucky {
public static void main(String[] args){

Layout l = new Layout();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setSize(300,100);
l.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Layout extends JFrame{
private JButton lb;
private JButton cb;
private JButton rb;
private FlowLayout layout;
private Container container;
public Layout(){
super ("TITLE");
layout = new FlowLayout();
container = getContentPane();
setLayout(layout);
//left stuff in here
lb = new JButton("left");
add(lb);
lb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(container);
}
}
);
//center stuff in here
cb = new JButton("center");
add(cb);
cb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(container);
}
}
);
//left stuff in here
rb = new JButton("right");
add(rb);
rb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.RIGHT);
layout.layoutContainer(container);
}
}
);
}
}

**

84 - Drawing Graphics

import javax.swing.*;

public class bucky {
public static void main(String[] args){

JFrame f = new JFrame("Dell Audio");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Peach p = new Peach();
f.add(p);
f.setSize(400,250);
f.setVisible(true);
}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Peach extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.fillRect(25, 25, 100, 30);
g.setColor(new Color(74,65,42));
g.fillRect(25, 65, 100, 30);
g.setColor(Color.RED);
g.drawString("Text here.", 25, 120);
}
}

**

85 - JColorChooser

import javax.swing.*;

public class bucky {
public static void main(String[] args){
Peach p = new Peach();
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Peach extends JFrame{
private JButton b;
private Color color = (Color.WHITE);
private JPanel panel;
public Peach(){
super("The title");
panel = new JPanel();
panel.setBackground(color);
b = new JButton("Choose a color");
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
color = JColorChooser.showDialog(null, "Pick your color", color);
if(color == null)
color = (Color.WHITE);
panel.setBackground(color);
}
}
);
add(panel, BorderLayout.CENTER);
add(b, BorderLayout.SOUTH);
setSize(425,150);
setVisible(true);
}
}

**

86 - Drawing More Stuff

import java.awt.Color;

import javax.swing.*;

public class bucky {
public static void main(String[] args){
JFrame f = new JFrame("Tittle");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Peach p = new Peach();
p.setBackground(Color.WHITE);
f.add(p);
f.setSize(500,270);
f.setVisible();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Peach extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.drawLine(10, 25, 200, 45);
g.setColor(Color.CYAN);
g.drawRect(10, 55, 100, 30);
g.setColor(Color.GRAY);
g.fillOval(10, 95, 100, 30);
g.setColor(Color.MAGENTA);
g.fill3DRect(10, 160, 100, 50, false);
}
}

**

87 - Series Finale


**

**

**

**

**

**

**

沒有留言:

張貼留言