Lec_05
.pdfЛекция 5.
GUI с помощью JFC/Swing
А. О. Шигаров shigarov@icc.ru
JFC (Java Foundation Classes)
•AWT (Abstract Window Toolkit)
•Swing
•Java 2D
JFC внутри Java
Java Conceptual Diagram из Java Platform Standard Edition 7 Documentation http://docs.oracle.com/javase/7/docs/index.html
AWT: UI компоненты
Peer architecture
Рисунок из книги Zukowski J. Java AWT Reference. O'Reilly. 1997. Chapter 1.
Abstract Window Toolkit Overview. http://oreilly.com/openbook/javawt/book/ch01.pdf
AWT: UI компоненты
AWT: Вспомогательные классы
AWT: Вспомогательные классы
Класс |
Пример |
|
|
Color |
Color(int r, int g, int b) // RGB: 0 ≤ r, g, b ≥ 255 |
|
Color(float h, float s, float b) // HSB: 0.0 ≤ h, s, b ≥ 1.0 |
|
Color c = new Color(255,0,0); |
|
|
|
Font(String name, int style, int size) |
Font |
Font f = new Font("Times New Roman", Font.PLAIN, 12); |
|
Font.BOLD, Font.ITALIC, Font.PLAIN |
|
|
Dimension |
Dimension(int width, int height) |
|
Dimension d = new Dimension(800,600); |
|
|
Graphics, |
draw…, drawOval(int x, int y, int width, int height), |
Graphics2D |
drawPolygon(Polygon p), … |
|
fill..., fillOval(int x, int y, int width, int height), fillPolygon(Polygon p), … |
|
set..., setColor(Color c), setFont(Font f), … |
|
get…, getColor(), getFont(), … |
|
|
AWT: Hello World!
import java.awt.*; import java.awt.event.*; public class AWTHelloWorld {
public static void createAndShowGUI() { Frame f = new Frame("AWTHelloWorld"); f.add(new Label("Hello World!"));
f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
System.exit(0); // Завершение приложения
}
});
f.pack(); //задаем окну предпочитаемые размеры f.setVisible(true);
}
public static void main(String[] args) {
//Запускаем выполнение приложения в потоке диспетчеризации событий
EventQueue.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } });
}
}
Swing: UI компоненты
Swing: Hello World!
import javax.swing.*;
public class SwingHelloWorld extends JFrame {
public SwingHelloWorld(String name) { super(name); } private static void createAndShowGUI() {
SwingHelloWorld frame = new SwingHelloWorld("SwingHelloWorld"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new JLabel("Hello World!")); frame.pack(); frame.setVisible(true);
}
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() {
public void run() { createAndShowGUI(); }
});
}
}