Lec_05
.pdfMVC (Model-View-Controller)
Swing: Архитектура UI компонентов
Рисунок из статьи Fowler A. A Swing Architecture Overview. http://www.oracle.com/technetwork/java/architecture-142923.html
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 |
|
|
|
Пример использования модели
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);
…
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);
}
Компоновка
Компоновка
BorderLayout |
BoxLayout |
FlowLayout
CardLayout |
CardLayout |
Компоновка
GridLayout |
GroupLayout |
SpringLayout
GridBagLayout
SpringLayout
Компоновка: Пример
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(); }
});
}
}
Компоновка без менеджера
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);
}