List of usage examples for javax.swing JPanel setLayout
public void setLayout(LayoutManager mgr)
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); System.out.println(layout.getAutoCreateGaps()); JButton buttonD = new JButton("D"); JButton buttonR = new JButton("R"); JButton buttonY = new JButton("Y"); JButton buttonO = new JButton("O"); JButton buttonT = new JButton("T"); GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup(); leftToRight.addComponent(buttonD);/* www. j a va 2 s .co m*/ GroupLayout.ParallelGroup columnMiddle = layout.createParallelGroup(); columnMiddle.addComponent(buttonR); columnMiddle.addComponent(buttonO); columnMiddle.addComponent(buttonT); leftToRight.addGroup(columnMiddle); leftToRight.addComponent(buttonY); GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup(); GroupLayout.ParallelGroup rowTop = layout.createParallelGroup(); rowTop.addComponent(buttonD); rowTop.addComponent(buttonR); rowTop.addComponent(buttonY); topToBottom.addGroup(rowTop); topToBottom.addComponent(buttonO); topToBottom.addComponent(buttonT); layout.setHorizontalGroup(leftToRight); layout.setVerticalGroup(topToBottom); frame.add(panel); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); System.out.println(layout.getHonorsVisibility()); JButton buttonD = new JButton("D"); JButton buttonR = new JButton("R"); JButton buttonY = new JButton("Y"); JButton buttonO = new JButton("O"); JButton buttonT = new JButton("T"); GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup(); leftToRight.addComponent(buttonD);// w ww. j a va 2 s . c om GroupLayout.ParallelGroup columnMiddle = layout.createParallelGroup(); columnMiddle.addComponent(buttonR); columnMiddle.addComponent(buttonO); columnMiddle.addComponent(buttonT); leftToRight.addGroup(columnMiddle); leftToRight.addComponent(buttonY); GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup(); GroupLayout.ParallelGroup rowTop = layout.createParallelGroup(); rowTop.addComponent(buttonD); rowTop.addComponent(buttonR); rowTop.addComponent(buttonY); topToBottom.addGroup(rowTop); topToBottom.addComponent(buttonO); topToBottom.addComponent(buttonT); layout.setHorizontalGroup(leftToRight); layout.setVerticalGroup(topToBottom); frame.add(panel); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String... args) { JPanel contentPane; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane = new JPanel(); contentPane.setLayout(new GridBagLayout()); JPanel centerPanel = new JPanel(); centerPanel.setOpaque(true);/*w ww . ja va 2s. c om*/ centerPanel.setBackground(Color.CYAN); GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.FIRST_LINE_START; gbc.weightx = 1.0; gbc.weighty = 0.9; gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.BOTH; contentPane.add(centerPanel, gbc); JButton leftButton = new JButton("Left"); JButton rightButton = new JButton("Right"); gbc.gridwidth = 1; gbc.gridy = 1; gbc.weightx = 0.3; gbc.weighty = 0.1; contentPane.add(leftButton, gbc); gbc.gridx = 1; gbc.weightx = 0.7; gbc.weighty = 0.1; contentPane.add(rightButton, gbc); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); }
From source file:OverlaySample.java
public static void main(String args[]) { ActionListener generalActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JComponent comp = (JComponent) actionEvent.getSource(); System.out.println(actionEvent.getActionCommand() + ": " + comp.getBounds()); }//ww w. j a va2s. co m }; ActionListener sizingActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { setupButtons(actionEvent.getActionCommand()); } }; JFrame frame = new JFrame("Overlay Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); LayoutManager overlay = new OverlayLayout(panel); panel.setLayout(overlay); Object settings[][] = { { "Small", new Dimension(25, 25), Color.white }, { "Medium", new Dimension(50, 50), Color.gray }, { "Large", new Dimension(100, 100), Color.black } }; JButton buttons[] = { smallButton, mediumButton, largeButton }; for (int i = 0, n = settings.length; i < n; i++) { JButton button = buttons[i]; button.addActionListener(generalActionListener); button.setActionCommand((String) settings[i][0]); button.setMaximumSize((Dimension) settings[i][1]); button.setBackground((Color) settings[i][2]); panel.add(button); } setupButtons(SET_CENTRAL); JPanel actionPanel = new JPanel(); actionPanel.setBorder(BorderFactory.createTitledBorder("Change Alignment")); String actionSettings[] = { SET_MINIMUM, SET_MAXIMUM, SET_CENTRAL, SET_MIXED }; for (int i = 0, n = actionSettings.length; i < n; i++) { JButton button = new JButton(actionSettings[i]); button.addActionListener(sizingActionListener); actionPanel.add(button); } Container contentPane = frame.getContentPane(); contentPane.add(panel, BorderLayout.CENTER); contentPane.add(actionPanel, BorderLayout.SOUTH); frame.setSize(400, 300); frame.setVisible(true); }
From source file:OverlaySample.java
public static void main(String args[]) { JFrame frame = new JFrame("Overlay Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { public boolean isOptimizedDrawingEnabled() { return false; }/*from ww w . j a v a 2 s . c o m*/ }; LayoutManager overlay = new OverlayLayout(panel); panel.setLayout(overlay); JButton button = new JButton("Small"); button.setMaximumSize(new Dimension(25, 25)); button.setBackground(Color.white); panel.add(button); button = new JButton("Medium"); button.setMaximumSize(new Dimension(50, 50)); button.setBackground(Color.gray); panel.add(button); button = new JButton("Large"); button.setMaximumSize(new Dimension(100, 100)); button.setBackground(Color.black); panel.add(button); frame.add(panel, BorderLayout.CENTER); frame.setSize(400, 300); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JTextPane textPane = new JTextPane(); JButton button = new JButton("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setLayout(new BorderLayout()); panel.setPreferredSize(new Dimension(200, 200)); panel.add(textPane, BorderLayout.CENTER); panel.add(button, BorderLayout.SOUTH); textPane.addStyle("myStyle", null); button.addActionListener(e -> {//from w w w . j a va 2 s .c o m StyledDocument doc = textPane.getStyledDocument(); int start = textPane.getSelectionStart(); int end = textPane.getSelectionEnd(); if (start == end) { return; } if (start > end) { int life = start; start = end; end = life; } Style style = textPane.getStyle("myStyle"); if (StyleConstants.isBold(style)) { StyleConstants.setBold(style, false); } else { StyleConstants.setBold(style, true); } doc.setCharacterAttributes(start, end - start, style, false); }); frame.add(panel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:com.ohalo.cn.awt.JFreeChartTest.java
public static void main(String[] args) throws Exception { JFreeChartTest test = new JFreeChartTest(); List<JFreeChart> charts = test.printHardDiskCharts(); JPanel mainPanel = new JPanel(); JFreeChart chart = charts.get(0);//from w w w . j a va2s . c o m JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(400, 300)); panel.add(chartPanel, BorderLayout.CENTER); mainPanel.add(panel, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20, 20, 10, 10), 0, 0)); chart = charts.get(1); panel = new JPanel(); ChartPanel chartPanel2 = new ChartPanel(chart); chartPanel2.setPreferredSize(new Dimension(400, 300)); panel.setLayout(new BorderLayout()); panel.add(chartPanel2, BorderLayout.CENTER); mainPanel.add(panel, new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 10, 10), 0, 0)); chart = charts.get(2); panel = new JPanel(); ChartPanel chartPanel3 = new ChartPanel(chart); chartPanel3.setPreferredSize(new Dimension(400, 300)); panel.setLayout(new BorderLayout()); panel.add(chartPanel3, BorderLayout.CENTER); mainPanel.add(panel, new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 10, 10), 0, 0)); chart = charts.get(3); panel = new JPanel(); ChartPanel chartPanel4 = new ChartPanel(chart); chartPanel4.setPreferredSize(new Dimension(400, 300)); panel.setLayout(new BorderLayout()); panel.add(chartPanel4, BorderLayout.CENTER); mainPanel.add(panel, new GridBagConstraints(1, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 20, 20), 0, 0)); JDialog dialog = new JDialog(new JFrame(), true); dialog.setTitle("?"); dialog.setSize(850, 650); dialog.getContentPane().add(mainPanel); dialog.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Overlay Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { public boolean isOptimizedDrawingEnabled() { return false; }//from w ww.j av a 2 s . c o m }; LayoutManager overlay = new OverlayLayout(panel); panel.setLayout(overlay); JButton button = new JButton("Small"); button.setMaximumSize(new Dimension(25, 25)); button.setBackground(Color.white); button.setAlignmentX(0.0f); button.setAlignmentY(0.0f); panel.add(button); button = new JButton("Medium"); button.setMaximumSize(new Dimension(50, 50)); button.setBackground(Color.gray); button.setAlignmentX(0.0f); button.setAlignmentY(0.0f); panel.add(button); button = new JButton("Large"); button.setMaximumSize(new Dimension(100, 100)); button.setBackground(Color.black); button.setAlignmentX(0.0f); button.setAlignmentY(0.0f); panel.add(button); frame.add(panel, BorderLayout.CENTER); frame.setSize(400, 300); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { final DefaultListModel<String> model = new DefaultListModel<>(); final JList<String> list = new JList<>(model); JFrame f = new JFrame(); model.addElement("A"); model.addElement("B"); model.addElement("C"); model.addElement("D"); model.addElement("E"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JPanel leftPanel = new JPanel(); JPanel rightPanel = new JPanel(); leftPanel.setLayout(new BorderLayout()); rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { int index = list.locationToIndex(e.getPoint()); Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = ""; if (text != null) newitem = text.trim(); else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); ListSelectionModel selmodel = list.getSelectionModel(); selmodel.setLeadSelectionIndex(index); }//from ww w .jav a 2 s . co m } } }); leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); leftPanel.add(new JScrollPane(list)); JButton removeall = new JButton("Remove All"); JButton add = new JButton("Add"); JButton rename = new JButton("Rename"); JButton delete = new JButton("Delete"); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = JOptionPane.showInputDialog("Add a new item"); String item = null; if (text != null) item = text.trim(); else return; if (!item.isEmpty()) model.addElement(item); } }); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index >= 0) model.remove(index); } }); rename.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index == -1) return; Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = null; if (text != null) { newitem = text.trim(); } else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); } } }); removeall.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { model.clear(); } }); rightPanel.add(add); rightPanel.add(rename); rightPanel.add(delete); rightPanel.add(removeall); rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); panel.add(leftPanel); panel.add(rightPanel); f.add(panel); f.setSize(350, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:List.java
public static void main(String[] args) { final DefaultListModel model = new DefaultListModel(); final JList list = new JList(model); JFrame f = new JFrame(); f.setTitle("JList models"); model.addElement("A"); model.addElement("B"); model.addElement("C"); model.addElement("D"); model.addElement("E"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JPanel leftPanel = new JPanel(); JPanel rightPanel = new JPanel(); leftPanel.setLayout(new BorderLayout()); rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { int index = list.locationToIndex(e.getPoint()); Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = ""; if (text != null) newitem = text.trim(); else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); ListSelectionModel selmodel = list.getSelectionModel(); selmodel.setLeadSelectionIndex(index); }/*from w w w. j a v a 2 s . com*/ } } }); leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); leftPanel.add(new JScrollPane(list)); JButton removeall = new JButton("Remove All"); JButton add = new JButton("Add"); JButton rename = new JButton("Rename"); JButton delete = new JButton("Delete"); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = JOptionPane.showInputDialog("Add a new item"); String item = null; if (text != null) item = text.trim(); else return; if (!item.isEmpty()) model.addElement(item); } }); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index >= 0) model.remove(index); } }); rename.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index == -1) return; Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = null; if (text != null) { newitem = text.trim(); } else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); } } }); removeall.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { model.clear(); } }); rightPanel.add(add); rightPanel.add(rename); rightPanel.add(delete); rightPanel.add(removeall); rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); panel.add(leftPanel); panel.add(rightPanel); f.add(panel); f.setSize(350, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }