importimport java.awt.*. import javax.swing.JFrame.javax.swing.JFrame f = new javax.swing.JFrame("title").
JFrame and override paint(Graphics g) methodFont class and Color class, how to set/get font and colordrawLine, drawRect, fillRect, drawOval, fillOval, drawString methodsgetContentPane()setSize, setVisible, setLocation....BorderLayout, FlowLayout, GridLayoutnew GridLayout(0, 5), new GridLayout(5,0) mean?add(component), remove(index), add(component, index)JPanel(), JPanel(layout)getText, setTextsetBackground
public class XXX implements ActionListener {
.............
............
public void actionPerformed(ActionEvent e) {
// your codes
}
}
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// your codes
}
});
ComponentJButton button = (JButton)e.getSource();
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
// do something
} else if( e.getSource() == button2) {
// do something
} ......
.....
}
String) of the sourcebutton.setActionCommand(String s) set the action command of the button to s.
If you don't set it, the default value is the text on the button.
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if(action.equals(string1)) {
// do something
} else if (action.equals(string2)) {
// do something
} .....
.....
RuntimeException, Checked exceptionRuntimeExceptions are classes that extends RuntimeException.
someMethod(......) throws SomeException {
// your code
}
or
try {
// some codes
} catch (ExceptionX ex) {
// some codes
} catch (ExceptionY ex) {
// some codes
}
.....
RuntimeException. NullPointerException, ArraryIndexOutOfBoundsException, ArithmeticException, NumberFormatException are
all RuntimeExceptions. Understand when will these exceptions occur. FileNotFoundException, IOException are checked exceptions. You should know when these exceptions occur.
try {
line1
line2
line3
......
line10
} catch (ExceptionX ex) {
lineX
......
} catch (ExceptionY ex) {
lineY
.......
} finally {
......
}
line101
line102
If the first exception occurs at line3, then the program will find out which exception matches the exceptin thrown by line 3. The
codes in the catch block will be executed. After that, the codes in finally block will be executed. And then line 101.......
public class ExceptionTest {
public static void main(String[] args) {
try {
System.out.println("line 1");
String[] s = new String[10];
System.out.println("line 2");
s[0] = "Hi";
System.out.println("line 3");
System.out.println(s[11]);
System.out.println("line 4");
System.out.println(s[1].toUpperCase());
System.out.println("line 5");
System.out.println(10/0);
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("line X");
} catch (NullPointerException ex) {
System.out.println("line Y");
} catch (ArithmeticException ex) {
System.out.println("line Z");
} finally {
System.out.println("line finally");
}
System.out.println("Start all over");
}
}
public static void main(String[] args) throws Exception {
// your codes
}
or
public static void main(String[] args) {
try {
// your codes
} catch (Exception ex) {
// catch all exception because all exception class are subclass of Exception
}
}
InputStream
FileInputStream in = new FileInputStream(filename)int read()
int ch = -1;
while((ch = in.read())!= -1) {
// do something
}
BufferedReader:
BufferedReader in = new BufferedReader(new FileReader(filename));: create a BufferedReader from a file. new BufferedReader(new InputStreamReader(System.in));: create a BufferedReader from user inputString readLine:
String line = null;
while((line = in.readLine())!=null) {
// do something
}
FileOutputStream
FileOutputStream out = new FileOutputStream(filename);out.write(int ch)PrintStream
PrintStream out = new PrintStream(new FileOutputStream(filename));out.print(...), out.println(....)System.out is a PrintStreamIOException or FileNotFoundExceptionclose()read, write methods.String[] split(String exp) splits a String by exp. Example
String s = "123ab456abXXX", then s.split("ab") returns {"123", "456", "XXX"}
Math class. Example Math.sqrt(5) is used to calculate the square root of 5. Math.random() generates a random double between 0 and 0.99999999. To generate an integer random number
from 0 to max-1, use (int)(max*Math.random()).