Professional Documents
Culture Documents
im danh
Nhc li bi trc
Khi nim Applets
S khc nhau gia Applets v Applications
Vng i ca applet
Mt s phng thc ca class Graphics
To mt applet
S dng tham s trong Applets
X l s kin
Mt s interface v class x l s kin
Mt s v d
Ni dung bi hc
Java Foundation Classes (AFC)
Abstract Windowing Toolkit (AWT)
Swing
Cc thng cha -Containers
Cc hp thoi - Dialogs
Qun l Layout
Cc thnh phn GUI Components
X l s kin
AWT
Java 2D
Swing
JFC
Accessibility
Mc ch chnh ca awt l
h tr cho cc ng dng
applet nhng cng c
dng thit k cc
chng trnh c giao din
ha c lp.
Swing
Swing gii quyt cc hn ch lin quan n cc thnh phn
Component
ProgressMonitor
InputStream
FilterInputStream
Container
JComponent
JComboBox
AbstractButton
JButton
JCheckBox
JList
JLabel
JToggleButton
ProgressMonitorInputStream
JMenuItem
JRadioButton
JSlider
JScrollBar
JEditPane
JTextComponent
JTextArea
JPassword
JTextField
Containers
Mt container l mt thnh phn c bit c th cha
cc thnh phn khc.
Top-Level Containers
Menu Bar
Top-level
container
Cc thnh phn
c th nhn thy
(Visible)
Content
pane
JFrame
JFrame L top-level container.
JFrame c dng to ra 1 ca s.
JFrame()
JFrame(String title)
ca s JFrame :
frame.getContentPane().add(b); //c
frame.add(b)
//mi
JFrame
ng JFrame
public int getDefaultCloseOperation()
JFrame
Khi thm cc thnh phn vo JFrame th:
JFrame
import javax.swing.*;
class FrameTest {
JPanel
JPanel l container c dng nhm cc
JPanel()
JPanel(LayoutManager mg)
JPanel
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class Main {
public static void main(String args []) {
JFrame frame = new JFrame("JFrame is me");
JPanel jpan = new JPanel();
frame.add( jpan );
JButton btnHello = new JButton("Hello");
jpan.add(btnHello);
frame.setSize(300,100);
frame.setVisible(true);
}
}
JApplet
JApplet l mt container.
javax.swing.JApplet khc vi java.applet.Applet.
Khi thm cc thnh phn vo JApplet bt buc
V d: getContentPane().add(component);
JApplet
import java.awt.*;
import javax.swing.*;
public class JAppletExample extends JApplet {
@Override
public void init() {
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.add(new JButton("Button 1"));
content.add(new JButton("Button 2"));
content.add(new JButton("Button 3"));
}
}
Dialog: JOptionPane
JOptionPane cho php bn d dng to ra cc hp thoi
nhp d liu v hin th kt qu. C rt nhiu la chn khi s
dng JOptionPane. Sau y l c php dng n gin:
Gi tr
userInput =
Phng thc
JOptionPane.showInputDialog(component, text);
JOptionPane.showMessageDialog(component, text);
Trong :
- component l ca s m hp thoi ny s t ln trn,
nu khng c th t l null
- text l xu k t
Dialog: JOptionPane
import javax.swing.JOptionPane;
public class JOptionPaneTest1 {
public static void main(String[] args) {
String ans;
ans = JOptionPane.showInputDialog(null, "Tc trn gi?");
double mph = Double.parseDouble(ans);
double kph = 1.621 * mph;
JOptionPane.showMessageDialog(null, "KPH = " + kph);
System.exit(0);
}
}
Dialog: JFileChooser
S dngJFileChooser dng la chn mt tp tin hoc
th mc trong a.
JFileChooser fileChooser = new JFileChooser();
if (retval == JFileChooser.APPROVE_OPTION) {
File myFile = fc.getSelectedFile();
Layout
FlowLayout
GridLayout
Object
BorderLayout
GridBagLayout
BoxLayout
CardLayot
FlowLayout
L Layout n gin v l mc nh cho Panel.
Cc thnh phn c sp xp theo hng.
Hng ca layout:
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
Khong cch gia cc thnh phn:
C gi tr mc nh = 5
GridLayout
L tp hp cc li c
cng kch thc
Khong cch gia cc :
Mc nh = 5
C th t li qua phng thc setHgap v setVgap.
Thay i kch thc ca cc :
Cc c th c kch thc to hn ph thuc vo
kch thc ca ca s.
BorderLayout
C 5 vng:
NORTH, SOUTH, EAST, WEST, CENTER
Khng phi tt c 5 vng u c s dng
t cc thnh phn vo mt v tr:
Panel.setLayout(new BorderLayout())
Panel.add(new JButton(Click here),
BorderLayout.NORTH)
t li khong cch gia cc thnh phn:
C th t li qua phng thc setHgap v setVgap.
S dng BorderLayout(int horGap, int verGap)
BoxLayout
BoxLayout
Y Axis
X Axis
Components
Mt s component thng dng:
JApplet**
JButton
JCheckBox
JColorChooser
JComboBox
JDialog**
JFileChooser
JFormattedTextField
JFrame**
JLabel
JMenu
JList
JMenuBar
JMenuItem
JPopupMenu
JPanel
JProgressBar
PasswordField
JRadioButton
ScrollBar
JSlider
JSpinner
JTable
JTextArea
JTextField
JToggleButton
JToolBa
JTree
JWindow **
Component: JLabel
JPanel textPanel = new JPanel();
JLabel redLabel = new JLabel("Red");
redLabel.setLocation(0, 0);
redLabel.setSize(50, 40);
textPanel.add(redLabel);
textPanel
Component: JButton
Khi to:
String text;
Icon image;
JButton btn = new JButton(text)
JButton btn = new JButton(text,image)
JButton btn = new JButton(image)
Phng thc:
ActionListener listener;
boolean b;
btn.addActionListener(listener);
btn.setEnable(b);
Component: JButton
X l s kin vi JButton:
Khi mt button c click th phng thc
actionPerformed()c thc hin vi mt ActionEvent
c truyn vo.
V d:
JButton btn = new JButton(Do Something);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
doMyAction();// khi click vo button
}
}
);
Component: JTextField
Cc phng thc:
JTextField txt = new JTextField(20) ;
txt.setText(Enter here);
Hoc JTextField txt= new JTextField("Enter here",
String str=txt.getText();
20);
txt.addActionListener(listener);
txt.setEditable(true/false);
txt.setFont(font);
txt.setHorizontalAlignment(align);
txt.requestFocus();//t con tr vo textbox
Component: JTextField
public class textFieldUpperCase extends JFrame {
public textFieldUpperCase() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField text = new JTextField(40);
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
text.setText(text.getText().toUpperCase());
}
});
getContentPane().add(text, BorderLayout.NORTH);
pack(); setVisible(true);
}
public static void main(String[] args) {
new textFieldUpperCase();
}
}
Component: JCheckBox
JCheckBox cb; //mt checkbox
String text;
//mt gi tr text
boolean state; //Gi tr true hoc false
cb = new JCheckBox(text); hoc
cb = new JCheckBox(text,state);
state = cb.isSelected();
cb.setSelected(state);
cb.addActionListener(listener);
cb.addItemListener(itemListener);
Component: JCheckBox
X l s kin khi click vo Checkbox:
int mode = 0;
};
bold.addActionListener(listener);
italic.addActionListener(listener);
Component: JRadio
JRadioButton yesButton
= new JRadioButton("Yes" , true);
JRadioButton noButton
= new JRadioButton("No" , false);
JRadioButton maybeButton = new JRadioButton("Maybe", false);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(yesButton);
bgroup.add(noButton);
bgroup.add(maybeButton);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(3, 1));
radioPanel.add(yesButton);
radioPanel.add(noButton);
radioPanel.add(maybeButton);
radioPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Married?"));
Component: JRadio
boolean b;
JRadioButton rb = new JRadioButton("Sample", false);
rb.setSelected(b);
t nt radio l b (true/false).
rb.addActionListener(listener);
Component: JList
C php:
JList()
JList(ListModel dataModel)
Jlist(Object[] listData)
Component: JList
V d:
JList boxes;
Component: JList
JList list;
DefaultListModel model;
int count = 5;
model = new DefaultListModel();
list = new JList(model);
JButton addButton = new JButton("Add Element");
for (int i = 0; i < 10; i++)
model.addElement("Element " + i);
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
model.addElement("Element " + counter);
counter++;
}
});
Component: JComboBox
Khi to:
String[] dias = {"lunes", "martes", "miercoles"};
JComboBox dayChoice = new JComboBox(dias)
Gi s c:
int index;
String item;
String obj; // obj c th kiu khc
Cc phng thc:
String JComboBox cb = new JComboBox(...);
cb.addActionListener(listener);
Component: JComboBox
Phng thc
ngha
cb.setSelectedIndex(index)
t mc c ch s l index c chn.
cb.setSelectedItem(obj)
t mc obj c chn.
cb.add(item)
Thm vo mt item.
cb.insert(item,index)
cb.remove(item)
cb.remove(index)
Xa mc c ch s index ra khi
combo box.
X l s kin
Mt s kin thng xy ra khi c s thay i trong giao
X l s kin
GUI
S kin
tc ng
Programspecific Action
handlers
ng k
Listener
Listeners
X l
s kin.
X l s kin
Event Object
ActionEvent
Interface v method
interface ActionListener
Cc method lin kt
addActionListener
actionPerformed(ActionEvent) removeActionListener
Event Source
JButton
JCheckBox
JComboBox
JTextField
JRadioButton
ItemEvent
interface ItemListener
addItemListener
itemStateChanged(ItemEvent) removeItemListener
JCheckBox
JRadioButton
JComboBox
MouseEvent
interface MouseListener
addMouseListener
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mouseClicked(MouseEvent)
c to bi s kin
phn GUI no.
X l s kin
S dng Inner Class Listener c tn: L cch
X l s kin
import javax.swing.*;
import java.awt.event.*;
class SomePanel extends JPanel {
private JButton myGreetingButton = new JButton("Hello");
private JTextField myGreetingField = new JTextField(20);
public SomePanel() {
ActionListener doGreeting = new GreetingListener();
myGreetingButton.addActionListener(doGreeting);
myGreetingField.addActionListener(doGreeting);
// Cc lnh khc
}
// Inner class tn l GreetingListener
private class GreetingListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
myGreetingField.setText("Guten Tag");
}
}
}
X l s kin
class myPanel extends JPanel {
. . .
public MyPanel() {
JButton b1 = new JButton("Hello");
...
}
b1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
// Cc lnh x l s kin cho b1
}
}
);
X l s kin
...
class Hello extends JApplet implements ActionListener{
JButton b;
...
public void init() {
JButton b = new JButton("Hello");
b.addActionListener(this);
...
}
public void actionPerformed(ActionEvent e) {
message = "Hi";
repaint();
}
...
}
Tng kt bi hc
Java Foundation Class (AFC)
Abstract Windowing Toolkit (AWT)
Swing
Cc thng cha -Containers
Cc hp thoi - Dialogs
Qun l Layout
Cc thnh phn GUI Components
X l s kin