⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridbaglab.java

📁 java2图形设计卷1:awt 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import com.sun.java.swing.*;
import com.sun.java.swing.text.*;
import com.sun.java.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.io.*;

public class GridBagLab extends JFrame {
	static String lastFile = null;
	static GridBagLab gblframe;
	static ConstraintsPanel cp;
	static TextArea helpTextArea = 
					new TextArea(contentsOfFile("intro.txt"));

	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();

		gblframe = this;

		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();

		JPanel buttonsPanel = new JPanel();

		contentPane.setLayout(new BorderLayout());
		contentPane.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);
	}
	public static void main(String args[]) {
		final GridBagLab   buttonsFrame = new GridBagLab();
		final JFrame constraintsFrame = new JFrame("Constraints");
		final JFrame helpFrame = 
					new JFrame("GridBag Constraints Explained");

		Container contentPane = constraintsFrame.getContentPane();
		contentPane.add(cp = new ConstraintsPanel(buttonsFrame));
		constraintsFrame.pack();

		Dimension constraintsSize = constraintsFrame.getSize();

		buttonsFrame.setBounds(300,100,500,constraintsSize.height);
		buttonsFrame.setVisible(true);

		Point buttonsLocation = buttonsFrame.getLocation();
		Dimension buttonsSize = buttonsFrame.getSize();

		constraintsFrame.setLocation(
			buttonsLocation.x + buttonsSize.width,
			buttonsLocation.y);

		constraintsFrame.setVisible(true);

		Point loc = buttonsFrame.getLocation();
		Dimension size = buttonsFrame.getSize();
		helpFrame.setBounds(loc.x, loc.y + size.height, 
							size.width + 
							constraintsFrame.getSize().width, 300);

		helpTextArea.setEditable(false);
		helpTextArea.setFont(
			new Font("Times-Roman", Font.PLAIN, 12));
		helpFrame.getContentPane().add(helpTextArea, "Center");
		helpFrame.setVisible(true);

		buttonsFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				buttonsFrame.dispose();
				constraintsFrame.dispose();
				helpFrame.dispose();
			}
			public void windowClosed(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	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 JFrame {
	public ConstraintsDialog(GridBagLab test) {
		super("GridBagConstraints");
		getContentPane().add(new ConstraintsPanel(test));
	}
}
class ConstraintsPanel extends JPanel {
	GridBagLab					buttonsFrame = null;
	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(GridBagLab frame) {
		this.buttonsFrame = frame;

		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 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 tgbc = 
					GridBagLab.gblframe.getConstraints();

				tgbc.ipadx = getPadx();
				GridBagLab.gblframe.setConstraints(tgbc);
			}
		});
		ipadyField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				GridBagConstraints tgbc = 
					GridBagLab.gblframe.getConstraints();

				tgbc.ipady = getPady();
				GridBagLab.gblframe.setConstraints(tgbc);
			}
		});

		class IpadListener extends MouseAdapter {
			public void mouseEntered(MouseEvent event) {
				GridBagLab.gblframe.setHelpViewer("ipad.txt");
			}
		}
		IpadListener ipadListener = new IpadListener();

		ipadxLabel.addMouseListener(ipadListener);
		ipadxField.addMouseListener(ipadListener);
		ipadyLabel.addMouseListener(ipadListener);
		ipadyField.addMouseListener(ipadListener);
	}
	public void setPadx(int padX) {
		ipadxField.setText(Integer.toString(padX));
		repaint();
	}
	public void setPady(int padY) {
		ipadyField.setText(Integer.toString(padY));
		repaint();
	}
	public int getPadx() {
		return Integer.parseInt(ipadxField.getText());
	}
	public int getPady() {
		return Integer.parseInt(ipadyField.getText());
	}
}
class InsetsPanel extends JPanel {
	JTextField 	topField 	= new JTextField(3),
				leftField 	= new JTextField(3),
				bottomField = new JTextField(3),
				rightField 	= new JTextField(3);

	public InsetsPanel() {
		setLayout(new BorderLayout());
		add(topField, "North");
		add(leftField, "West");
		add(bottomField, "South");
		add(rightField, "East");

		topField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				updateInsets();
			}
		});
		leftField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				updateInsets();
			}
		});
		bottomField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				updateInsets();
			}
		});
		rightField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				updateInsets();
			}
		});
		setBorder(new CompoundBorder(
			BorderFactory.createTitledBorder("Insets"),
			BorderFactory.createEmptyBorder(10,10,10,10)));

		addMouseListener(new MouseAdapter() {	
			public void mouseEntered(MouseEvent event) {
				GridBagLab.gblframe.setHelpViewer("insets.txt");
			}
		});
	}
	public void setInsetsConstraints(Insets insets) {
		topField.setText(Integer.toString(insets.top));
		leftField.setText(Integer.toString(insets.left));
		bottomField.setText(Integer.toString(insets.bottom));
		rightField.setText(Integer.toString(insets.right));
	}
	public Insets getInsetsConstraint() {
		return new Insets(
			Integer.parseInt(topField.getText()),
			Integer.parseInt(leftField.getText()),
			Integer.parseInt(bottomField.getText()),
			Integer.parseInt(rightField.getText()));
	}
	void updateInsets() {
		GridBagConstraints tgbc =
					GridBagLab.gblframe.getConstraints();

		tgbc.insets = getInsetsConstraint();
		GridBagLab.gblframe.setConstraints(tgbc);
	}
}

class DisplayAreaPanel extends JPanel {
	JLabel	gridxLabel = new JLabel("gridx:"),
			gridyLabel = new JLabel("gridy:"),
			gridwidthLabel = new JLabel("gridwidth:"),
			gridheightLabel = new JLabel("gridheight:");

	JComboBox 	gridxCombo 		= new JComboBox(),
				gridyCombo 		= new JComboBox(),
				gridwidthCombo 	= new JComboBox(),
				gridheightCombo = new JComboBox();

	private void addToolTips() {
		gridxLabel.setToolTipText("grid x"); 
		gridyLabel.setToolTipText("grid y");
		gridwidthLabel.setToolTipText("width in grid cells");
		gridheightLabel.setToolTipText("height in grid cells");

		gridxCombo.setToolTipText("integer value");
		gridyCombo.setToolTipText("integer value");
		gridwidthCombo.setToolTipText("integer value");
		gridheightCombo.setToolTipText("integer value");
	}
	public DisplayAreaPanel() {
		GridBagLayout 		gbl = new GridBagLayout();
		GridBagConstraints 	gbc = new GridBagConstraints();

		setLayout(gbl);

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.HORIZONTAL;

		add(gridxLabel, gbc);
		add(Box.createHorizontalStrut(7), gbc);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		add(gridxCombo, gbc);

		gbc.gridwidth = 1;

		add(gridyLabel, gbc);
		add(Box.createHorizontalStrut(7), gbc);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		add(gridyCombo, gbc);
		add(Box.createVerticalStrut(10), gbc);

		gbc.gridwidth = 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -