📄 demo.java
字号:
ButtonGroup bg = new ButtonGroup(); bg.add(c); bg.add(d); p.add(c); p.add(d); return p; } public static JList mkList(Object[] elts) { JList list = new JList(elts); list.setFont(new Font("Luxi", Font.PLAIN, 14)); return list; } public static JTabbedPane mkTabs(String[] names) { JTabbedPane tabs = new JTabbedPane(); for (int i = 0; i < names.length; ++i) { tabs.addTab(names[i], mkButton(names[i])); } return tabs; } public static JComboBox mkComboBox(String[] names) { JComboBox box = new JComboBox(names); return box; } public static JSpinner mkSpinner() { JSpinner spinner = new JSpinner(); return spinner; } public static JButton mkBigButton(String title) { JButton b = new JButton(title); b.setMargin(new Insets(5,5,5,5)); //b.setFont(new Font("Luxi", Font.PLAIN, 14)); return b; } public static JToggleButton mkToggle(String title) { JToggleButton b = new JToggleButton(title); b.setMargin(new Insets(5,5,5,5)); b.setFont(new Font("Luxi", Font.PLAIN, 14)); return b; } public static JPanel mkPanel(JComponent[] inners) { JPanel p = new JPanel(); for (int i = 0; i < inners.length; ++i) { p.add(inners[i]); } return p; } public static JScrollBar mkScrollBar() { JScrollBar scrollbar = new JScrollBar(); return scrollbar; } public static JPanel mkViewportBox(final JComponent inner) { final JViewport port = new JViewport(); port.setView(inner); JButton left = mkBigButton("left"); JButton right = mkBigButton("right"); left.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Point p = port.getViewPosition(); port.setViewPosition(new Point(p.x - 10, p.y)); } }); right.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Point p = port.getViewPosition(); port.setViewPosition(new Point(p.x + 10, p.y)); } }); return mkPanel(new JComponent[] {port, left, right}); } public static JPanel mkListPanel(Object[] elts) { final DefaultListModel mod = new DefaultListModel(); final JList list1 = new JList(mod); list1.setLayoutOrientation(JList.VERTICAL_WRAP); list1.setVisibleRowCount(4); final JList list2 = new JList(mod); list2.setLayoutOrientation(JList.VERTICAL_WRAP); list2.setVisibleRowCount(4); list2.setSelectionModel(list1.getSelectionModel()); for (int i = 0; i < elts.length; ++i) mod.addElement(elts[i]); list1.setCellRenderer(new LabelCellRenderer()); list2.setCellRenderer(new CheckCellRenderer()); JButton add = mkBigButton("add element"); add.addActionListener(new ActionListener() { int i = 0; public void actionPerformed(ActionEvent e) { mod.addElement("new element " + i); ++i; } }); JButton del = mkBigButton("delete selected"); del.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { for (int i = 0; i < mod.getSize(); ++i) if (list1.isSelectedIndex(i)) mod.remove(i); } }); return mkPanel(new JComponent[] {list1, //mkScrollPane(list1), list2, //mkScrollPane(list2), mkPanel(new JComponent[] {add, del})}); } public static JButton mkDisposerButton(final JFrame c) { JButton close = mkBigButton("Close"); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { c.dispose(); } }); return close; } public static JColorChooser mkColorChooser() { return new JColorChooser(); } private static class PopUpAction implements ActionListener { private JComponent inner; private String name; PopUpAction(String n, JComponent i, JMenu m) { name = n; inner = i; JMenuItem item = new JMenuItem(name); item.addActionListener(this); m.add(item); } PopUpAction(String n, JComponent i, JPanel p) { name = n; inner = i; JButton b = mkBigButton(name); b.addActionListener(this); p.add(b); } public void actionPerformed(ActionEvent e) { JFrame frame = new JFrame(name); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(inner, BorderLayout.CENTER); frame.getContentPane().add(mkDisposerButton(frame), BorderLayout.SOUTH); frame.pack(); frame.show(); } } private static JEditorPane mkEditorPane() { JEditorPane editorPane = new JEditorPane(); editorPane.setEditable(true); return editorPane; } private static JTree mkTree() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root node"); DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child node 1"); DefaultMutableTreeNode child11 = new DefaultMutableTreeNode("Child node 1.1"); DefaultMutableTreeNode child12 = new DefaultMutableTreeNode("Child node 1.2"); DefaultMutableTreeNode child13 = new DefaultMutableTreeNode("Child node 1.3"); DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child node 2"); DefaultMutableTreeNode child21 = new DefaultMutableTreeNode("Child node 2.1"); DefaultMutableTreeNode child22 = new DefaultMutableTreeNode("Child node 2.2"); DefaultMutableTreeNode child23 = new DefaultMutableTreeNode("Child node 2.3"); DefaultMutableTreeNode child24 = new DefaultMutableTreeNode("Child node 2.4"); DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Child node 3"); root.add(child1); root.add(child2); root.add(child3); child1.add(child11); child1.add(child12); child1.add(child13); child2.add(child21); child2.add(child22); child2.add(child23); child2.add(child24); JTree tree = new JTree(root); tree.setLargeModel(true); DefaultTreeSelectionModel dtsm = new DefaultTreeSelectionModel(); dtsm.setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION); tree.setSelectionModel(dtsm); return tree; } private static JTable mkTable() { Object[][] tableData = new Object[][] { { "Field 1", "Field 2" , "Field 3" }, { "Field 4", "Field 5" , "Field 6" }, { "Field 7", "Field 8" , "Field 9" }, { "Field 10", "Field 11" , "Field 12" } }; Object[] columnNames = new Object[] {"Column 1", "Column 2", "Column 3"}; JTable table = new JTable(tableData, columnNames); return table; } private JPanel mkButtonBar() { JPanel panel = new JPanel (new GridLayout(2, 1)); JPanel panelA = new JPanel(new FlowLayout()); JPanel panelB = new JPanel(new FlowLayout()); new PopUpAction("Buttons", (new ButtonDemo("Button Demo")).createContent(), panelA); new PopUpAction("Toggles", mkToggle("cool and refreshing"), panelA); new PopUpAction("Checkbox", mkCheckbox("ice cold"), panelA); new PopUpAction("Radio", mkRadio("delicious"), panelA); new PopUpAction("Slider", (new SliderDemo("Slider Demo")).createContent(), panelA); new PopUpAction("ProgressBar", ProgressBarDemo.createContent(), panelA); new PopUpAction("List", mkListPanel(new String[] { "hello", "this", "is", "a", "list", "that", "wraps", "over"}), panelA); new PopUpAction("Scrollbar", (new ScrollBarDemo("ScrollBar Demo")).createContent(), panelA); new PopUpAction("Viewport", mkViewportBox(mkBigButton("View Me!")), panelA); new PopUpAction("ScrollPane", mkScrollPane(mkBigButton("Scroll Me!")), panelA); new PopUpAction("TabPane", mkTabs(new String[] {"happy", "sad", "indifferent"}), panelB); new PopUpAction("Spinner", mkSpinner(), panelB); new PopUpAction("TextField", (new TextFieldDemo("TextField Demo")).createContent(), panelB); new PopUpAction("FileChooser", (new FileChooserDemo("FileChooser Demo")).createContent(), panelB); new PopUpAction("ColorChooser", mkColorChooser(), panelB); new PopUpAction("ComboBox", (new ComboBoxDemo("ComboBox Demo")).createContent(), panelB); new PopUpAction("Editor", mkEditorPane(), panelB); new PopUpAction("Tree", mkTree(), panelB); new PopUpAction("Table", mkTable(), panelB); JButton exitDisposer = mkDisposerButton(frame); panelB.add(exitDisposer); exitDisposer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(1); } }); panel.add(panelA); panel.add(panelB); return panel; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -