List of usage examples for javax.swing JPanel add
public Component add(String name, Component comp)
From source file:MyLabel.java
public static void main(String[] args) { String lyrics = "<html>Line<br>line<br>line</html>"; JPanel panel = new JPanel(); panel.setLayout(new BorderLayout(10, 10)); JLabel label = new JLabel(lyrics); label.setFont(new Font("Georgia", Font.PLAIN, 14)); label.setForeground(new Color(50, 50, 25)); panel.add(label, BorderLayout.CENTER); panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); JFrame f = new JFrame(); f.add(panel);//from w w w . ja v a 2 s. co m f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JPanel ui = new JPanel(new BorderLayout(4, 4)); ui.setBorder(new EmptyBorder(6, 6, 6, 6)); JPanel controls = new JPanel(new FlowLayout(FlowLayout.LEADING)); ui.add(controls, BorderLayout.PAGE_START); int s = 100;/* w ww .j a v a 2 s .com*/ Dimension[] sizes = { new Dimension(s * 4, s * 2), new Dimension(s * 6, s * 3), new Dimension(s * 8, s * 4) }; final JComboBox cb = new JComboBox(sizes); controls.add(cb); final JPanel[] panels = new JPanel[sizes.length]; for (int ii = 0; ii < sizes.length; ii++) { Dimension d = sizes[ii]; BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB); JPanel p = new JPanel(new GridLayout()); JLabel l = new JLabel(new ImageIcon(bi)); p.add(l); panels[ii] = p; } ItemListener sizeListener = new ItemListener() { JPanel current = panels[0]; @Override public void itemStateChanged(ItemEvent e) { JPanel next = panels[cb.getSelectedIndex()]; swapComponentsAndResizeUI(ui, current, next); current = next; } }; cb.addItemListener(sizeListener); ui.add(panels[0], BorderLayout.CENTER); JFrame f = new JFrame("Three Sized Panels"); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setContentPane(ui); f.pack(); f.setLocationByPlatform(true); f.setVisible(true); }
From source file:Main.java
public static void main(String args[]) throws Exception { JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Name: "); label.setDisplayedMnemonic(KeyEvent.VK_N); JTextField textField = new JTextField(); label.setLabelFor(textField);/*from w w w. ja va 2 s . c o m*/ panel.add(label, BorderLayout.WEST); panel.add(textField, BorderLayout.CENTER); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel, BorderLayout.NORTH); frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); textField.setText("your text"); String filename = "test.txt"; FileWriter writer = new FileWriter(filename); textField.write(writer); writer.close(); }
From source file:Main.java
public static void main(String[] args) { JTextField[] txtAllAverages;/* w ww . ja v a 2s.c om*/ int testCount = 2; txtAllAverages = new JTextField[testCount]; JPanel inputControls = new JPanel(new BorderLayout(5, 5)); JPanel inputControlsLabels = new JPanel(new GridLayout(0, 1, 3, 3)); JPanel inputControlsFields = new JPanel(new GridLayout(0, 1, 3, 3)); inputControls.add(inputControlsLabels, BorderLayout.WEST); inputControls.add(inputControlsFields, BorderLayout.CENTER); for (int i = 0; i < testCount; i++) { inputControlsLabels.add(new JLabel("Test score: ")); JTextField field = new JTextField(10); inputControlsFields.add(field); txtAllAverages[i] = field; } JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 2)); controls.add(new JButton("Reset")); controls.add(new JButton("Submit")); JPanel gui = new JPanel(new BorderLayout(10, 10)); gui.setBorder(new TitledBorder("Averages")); gui.add(inputControls, BorderLayout.CENTER); gui.add(controls, BorderLayout.SOUTH); JFrame f = new JFrame(); f.setContentPane(gui); f.pack(); f.setLocationByPlatform(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:SimpleClient.java
public static void main(String argv[]) { boolean usage = false; for (int optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-L")) { url.addElement(argv[++optind]); } else if (argv[optind].startsWith("-")) { usage = true;/*ww w . j a v a 2 s .co m*/ break; } else { usage = true; break; } } if (usage || url.size() == 0) { System.out.println("Usage: SimpleClient -L url"); System.out.println(" where url is protocol://username:password@hostname/"); System.exit(1); } try { // Set up our Mailcap entries. This will allow the JAF // to locate our viewers. File capfile = new File("simple.mailcap"); if (!capfile.isFile()) { System.out.println("Cannot locate the \"simple.mailcap\" file."); System.exit(1); } CommandMap.setDefaultCommandMap(new MailcapCommandMap(new FileInputStream(capfile))); JFrame frame = new JFrame("Simple JavaMail Client"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Get a Store object SimpleAuthenticator auth = new SimpleAuthenticator(frame); Session session = Session.getDefaultInstance(System.getProperties(), auth); //session.setDebug(true); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); // create a node for each store we have for (Enumeration e = url.elements(); e.hasMoreElements();) { String urlstring = (String) e.nextElement(); URLName urln = new URLName(urlstring); Store store = session.getStore(urln); StoreTreeNode storenode = new StoreTreeNode(store); root.add(storenode); } DefaultTreeModel treeModel = new DefaultTreeModel(root); JTree tree = new JTree(treeModel); tree.addTreeSelectionListener(new TreePress()); /* Put the Tree in a scroller. */ JScrollPane sp = new JScrollPane(); sp.setPreferredSize(new Dimension(250, 300)); sp.getViewport().add(tree); /* Create a double buffered JPanel */ JPanel sv = new JPanel(new BorderLayout()); sv.add("Center", sp); fv = new FolderViewer(null); JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sv, fv); jsp.setOneTouchExpandable(true); mv = new MessageViewer(); JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, mv); jsp2.setOneTouchExpandable(true); frame.getContentPane().add(jsp2); frame.pack(); frame.show(); } catch (Exception ex) { System.out.println("SimpletClient caught exception"); ex.printStackTrace(); System.exit(1); } }
From source file:OffsetSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Offset Example"); Container content = frame.getContentPane(); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Name: "); label.setDisplayedMnemonic(KeyEvent.VK_N); final JTextField textField = new JTextField(); label.setLabelFor(textField);/* w w w . ja va2s .c om*/ panel.add(label, BorderLayout.WEST); panel.add(textField, BorderLayout.CENTER); content.add(panel, BorderLayout.NORTH); JButton button = new JButton("Get Offset"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Offset: " + textField.getScrollOffset()); System.out.println("Visibility: " + textField.getHorizontalVisibility()); BoundedRangeModel model = textField.getHorizontalVisibility(); int extent = model.getExtent(); textField.setScrollOffset(extent); } }; button.addActionListener(actionListener); content.add(button, BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); }
From source file:LabelSampleLoadText.java
public static void main(String args[]) { JFrame frame = new JFrame("Label Focus Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Name: "); label.setDisplayedMnemonic(KeyEvent.VK_N); JTextField textField = new JTextField(); label.setLabelFor(textField);/*from w ww . jav a2s.c om*/ panel.add(label, BorderLayout.WEST); panel.add(textField, BorderLayout.CENTER); frame.add(panel, BorderLayout.NORTH); frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); FileReader reader = null; try { String filename = "test.txt"; reader = new FileReader(filename); textField.read(reader, filename); } catch (IOException exception) { System.err.println("Load oops"); } finally { if (reader != null) { try { reader.close(); } catch (IOException exception) { System.err.println("Error closing reader"); exception.printStackTrace(); } } } }
From source file:LabelSampleSaveText.java
public static void main(String args[]) { JFrame frame = new JFrame("Label Focus Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Name: "); label.setDisplayedMnemonic(KeyEvent.VK_N); JTextField textField = new JTextField(); label.setLabelFor(textField);/*from w ww .j a va 2 s . c o m*/ panel.add(label, BorderLayout.WEST); panel.add(textField, BorderLayout.CENTER); frame.add(panel, BorderLayout.NORTH); frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); textField.setText("your text"); String filename = "test.txt"; FileWriter writer = null; try { writer = new FileWriter(filename); textField.write(writer); } catch (IOException exception) { System.err.println("Save oops"); } finally { if (writer != null) { try { writer.close(); } catch (IOException exception) { System.err.println("Error closing writer"); exception.printStackTrace(); } } } }
From source file:DndTree.java
public static void main(String args[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("Drag me:"); JTextField text = new JTextField(); text.setDragEnabled(true);/* w w w .j av a2 s .c o m*/ top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); tree.setDropMode(DropMode.USE_SELECTION); f.setSize(300, 400); f.setVisible(true); }
From source file:DndTree.java
public static void main(String args[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("Drag me:"); JTextField text = new JTextField(); text.setDragEnabled(true);// w w w .j a va2 s. c om top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); tree.setDropMode(DropMode.INSERT); f.setSize(300, 400); f.setVisible(true); }