Re: How many panels can I add to a JFrame
RedGrittyBrick wrote:
danny wrote:
Oh no! I don't know what happened I did it ok and know when I send it
look what happened.
Is this what you were aiming for?
+---------------------------------------------+--------------+
| Label 1 (borderL center) |(borderL.east)|
| | |
| +-----+ + check +-----+ (.) radio +-----+ | |button 1| |
| | | | pic | | pic | | |
| | pic | + check | | (.) radio | | | |button 2| |
| | | | | | | | |
| +-----+ + check +-----+ (.) radio +-----+ | |button 3| |
| | |
+---------------------------------------------+ |button 4| |
| label |combo box| | check box| | |
| | |
+---------------------------------------------+--------------+
For fun, I just did a comparison of MigLayout vs nested simple Layouts
~28 lines for a single MigLayout (excluding blank lines)
~52 lines for nested layouts.
I'll let someone else show how to do it in a single GridBagLayout :-)
Here's the panel constructor using MigLayout ...
---------------------------------------------------------------
Danny() {
setLayout(new MigLayout("wrap",
"[grow][][grow][][grow][]", "[][grow][]"));
add(new JLabel("Label 1"), "span 5");
add(new JButton("Button 1"), "span 1 3, split 4, flowy");
add(new JButton("Button 2"));
add(new JButton("Button 3"));
add(new JButton("Button 4"));
add(new Pic("Pic1"), "grow");
JRadioButton radio1 = new JRadioButton("Radio 1");
JRadioButton radio2 = new JRadioButton("Radio 2");
JRadioButton radio3 = new JRadioButton("Radio 3");
ButtonGroup group = new ButtonGroup();
group.add(radio1);
group.add(radio2);
group.add(radio3);
add(radio1, "split 3, flowy");
add(radio2);
add(radio3);
add(new Pic("Pic2"), "grow");
add(new JCheckBox("Check 1"), "split 3, flowy");
add(new JCheckBox("Check 2"));
add(new JCheckBox("Check 3"));
add(new Pic("Pic3"), "grow");
add(new JLabel("label"));
add(new JComboBox(new String[] {"One","Two"}));
add(new JCheckBox("Check Box"));
}
---------------------------------------------------------------
Here's the panel constructor using nested simple layouts ...
---------------------------------------------------------------
Danny() {
setLayout(new BorderLayout());
JLabel pic1Label = new Pic("Pic1");
JLabel pic2Label = new Pic("Pic2");
JLabel pic3Label = new Pic("Pic3");
JPanel checkPanel = new JPanel();
checkPanel.setLayout(new BoxLayout(checkPanel,
BoxLayout.PAGE_AXIS));
checkPanel.add(new JCheckBox("Check 1"));
checkPanel.add(new JCheckBox("Check 2"));
checkPanel.add(new JCheckBox("Check 3"));
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new BoxLayout(radioPanel,
BoxLayout.PAGE_AXIS));
JRadioButton radio1 = new JRadioButton("Radio 1");
JRadioButton radio2 = new JRadioButton("Radio 2");
JRadioButton radio3 = new JRadioButton("Radio 3");
ButtonGroup group = new ButtonGroup();
group.add(radio1);
group.add(radio2);
group.add(radio3);
radioPanel.add(radio1);
radioPanel.add(radio2);
radioPanel.add(radio3);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel,
BoxLayout.LINE_AXIS));
centerPanel.add(pic1Label);
centerPanel.add(checkPanel);
centerPanel.add(pic2Label);
centerPanel.add(radioPanel);
centerPanel.add(pic3Label);
JPanel bottomPanel = new JPanel();
bottomPanel.setBorder(BorderFactory.createRaisedBevelBorder());
bottomPanel.setLayout(new FlowLayout());
bottomPanel.add(new JLabel("label"));
bottomPanel.add(new JComboBox(new String[] {"One","Two"}));
bottomPanel.add(new JCheckBox("Check Box"));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
leftPanel.add(new JLabel("Label 1"), BorderLayout.NORTH);
leftPanel.add(centerPanel, BorderLayout.CENTER);
leftPanel.add(bottomPanel, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createRaisedBevelBorder());
buttonPanel.setLayout(new BoxLayout(buttonPanel,
BoxLayout.PAGE_AXIS));
buttonPanel.add(Box.createVerticalGlue());
buttonPanel.add(new JButton("Button 1"));
buttonPanel.add(new JButton("Button 2"));
buttonPanel.add(new JButton("Button 3"));
buttonPanel.add(new JButton("Button 4"));
buttonPanel.add(Box.createVerticalGlue());
add(leftPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.EAST);
}
---------------------------------------------------------------
Here's the surrounding code needed to make a SSCCE
---------------------------------------------------------------
public class Danny extends JPanel{
// insert Danny constructor here
class Pic extends JLabel {
Pic(String caption) {
setText(caption);
setOpaque(true);
setHorizontalAlignment(SwingConstants.CENTER);
setBackground(Color.GREEN);
Dimension size = new Dimension(100,100);
setMinimumSize(size);
setPreferredSize(size);
setMaximumSize(new Dimension(1000,1000));
}
}
public static void main (String[] args) {
SwingUtilities.invokeLater( new Runnable(){
public void run() {
JFrame f = new JFrame("Danny's Layout Problem");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Danny());
f.pack();
f.setVisible(true);
}
});
}
}
---------------------------------------------------------------