📄 gridbaglab.java
字号:
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.io.*;
import java.util.*;
public class GridBagLab extends JFrame {
static String lastFile = null;
static ConstraintsPanel cp;
static JTextArea helpTextArea =
new JTextArea(contentsOfFile("intro.txt"));
JDesktopPane desktop = new JDesktopPane();
JInternalFrame buttonsFrame =
new JInternalFrame("Buttons", true, false, true, false);
JInternalFrame constraintsFrame =
new JInternalFrame("Constraints", false, false, true, true);
JInternalFrame helpFrame =
new JInternalFrame("GridBag Constraints Explained", true, false, true, true);
final GridBagLayout gbl = new GridBagLayout();
ButtonGroup group = new ButtonGroup();
JToggleButton selected = null;
JToggleButton oneButton = new JToggleButton(),
twoButton = new JToggleButton(),
threeButton = new JToggleButton(),
fourButton = new JToggleButton(),
fiveButton = new JToggleButton(),
sixButton = new JToggleButton(),
sevenButton = new JToggleButton(),
eightButton = new JToggleButton(),
nineButton = new JToggleButton(),
tenButton = new JToggleButton();
public GridBagLab() {
super("GridBag Lab");
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem aboutItem = new JMenuItem("about ...");
JMenuItem quitItem = new JMenuItem("quit");
final AboutDialog aboutDialog = new AboutDialog(this);
fileMenu.add(aboutItem);
fileMenu.add(quitItem);
mb.add(fileMenu);
setJMenuBar(mb);
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Point loc = getLocation();
aboutDialog.pack();
aboutDialog.setLocation(loc.x + 100, loc.y + 100);
aboutDialog.setVisible(true);
}
});
quitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
GridBagConstraints gbc = new GridBagConstraints();
ButtonListener listener = new ButtonListener();
oneButton.setIcon(new ImageIcon("gifs/one.gif"));
oneButton.addActionListener(listener);
twoButton.setIcon(new ImageIcon("gifs/two.gif"));
twoButton.addActionListener(listener);
threeButton.setIcon(new ImageIcon("gifs/three.gif"));
threeButton.addActionListener(listener);
fourButton.setIcon(new ImageIcon("gifs/four.gif"));
fourButton.addActionListener(listener);
fiveButton.setIcon(new ImageIcon("gifs/five.gif"));
fiveButton.addActionListener(listener);
sixButton.setIcon(new ImageIcon("gifs/six.gif"));
sixButton.addActionListener(listener);
sevenButton.setIcon(new ImageIcon("gifs/seven.gif"));
sevenButton.addActionListener(listener);
eightButton.setIcon(new ImageIcon("gifs/eight.gif"));
eightButton.addActionListener(listener);
nineButton.setIcon(new ImageIcon("gifs/nine.gif"));
nineButton.addActionListener(listener);
tenButton.setIcon(new ImageIcon("gifs/ten.gif"));
tenButton.addActionListener(listener);
oneButton.setFocusPainted(false);
twoButton.setFocusPainted(false);
threeButton.setFocusPainted(false);
fourButton.setFocusPainted(false);
fiveButton.setFocusPainted(false);
sixButton.setFocusPainted(false);
sevenButton.setFocusPainted(false);
eightButton.setFocusPainted(false);
nineButton.setFocusPainted(false);
tenButton.setFocusPainted(false);
group.add(oneButton);
group.add(twoButton);
group.add(threeButton);
group.add(fourButton);
group.add(fiveButton);
group.add(sixButton);
group.add(sevenButton);
group.add(eightButton);
group.add(nineButton);
group.add(tenButton);
Container contentPane = getContentPane();
desktop.add(buttonsFrame);
desktop.add(constraintsFrame);
desktop.add(helpFrame);
contentPane.setLayout(new BorderLayout());
contentPane.add(desktop, "Center");
JPanel buttonsPanel = new JPanel();
buttonsFrame.getContentPane().add(buttonsPanel, "Center");
buttonsPanel.setLayout(gbl);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
buttonsPanel.add(oneButton, gbc);
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
buttonsPanel.add(twoButton, gbc);
buttonsPanel.add(threeButton, gbc);
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
buttonsPanel.add(fourButton, gbc);
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 1;
buttonsPanel.add(fiveButton, gbc);
buttonsPanel.add(sixButton, gbc);
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
buttonsPanel.add(sevenButton, gbc);
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 2;
buttonsPanel.add(eightButton, gbc);
buttonsPanel.add(nineButton, gbc);
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
buttonsPanel.add(tenButton, gbc);
Container constraintsPane = constraintsFrame.getContentPane();
constraintsPane.add(cp = new ConstraintsPanel());
//constraintsFrame.pack();
constraintsFrame.setSize(250,600);
Dimension constraintsSize = constraintsFrame.getSize();
setBounds(300,100,500, constraintsSize.height);
Point buttonsLocation = buttonsFrame.getLocation();
Dimension buttonsSize = buttonsFrame.getSize();
buttonsFrame.setBounds(0,0,350,350);
helpFrame.setBounds(0,352,350+constraintsSize.width,buttonsFrame.getSize().height+2);
helpTextArea.setWrapStyleWord(true);
helpTextArea.setEditable(false);
helpTextArea.setFont(
new Font("Times-Roman", Font.PLAIN, 12));
helpFrame.getContentPane().add(
new JScrollPane(helpTextArea),
"Center");
}
public void setHelpViewer(String filename) {
if(lastFile == null || !lastFile.equals(filename)) {
helpTextArea.setText(contentsOfFile(filename));
lastFile = filename;
}
}
public void setConstraints(GridBagConstraints gbc) {
if(selected != null) {
gbl.setConstraints(selected, gbc);
selected.invalidate();
validate();
}
}
public GridBagConstraints getConstraints() {
GridBagConstraints gbc = null;
if(selected != null)
gbc = gbl.getConstraints(selected);
return gbc;
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
selected = (JToggleButton)e.getSource();
cp.setConstraints(gbl.getConstraints(selected));
}
}
static String contentsOfFile(String filename) {
String s = new String();
File f;
char[] buff = new char[50000];
InputStream is;
InputStreamReader reader;
URL url;
try {
f = new File(filename);
reader = new FileReader(f);
int nch;
while ((
nch = reader.read(buff, 0, buff.length)) != -1) {
s = s + new String(buff, 0, nch);
}
}
catch (java.io.IOException ex) {
s = "Could not load file: " + filename;
}
return s;
}
class ConstraintsDialog extends JInternalFrame {
public ConstraintsDialog(GridBagLab test) {
super("GridBagConstraints");
getContentPane().add(new ConstraintsPanel());
}
}
class ConstraintsPanel extends JPanel {
AnchorFillWeightPanel afpanel = new AnchorFillWeightPanel();
DisplayAreaPanel dpanel = new DisplayAreaPanel();
PaddingPanel ppanel = new PaddingPanel();
InsetsPanel ipanel = new InsetsPanel();
String dpaneltip = "display area attributes",
afpaneltip = "component attributes",
ppaneltip = "padding";
public ConstraintsPanel() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(dpanel);
add(Box.createVerticalStrut(15));
add(afpanel);
add(Box.createVerticalStrut(15));
add(ppanel);
add(Box.createVerticalStrut(15));
add(ipanel);
add(Box.createVerticalStrut(15));
}
public void setConstraints(GridBagConstraints gbc) {
afpanel.setAnchor(gbc.anchor);
afpanel.setFill(gbc.fill);
afpanel.setWeightx(new Double(gbc.weightx));
afpanel.setWeighty(new Double(gbc.weighty));
dpanel.setGridx(gbc.gridx);
dpanel.setGridy(gbc.gridy);
dpanel.setGridwidth(gbc.gridwidth);
dpanel.setGridheight(gbc.gridheight);
ppanel.setPadx(gbc.ipadx);
ppanel.setPady(gbc.ipady);
ipanel.setInsetsConstraints(gbc.insets);
}
public GridBagConstraints getConstraints() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = afpanel.getAnchor();
gbc.fill = afpanel.getFill();
gbc.gridx = dpanel.getGridx();
gbc.gridy = dpanel.getGridy();
gbc.gridwidth = dpanel.getGridwidth();
gbc.gridheight = dpanel.getGridheight();
gbc.weightx = (afpanel.getWeightx()).doubleValue();
gbc.weighty = (afpanel.getWeighty()).doubleValue();
gbc.ipadx = ppanel.getPadx();
gbc.ipady = ppanel.getPady();
gbc.insets = ipanel.getInsetsConstraint();
return gbc;
}
}
class IpadListener extends MouseAdapter {
public void mouseEntered(MouseEvent event) {
setHelpViewer("ipad.txt");
}
}
class PaddingPanel extends JPanel {
JLabel ipadxLabel = new JLabel("ipadx:"),
ipadyLabel = new JLabel("ipady:");
JTextField ipadxField = new JTextField(3),
ipadyField = new JTextField(3);
int padX, padY;
public PaddingPanel() {
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
gbc.anchor = GridBagConstraints.NORTHWEST;
add(ipadxLabel, gbc);
add(Box.createHorizontalStrut(10), gbc);
add(ipadxField, gbc);
add(Box.createHorizontalStrut(20), gbc);
add(ipadyLabel, gbc);
add(Box.createHorizontalStrut(10), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.0;
add(ipadyField, gbc);
setBorder(new CompoundBorder(
BorderFactory.createTitledBorder("Internal Padding"),
BorderFactory.createEmptyBorder(10,10,10,10)));
ipadxField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
GridBagConstraints c = getConstraints();
c.ipadx = getPadx();
setConstraints(c);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -