📄 gridbaglab.java
字号:
class AnchorListener extends MouseAdapter {
public void mouseEntered(MouseEvent event) {
setHelpViewer("anchor.txt");
}
}
class WeightListener extends MouseAdapter {
public void mouseEntered(MouseEvent event) {
setHelpViewer("weight.txt");
}
}
class FillListener extends MouseAdapter {
public void mouseEntered(MouseEvent event) {
setHelpViewer("fill.txt");
}
}
class AnchorFillWeightPanel extends JPanel {
JLabel anchorLabel = new JLabel("Anchor:"),
fillLabel = new JLabel("Fill:"),
weightxLabel = new JLabel("weightx:"),
weightyLabel = new JLabel("weighty:");
JComboBox anchorCombo = new JComboBox(),
fillCombo = new JComboBox();
JComboBox weightxCombo = new JComboBox(),
weightyCombo = new JComboBox();
public AnchorFillWeightPanel() {
anchorCombo.addItem("NORTH");
anchorCombo.addItem("NORTHEAST");
anchorCombo.addItem("EAST");
anchorCombo.addItem("SOUTHEAST");
anchorCombo.addItem("SOUTH");
anchorCombo.addItem("SOUTHWEST");
anchorCombo.addItem("WEST");
anchorCombo.addItem("NORTHWEST");
anchorCombo.addItem("CENTER");
weightxCombo.addItem("0.0");
weightxCombo.addItem("0.1");
weightxCombo.addItem("0.2");
weightxCombo.addItem("0.25");
weightxCombo.addItem("0.3");
weightxCombo.addItem("0.4");
weightxCombo.addItem("0.5");
weightxCombo.addItem("0.6");
weightxCombo.addItem("0.7");
weightxCombo.addItem("0.75");
weightxCombo.addItem("0.8");
weightxCombo.addItem("0.9");
weightxCombo.addItem("1.0");
weightyCombo.addItem("0.0");
weightyCombo.addItem("0.1");
weightyCombo.addItem("0.2");
weightyCombo.addItem("0.25");
weightyCombo.addItem("0.3");
weightyCombo.addItem("0.4");
weightyCombo.addItem("0.5");
weightyCombo.addItem("0.6");
weightyCombo.addItem("0.7");
weightyCombo.addItem("0.75");
weightyCombo.addItem("0.8");
weightyCombo.addItem("0.9");
weightyCombo.addItem("1.0");
fillCombo.addItem("NONE");
fillCombo.addItem("HORIZONTAL");
fillCombo.addItem("VERTICAL");
fillCombo.addItem("BOTH");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
gbc.anchor = GridBagConstraints.NORTHWEST;
add(anchorLabel, gbc);
add(Box.createHorizontalStrut(10), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
add(anchorCombo, gbc);
gbc.weightx = 0;
add(Box.createVerticalStrut(3), gbc);
gbc.gridwidth = 1;
add(fillLabel, gbc);
add(Box.createHorizontalStrut(10), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
add(fillCombo, gbc);
gbc.weightx = 0;
add(Box.createVerticalStrut(13), gbc);
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
add(weightxLabel, gbc);
add(Box.createHorizontalStrut(10), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
add(weightxCombo, gbc);
gbc.weightx = 0;
add(Box.createVerticalStrut(3), gbc);
gbc.gridwidth = 1;
add(weightyLabel, gbc);
add(Box.createHorizontalStrut(10), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
add(weightyCombo, gbc);
gbc.weightx = 0;
gbc.gridwidth = 1;
setBorder(new CompoundBorder(
BorderFactory.createTitledBorder(
"Anchor, Fill and Weight"),
BorderFactory.createEmptyBorder(10,10,10,10)));
fillCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
int state = event.getStateChange();
if(state == ItemEvent.SELECTED) {
GridBagConstraints tgbc =
getConstraints();
tgbc.fill = getFill();
setConstraints(tgbc);
}
}
});
anchorCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
int state = event.getStateChange();
if(state == ItemEvent.SELECTED) {
GridBagConstraints tgbc =
getConstraints();
tgbc.anchor = getAnchor();
setConstraints(tgbc);
}
}
});
weightxCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
int state = event.getStateChange();
if(state == ItemEvent.SELECTED) {
GridBagConstraints tgbc =
getConstraints();
tgbc.weightx = (getWeightx()).doubleValue();
setConstraints(tgbc);
}
}
});
weightyCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
int state = event.getStateChange();
if(state == ItemEvent.SELECTED) {
GridBagConstraints tgbc =
getConstraints();
tgbc.weighty = (getWeighty()).doubleValue();
setConstraints(tgbc);
}
}
});
AnchorListener anchorListener = new AnchorListener();
WeightListener weightListener = new WeightListener();
FillListener fillListener = new FillListener();
anchorLabel.addMouseListener(anchorListener);
anchorCombo.addMouseListener(anchorListener);
weightxLabel.addMouseListener(weightListener);
weightyLabel.addMouseListener(weightListener);
weightxCombo.addMouseListener(weightListener);
weightyCombo.addMouseListener(weightListener);
fillLabel.addMouseListener(fillListener);
fillCombo.addMouseListener(fillListener);
}
public Double getWeightx() {
return Double.valueOf(
(String)weightxCombo.getSelectedItem());
}
public Double getWeighty() {
return Double.valueOf(
(String)weightyCombo.getSelectedItem());
}
public void setWeightx(Double d) {
weightxCombo.setSelectedItem(d.toString());
}
public void setWeighty(Double d) {
weightyCombo.setSelectedItem(d.toString());
}
public int getAnchor() {
String index = (String)anchorCombo.getSelectedItem();
int anchor = GridBagConstraints.NORTH;
if("NORTH".equals(index))
anchor = GridBagConstraints.NORTH;
else if("NORTHEAST".equals(index))
anchor = GridBagConstraints.NORTHEAST;
else if("NORTHWEST".equals(index))
anchor = GridBagConstraints.NORTHWEST;
else if("EAST".equals(index))
anchor = GridBagConstraints.EAST;
else if("SOUTHEAST".equals(index))
anchor = GridBagConstraints.SOUTHEAST;
else if("SOUTH".equals(index))
anchor = GridBagConstraints.SOUTH;
else if("SOUTHWEST".equals(index))
anchor = GridBagConstraints.SOUTHWEST;
else if("WEST".equals(index))
anchor = GridBagConstraints.WEST;
else if("CENTER".equals(index))
anchor = GridBagConstraints.CENTER;
return anchor;
}
public int getFill() {
String index = (String)fillCombo.getSelectedItem();
int fill = GridBagConstraints.NONE;
if("NONE".equals(index))
fill = GridBagConstraints.NONE;
else if("HORIZONTAL".equals(index))
fill = GridBagConstraints.HORIZONTAL;
else if("VERTICAL".equals(index))
fill = GridBagConstraints.VERTICAL;
else if("BOTH".equals(index))
fill = GridBagConstraints.BOTH;
return fill;
}
public void setAnchor(int anchor) {
if(anchor == GridBagConstraints.NORTH)
anchorCombo.setSelectedItem("NORTH");
else if(anchor == GridBagConstraints.NORTHEAST)
anchorCombo.setSelectedItem("NORTHEAST");
else if(anchor == GridBagConstraints.NORTHWEST)
anchorCombo.setSelectedItem("NORTHWEST");
else if(anchor == GridBagConstraints.SOUTH)
anchorCombo.setSelectedItem("SOUTH");
else if(anchor == GridBagConstraints.SOUTHEAST)
anchorCombo.setSelectedItem("SOUTHEAST");
else if(anchor == GridBagConstraints.SOUTHWEST)
anchorCombo.setSelectedItem("SOUTHWEST");
else if(anchor == GridBagConstraints.EAST)
anchorCombo.setSelectedItem("EAST");
else if(anchor == GridBagConstraints.WEST)
anchorCombo.setSelectedItem("WEST");
else if(anchor == GridBagConstraints.CENTER)
anchorCombo.setSelectedItem("CENTER");
}
public void setFill(int fill) {
if(fill == GridBagConstraints.NONE)
fillCombo.setSelectedItem("NONE");
else if(fill == GridBagConstraints.HORIZONTAL)
fillCombo.setSelectedItem("HORIZONTAL");
else if(fill == GridBagConstraints.VERTICAL)
fillCombo.setSelectedItem("VERTICAL");
else if(fill == GridBagConstraints.BOTH)
fillCombo.setSelectedItem("BOTH");
}
}
class AboutDialog extends JDialog {
public AboutDialog(JFrame frame) {
super(frame, "GridBagLab");
Container contentPane = getContentPane();
JButton button = new JButton("Ok");
contentPane.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
contentPane.add(new JLabel(
"A Swing application for exploring GridBagLayout"));
contentPane.add(Box.createVerticalStrut(20));
contentPane.add(new JLabel(
"Copyright 1997, Sabreware Inc.",
new ImageIcon("sabreware.gif"),
SwingConstants.LEFT));
contentPane.add(Box.createVerticalStrut(50));
contentPane.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
public static void main(String args[]) {
GJApp.launch(new GridBagLab(),
"GridBagLab",200,200,600,500);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
launch(f,title,x,y,w,h,null);
}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h,
String propertiesFilename) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
if(propertiesFilename != null) {
resources = ResourceBundle.getBundle(
propertiesFilename, Locale.getDefault());
}
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -