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

📄 constraintspanel.java

📁 swing 教程,与大家分享一下,哈哈,希望大家多多指教
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		if(gridy == GridBagConstraints.RELATIVE)
			gridyCombo.setSelectedItem("RELATIVE");
		else
			gridyCombo.setSelectedIndex(gridy+1);
		repaint();
	}
	public void setGridheight(int gridheight) {
		if(gridheight == GridBagConstraints.RELATIVE)
			gridheightCombo.setSelectedItem("RELATIVE");
		else if(gridheight == GridBagConstraints.REMAINDER)
			gridheightCombo.setSelectedItem("REMAINDER");
		else	
			gridheightCombo.setSelectedIndex(gridheight+1);
	}
	public void setGridwidth(int gridwidth) {
		if(gridwidth == GridBagConstraints.RELATIVE)
			gridwidthCombo.setSelectedItem("RELATIVE");
		else if(gridwidth == GridBagConstraints.REMAINDER)
			gridwidthCombo.setSelectedItem("REMAINDER");
		else	
			gridwidthCombo.setSelectedIndex(gridwidth+1);
	}
	public int getGridx() {
		String x = (String)gridxCombo.getSelectedItem();
		int rv;

		if(x.equals("RELATIVE"))
			rv = GridBagConstraints.RELATIVE;
		else {
			rv = Integer.parseInt(
					(String)gridxCombo.getSelectedItem());
		}
		return rv;
	}
	public int getGridy() {
		String y = (String)gridyCombo.getSelectedItem();
		int rv;

		if(y.equals("RELATIVE"))
			rv = GridBagConstraints.RELATIVE;
		else {
			rv = Integer.parseInt(
					(String)gridyCombo.getSelectedItem());
		}
		return rv;
	}
	public int getGridwidth() {
		String width = (String)gridwidthCombo.getSelectedItem();
		int rv;

		if(width.equals("RELATIVE"))
			rv = GridBagConstraints.RELATIVE;
		else if(width.equals("REMAINDER"))
			rv = GridBagConstraints.REMAINDER;
		else {
			rv = Integer.parseInt(
					(String)gridwidthCombo.getSelectedItem());
		}
		return rv;
	}
	public int getGridheight() {
		String height = (String)gridheightCombo.getSelectedItem();
		int rv;

		if(height.equals("RELATIVE"))
			rv = GridBagConstraints.RELATIVE;
		else if(height.equals("REMAINDER"))
			rv = GridBagConstraints.REMAINDER;
		else {
			rv = Integer.parseInt(
					(String)gridheightCombo.getSelectedItem());
		}
		return rv;
	}
}
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)));
	}
	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");
	}
}

⌨️ 快捷键说明

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