![](/user_photo/2706_HbeT2.jpg)
Lec_05
.pdf![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA11x1.jpg)
MVC (Model-View-Controller)
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA12x1.jpg)
Swing: Архитектура UI компонентов
Рисунок из статьи Fowler A. A Swing Architecture Overview. http://www.oracle.com/technetwork/java/architecture-142923.html
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA13x1.jpg)
Swing: Архитектура UI компонентов
Компонент |
Модель (Тип модели) |
|
|
JButton, JToggleButton, JCheckBox, |
ButtonModel (GUI/Data) |
JRadioButton, JMenu, JMenuItem, |
|
JCheckBoxMenuItem, |
|
JRadioButtonMenuItem |
|
|
|
JComboBox |
ComboBoxModel (Data) |
|
|
JProgressBar, JScrollBar, JSlider |
BoundedRangeModel (GUI/Data) |
|
|
JTabbedPane |
SingleSelectionModel (GUI) |
|
|
JList |
ListModel (Data), ListSelectionModel (GUI) |
|
|
JTable |
TableModel (Data), TableColumnModel (GUI) |
|
|
JTree |
TreeModel (Data), TreeSelectionModel (GUI) |
|
|
JEditorPane, JTextPane, JTextArea, |
Document (Data) |
JTextField, JPasswordField |
|
|
|
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA14x1.jpg)
Пример использования модели
TreeModel в JTree
…
JTree tree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode(); root.setUserObject("Root");
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("Child"); root.add(childNode);
childNode.add(new DefaultMutableTreeNode("Grandchild")); root.add(new DefaultMutableTreeNode("Another child")); DefaultTreeModel model = new DefaultTreeModel(root); tree.setModel(model);
…
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA15x1.jpg)
Swing: Контейнеры
private static void createAndShowGUI() {
ContainerDemo frame = new ContainerDemo("ContainerDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container contentPane = frame.getContentPane(); JPanel panel1 = new JPanel();
JButton b = new JButton("Press me"); panel1.add(b); contentPane.add(panel1);
JPanel panel2 = new JPanel();
JTextField tf = new JTextField("Write here"); panel2.add(tf);
panel2.add(new JLabel("Read me")); contentPane.add(panel2);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); frame.pack(); frame.setVisible(true);
}
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA16x1.jpg)
Компоновка
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA17x1.jpg)
Компоновка
BorderLayout |
BoxLayout |
FlowLayout
CardLayout |
CardLayout |
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA18x1.jpg)
Компоновка
GridLayout |
GroupLayout |
SpringLayout
GridBagLayout
SpringLayout
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA19x1.jpg)
Компоновка: Пример
import java.awt.*; import javax.swing.*; public class FlowLayoutDemo extends JFrame {
public FlowLayoutDemo(String name) { super(name); } private static void createAndShowGUI() {
FlowLayoutDemo frame = new FlowLayoutDemo("FlowLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container pane = frame.getContentPane(); FlowLayout experimentLayout = new FlowLayout(); experimentLayout.setAlignment(FlowLayout.LEFT); pane.setLayout(experimentLayout);
for (int i = 0; i < 5; i++) pane.add(new JButton("Button " + i)); frame.pack(); frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); }
});
}
}
![](/html/2706/520/html_N0LveenT6J.U7oM/htmlconvd-vUQgzA20x1.jpg)
Компоновка без менеджера
private static void createAndShowGUI() {
FlowLayoutDemo frame = new FlowLayoutDemo("FlowLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container pane = frame.getContentPane(); pane.setLayout(null);
Insets insets = pane.getInsets(); JButton b1 = new JButton("one"); JButton b2 = new JButton("two"); pane.add(b1);
pane.add(b2);
Dimension size = b1.getPreferredSize();
b1.setBounds(25 + insets.left, 5 + insets.top, size.width, size.height); size = b2.getPreferredSize();
b2.setBounds(55 + insets.left, 40 + insets.top, size.width, size.height); frame.pack(); frame.setVisible(true);
frame.setSize(300, 125);
}