Presentation is loading. Please wait.

Presentation is loading. Please wait.

2013.03.21.  Java GUI API  Layout Managers [Sample code] ShowFlowLayout.java 、 ShowGripLayout.java 、 ShowBorderLayout.java TestImageIcon.java.

Similar presentations


Presentation on theme: "2013.03.21.  Java GUI API  Layout Managers [Sample code] ShowFlowLayout.java 、 ShowGripLayout.java 、 ShowBorderLayout.java TestImageIcon.java."— Presentation transcript:

1 2013.03.21

2  Java GUI API  Layout Managers [Sample code] ShowFlowLayout.java 、 ShowGripLayout.java 、 ShowBorderLayout.java TestImageIcon.java

3  GUI: Graphical User Interface  GUI classes can be classified into three group: 1. container classes 2. component classes 3. helper classes

4  Container classes: ex: JFrame, Jpanel, Japplet  Component classes ex: JButton, JTextFiels, JTextArea, JComboBox, Jlist, JRadioButton  Helper classes ex: Graphics, Color, Font, FontMetrics, Dimension

5 //Add Component(JButton) to the JFrame import javax.swing.*; public class MyFrameWithComponents { public static void main(String[] args) { JFrame frame = new JFrame(); JButton jbtOK = new JButton("OK"); //add a button into frame frame.add(jbtOK); frame.setSize(400,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null);//ceter the frame frame.setVisible(true); }

6  FlowLayout: ( 水流 ) 最簡單的 layout 方式, 方向由左到右  GridLayout: ( 格子 ) 以指定行與列的矩陣方式排列  BorderLayout: ( 邊緣 ) 分為五種邊緣, North,West,East,South, 指定其一

7 //Show FlowLayout 從左到右, 從上到下的佈置 import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JFrame; import java.awt.FlowLayout; import javax.swing.*; public class ShowFlowLayout extends JFrame { public ShowFlowLayout() { //Set FlowLayout, aligned left with horizontal gap 10 // and vertical gap 20 between components setLayout(new FlowLayout(FlowLayout.LEFT,10,20)); //add labels and text field to the frame add(new JLabel("First Name")); add(new JTextField(8)); add(new JLabel("MI")); add(new JTextField(1)); add(new JLabel("Last Name")); add(new JTextField(8)); } public static void main(String[] args) { ShowFlowLayout frame = new ShowFlowLayout(); frame.setTitle("ShowFlowLayout"); frame.setSize(200,200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

8 //Show GridLayout 格子狀的佈置, 指定行數和列數, 還有垂直跟水平間距 import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JFrame; import java.awt.GridLayout; import javax.swing.*; public class ShowGridLayout extends JFrame { public ShowGridLayout() { setLayout(new GridLayout(3,2,5,5));// (row, column, hgap, vgap) //add labels and text field to the frame add(new JLabel("First Name")); add(new JTextField(8)); add(new JLabel("MI")); add(new JTextField(1)); add(new JLabel("Last Name")); add(new JTextField(8)); } public static void main(String[] args) { ShowGridLayout frame = new ShowGridLayout(); frame.setTitle("ShowGridLayout"); frame.setSize(200,125); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

9 //Show BorderLayout 邊緣的佈置法 import javax.swing.JButton; import javax.swing.JFrame; import java.awt.BorderLayout; import javax.swing.*; public class ShowBorderLayout extends JFrame { public ShowBorderLayout() { setLayout(new BorderLayout(5,10));// Set BorderLayout with horizontal gap 5 and vertical gap 10 //add labels and text field to the frame add(new JButton("East"),BorderLayout.EAST); add(new JButton("South"),BorderLayout.SOUTH); add(new JButton("West"),BorderLayout.WEST); add(new JButton("North"),BorderLayout.NORTH); add(new JButton("CENTER"),BorderLayout.CENTER); } public static void main(String[] args) { ShowBorderLayout frame = new ShowBorderLayout(); frame.setTitle("ShowBorderLayout"); frame.setSize(300,200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

10  Java currently supports three image formats: GIF 、 JPEG 、 PNG ImageIcon icon = new ImageIcon(“image/us.gif”);  An image icon can be displayed in a label or a button using: new JLabel(imageIcon) new JButton(imageIcon)

11 // 圖形介面加入 Image import javax.swing.*; import java.awt.*; public class TestImageIcon extends JFrame { private ImageIcon usIcon = new ImageIcon("./us.gif"); private ImageIcon myIcon = new ImageIcon("./my.jpg"); private ImageIcon frIcon = new ImageIcon("./fr.gif"); private ImageIcon ukIcon = new ImageIcon("./uk.gif"); public TestImageIcon() { setLayout(new GridLayout(1, 4, 5, 5)); add(new JLabel(usIcon)); add(new JLabel(myIcon)); add(new JButton(frIcon)); add(new JButton(ukIcon)); } /** Main method */ public static void main(String[] args) { TestImageIcon frame = new TestImageIcon(); frame.setTitle("TestImageIcon"); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1200, 250); frame.setVisible(true); }

12

13  Ppt 下載:  http://oss.csie.fju.edu.tw/~jastine01/ppt.html


Download ppt "2013.03.21.  Java GUI API  Layout Managers [Sample code] ShowFlowLayout.java 、 ShowGripLayout.java 、 ShowBorderLayout.java TestImageIcon.java."

Similar presentations


Ads by Google