publicstaticvoid
addComponentsToPane(Container pane) {
pane.setLayout(
new
FlowLayout());
pane.add(
new
JButton(
"Button 1"
));
pane.add(
new
JButton(
"Button 2"
));
pane.add(
new
JButton(
"Button 3"
));
pane.add(
new
JButton(
"Long-Named Button 4"
));
pane.add(
new
JButton(
"5"
));
}
|
publicstaticvoid
addComponentsToPane(Container pane) {
JButton button =
new
JButton(
"Button 1 (PAGE_START)"
);
pane.add(button, BorderLayout.
PAGE_START
);
button =
new
JButton(
"Button 2 (CENTER)"
);
button.setPreferredSize(
new
Dimension(200, 100));
pane.add(button, BorderLayout.
CENTER
);
button =
new
JButton(
"Button 3 (LINE_START)"
);
pane.add(button, BorderLayout.
LINE_START
);
button =
new
JButton(
"Long-Named Button 4 (PAGE_END)"
);
pane.add(button, BorderLayout.
PAGE_END
);
button =
new
JButton(
"5 (LINE_END)"
);
pane.add(button, BorderLayout.
LINE_END
);
}
|
publicstaticvoid
addComponentsToPane(Container pane) {
JPanel xPanel =
new
JPanel();
xPanel.setLayout(
new
BoxLayout(xPanel, BoxLayout.
X_AXIS
));
addButtons(xPanel);
JPanel yPanel =
new
JPanel();
yPanel.setLayout(
new
BoxLayout(yPanel, BoxLayout.
Y_AXIS
));
addButtons(yPanel);
pane.add(yPanel, BorderLayout.
PAGE_START
);
pane.add(xPanel, BorderLayout.
PAGE_END
);
}
privatestaticvoid
addAButton(String text, Container container) {
JButton button =
new
JButton(text);
button.setAlignmentX(Component.
CENTER_ALIGNMENT
);
container.add(button);
}
privatestaticvoid
addButtons(Container container) {
addAButton(
"Button 1"
, container);
addAButton(
"Button 2"
, container);
addAButton(
"Button 3"
, container);
addAButton(
"Long-Named Button 4"
, container);
addAButton(
"5"
, container);
}
|
publicvoid
addComponentToPane(Container pane) {
final
JPanel contentPanel =
new
JPanel();
JPanel controlPanel =
new
JPanel();
final
CardLayout cardLayout=
new
CardLayout();;
pane.setLayout(
new
BorderLayout());
pane.add(contentPanel, BorderLayout.
CENTER
);
pane.add(controlPanel, BorderLayout.
PAGE_END
);
controlPanel.setLayout(
new
FlowLayout());
JButton[] b =
new
JButton[10];
for
(
int
i = 0; i < 10; i++) {
b[i] =
new
JButton(
"No."
+ i);
contentPanel.add(b[i]);
}
contentPanel.setLayout(cardLayout);
JButton nextButton =
new
JButton(
"next"
);
nextButton.addActionListener(
new
ActionListener(){
publicvoid
actionPerformed(ActionEvent e) {
cardLayout.next(contentPanel);
}});
controlPanel.add(nextButton);
}
|
publicstaticvoid
addComponentsToPane(Container pane) {
JButton[] buttons =
new
JButton[9];
pane.setLayout(
new
GridLayout(3, 3));
for
(
int
i = 0; i < buttons.
length
; i++) {
buttons[i] =
new
JButton(i +
""
);
pane.add(buttons[i]);
}
}
|
publicstaticvoid
addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(
new
GridBagLayout());
GridBagConstraints c =
new
GridBagConstraints();
button =
new
JButton(
"Button 1"
);
c.
fill
= GridBagConstraints.
HORIZONTAL
;
c.
gridx
= 0;
c.
gridy
= 0;
pane.add(button, c);
button =
new
JButton(
"Button 2"
);
c.
fill
= GridBagConstraints.
HORIZONTAL
;
c.
weightx
= 0.5;
c.
gridx
= 1;
c.
gridy
= 0;
pane.add(button, c);
button =
new
JButton(
"Button 3"
);
c.
fill
= GridBagConstraints.
HORIZONTAL
;
c.
weightx
= 0.5;
c.
gridx
= 2;
c.
gridy
= 0;
pane.add(button, c);
button =
new
JButton(
"Long-Named Button 4"
);
c.
fill
= GridBagConstraints.
HORIZONTAL
;
c.
ipady
= 40;
// make this component tall
c.
weightx
= 0.0;
c.
gridwidth
= 3;
c.
gridx
= 0;
c.
gridy
= 1;
pane.add(button, c);
button =
new
JButton(
"5"
);
c.
fill
= GridBagConstraints.
HORIZONTAL
;
c.
ipady
= 0;
// reset to default
c.
weighty
= 1.0;
// request any extra vertical space
c.
anchor
= GridBagConstraints.
PAGE_END
;
// bottom of space
c.
insets
=
new
Insets(10, 0, 0, 0);
// top padding
c.
gridx
= 1;
// aligned with button 2
c.
gridwidth
= 2;
// 2 columns wide
c.
gridy
= 2;
// third row
pane.add(button, c);
}
|
import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JTextField; public class GridBagLayoutFrame extends JFrame { private static final long serialVersionUID = 6819222900970457455L; private JPanel mainPanel = new JPanel(); private JButton addButton = new JButton(); private JButton leftButton = new JButton(); private JButton rightButton = new JButton(); private JLabel label = new JLabel(); private JTextField field = new JTextField(); private DefaultListModel leftModel = new DefaultListModel(); private DefaultListModel rightMOdel = new DefaultListModel(); private JList leftList = new JList(leftModel); private JList rightList = new JList(rightMOdel); public GridBagLayoutFrame(String title) { setTitle("GridBagLayoutFrameDemo"); setPreferredSize(new Dimension(600, 400)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initComponent(); addData(); pack(); setVisible(true); } private static void createAndShowGUI() { new GridBagLayoutFrame("GridBagLayoutFrameDemo"); } private void initComponent() { label.setText("添加选项:"); addButton.setText("添加"); leftList.setPreferredSize(new Dimension(150, 150)); rightList.setPreferredSize(leftList.getPreferredSize()); leftButton.setText("左"); rightButton.setText("右"); mainPanel.setBorder(BorderFactory.createTitledBorder("左右选择框")); mainPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; // 0行0列 c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0; c.weighty = 0; mainPanel.add(label, c); c.gridx++; c.weightx = 1; mainPanel.add(field, c); c.gridx++; c.weightx = 0; c.gridwidth = 1; c.gridheight = 1; // c.fill = GridBagConstraints.HORIZONTAL; mainPanel.add(addButton, c); c.gridx = 0; c.gridy = 1; c.weightx = 1; c.weighty = 1; c.gridwidth = 2; c.gridheight = 2; c.fill = GridBagConstraints.BOTH; mainPanel.add(leftList, c); c.gridx = 2; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0; c.weighty = 0.5; c.anchor = GridBagConstraints.SOUTH; c.fill = GridBagConstraints.HORIZONTAL; mainPanel.add(leftButton, c); c.gridx = 2; c.gridy = 2; c.anchor = GridBagConstraints.NORTH; c.fill = GridBagConstraints.HORIZONTAL; mainPanel.add(rightButton, c); c.gridx = 3; c.gridy = 1; c.gridwidth = 1; c.gridheight = 2; c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH; mainPanel.add(rightList, c); this.getContentPane().add(mainPanel); } private void addData() { addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addItem(); } }); leftButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { leftItem(); } }); rightButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { rightItem(); } }); } private void addItem() { if (field.getText() != null && !field.getText().equals("")) { ((DefaultListModel) leftList.getModel()) .addElement(field.getText()); field.setText(""); } } private void leftItem() { if (rightList.getSelectedIndex() != -1) { Object o = rightList.getSelectedValue(); ((DefaultListModel) rightList.getModel()).remove(rightList .getSelectedIndex()); ((DefaultListModel) leftList.getModel()).addElement(o); } } private void rightItem() { if (leftList.getSelectedIndex() != -1) { Object o = leftList.getSelectedValue(); ((DefaultListModel) leftList.getModel()).remove(leftList .getSelectedIndex()); ((DefaultListModel) rightList.getModel()).addElement(o); } } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
注:本人摘抄于http://zhangjunhd.blog.51cto.com/113473/128174