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
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论














收起资源包目录





































































































共 2116 条
- 1
- 2
- 3
- 4
- 5
- 6
- 22
资源评论

- veesn2014-09-18源码给力给力
- 江湖狂2013-09-18还行,不错的资源。非常感谢!!

元明
- 粉丝: 3
- 资源: 2
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
