Download Slides from last year

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
10/1/2008
Overview
• Layout managers
Lecture 8: Layouts, design and
exceptions.
Written by: Daniel Dalevi
– BorderLayout
– GridLayout
• JTextField and JTextArea
• JPanel
• An interactive control panel with buttons
(example)
• Handle exceptions
Layout managers
BorderLayout
• As we know, components can be added to
frames.
• The JFrame contains a content pane which is a
type of container.
• If adding several components to the frame
they will be placed on top of each other.
• Layout managers help organize the location of
components.
• Divides the container into five different areas.
• Components are added to the layout using
add( Component, Index );
1
10/1/2008
Sample code
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize( 500, 200 );
frame.setTitle( "Testing boarder layout" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JButton
JButton
JButton
JButton
JButton
bN
bS
bW
bE
bC
=
=
=
=
=
new
new
new
new
new
JButton(
JButton(
JButton(
JButton(
JButton(
"bN"
"bS"
"bW"
"bE"
"bC"
GridLayout
• Divides the container into a grid of M rows
and N columns.
• Components are added to the layout using
add( Component );
N
);
);
);
);
);
frame.getContentPane().setLayout( new BorderLayout() );
frame.add(
frame.add(
frame.add(
frame.add(
frame.add(
bN,
bS,
bW,
bE,
bC,
BorderLayout.NORTH );
BorderLayout.SOUTH );
BorderLayout.WEST );
BorderLayout.EAST );
BorderLayout.CENTER );
M
frame.setVisible( true );
}
}
Sample code
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize( 500, 200 );
frame.setTitle( "Testing boarder layout" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().setLayout( new GridLayout( 8, 3 ) );
for( int i = 1; i <= 24; ++i )
{
String txt = Integer.toString( i );
JButton b = new JButton( txt );
frame.add( b );
}
frame.setVisible( true );
}
}
JTextField
• A text field can be used to enter or display a
string.
Function
Description
JTextField()
Default constructor
JTextField( String txt )
Constructor that assigns txt to
the text area.
setFont( Font f )
Sets the font
setText( Strint txt )
Sets text to display in text area
String getText()
Returns the current text of the
text area
2
10/1/2008
JTextArea
JPanel
• If you want user to be able to enter multiple
lines of text, you can either use multiple
JTextField instances, or, use a JTextArea
Function
Description
JTextArea()
Default constructor
JTextArea( int M, int N )
Constructor that creates a
text area of M cols and N rows
append( String s)
Adds the string s to the text
area
insert( String s, int pos )
Inserts the string s at position
pos.
int getLineCount()
Returns the number of lines
Example:
JPanel
JComponent
ControlPanel
JButton
JTextField
StringComponent
• JPanels is a light-weight container and can be
used to group components.
• JPanels, as JFrames, can use layout managers
for organizing the location of components.
• A controlpanel can be created by extending
the JPanel and let it contain several buttons
and Fields.
Exceptions
JFrame
MyViewer
StringComponent
ControlPanel
• A class that signals a condition that prevents
the program from continuing normally.
• Under such conditions an Exception is
thrown.
• All Exceptions should be catch otherwise
program will be terminated.
3
10/1/2008
Different kinds of Exceptions
Throwable
Many more …
Error
Exception
FileNotFound
Exception
Scanner fileIn = null;
try{
fileIn = new Scanner( file );
}
catch( Exception e )
{
System.out.println( "Cannot open file: " + filename );
System.exit( 1 );
}
Runtime
Exception
IOException
EOFException
FileWriter (again)
import java.io.File;
import java.util.Scanner;
public class CatFile
{
public static void main(String[] args)
{
Scanner in = new Scanner( System.in );
System.out.print( "Enter a filename: " );
String filename = in.nextLine();
File file = new File( filename );
NullPointer
Exception
while( fileIn.hasNext() )
{
System.out.println( fileIn.nextLine() );
}
fileIn.close();
IndexOutOfBound
Exception
}
}
More specific …
Checked and unchecked exceptions
try {
fileIn = new Scanner( file );
}
catch( FileNotFoundException e )
{
System.out.println( “File not found!” );
System.exit( 1 );
}
catch( BadDataException e2 )
{
System.out.println( “Bad data: “ +e2.getMessage() );
System.exit( 1 );
}
catch( IOException e3 )
{
e3.printStackTrace();
System.exit( 1 );
}
• Unchecked exceptions, the programmer may
ignore (though often not recommended).
– IndexOutOfBoundException
– NullPointerException
• If you call a method that throws a checked
exception, the compiler checks that you do
not ignore it.
– IOException
4