欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

dimensionsupport.java

PIY(Program It Yourself)是一个基于Java的应用程序开发环境
JAVA
字号:
package piy.support;

import java.awt.event.*;
import java.awt.*;
import piy.*;
import javax.swing.*;
import java.awt.Dimension;

/**
* Support for properties of Point type.
* @author David Vivash
* @version 1.0, 08/02/01
*/
public class DimensionSupport extends ClassSupport implements KeyListener
{

	private Dimension dimension;
	private JTextField widthField;
	private JTextField heightField;


	public DimensionSupport(Property property) {
		super(property);

		//Make sure everything is correctly typed
		if ((property.getValue() == null) || (property.getType() != Dimension.class)
								|| (property.getValue().getClass() != Dimension.class))
			property.setValue(new Dimension(0,0));


		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

		setLayout(gridbag);
		c.fill = GridBagConstraints.HORIZONTAL;
		Insets pad = new Insets(0,4,0,0);
		Insets noPad = new Insets(0,0,0,0);


		c.ipadx = 8; c.ipady = 8;
		c.anchor = GridBagConstraints.EAST;

		c.gridx = 0; c.weightx = 0; c.insets = pad;

		JLabel label = new JLabel("Width");
		gridbag.setConstraints(label, c);
		add(label);
			
		dimension = (Dimension)property.getValue();
		if (dimension == null) {
			dimension = new Dimension(0, 0);
			property.setValue(dimension);
		}
		

		widthField = new JTextField(""+dimension.getWidth());
		heightField = new JTextField(""+dimension.getHeight());

		c.gridx = 1; c.weightx = 1.0; c.insets = noPad;
		gridbag.setConstraints(widthField , c);
		add(widthField);
	
		c.gridx = 0; c.weightx = 0; c.insets = pad; c.gridy = 1;

		JLabel label2 = new JLabel("Height");
		gridbag.setConstraints(label2, c);
		add(label2);

		c.gridx = 1; c.weightx = 1.0; c.insets = noPad;
		gridbag.setConstraints(heightField , c);
		add(heightField);

		validate();

		widthField.addKeyListener(this);
		heightField.addKeyListener(this);
	}

	public static Class getSupportedClass() { return Dimension.class; }

	public Dimension getMinimumSize() { return new Dimension(10, 48); }
	public Dimension getPreferredSize() { return new Dimension(100, 48); }
	public Dimension getMaximumSize() { return new Dimension(10000, 80); }

	//--------- KeyListener methods ------------
	public void keyPressed(KeyEvent e) {	/* ignore */ }
	public void keyTyped(KeyEvent e) { 		/* ignore */ }

	public void keyReleased(KeyEvent e) {	
		try {
			String widthText = widthField.getText();
			String heightText = heightField.getText();
			double widthVal=0, heightVal=0;

			if (!(widthText.equals("") || widthText.equals("-")))
				widthVal = Double.parseDouble(widthText);
				
			if (!(heightText.equals("") || heightText.equals("-")))
				heightVal = Double.parseDouble(heightText);

			property.setValue(new Dimension((int)widthVal, (int)heightVal));
			ProjectHandler.getInstance().projectChanged();			

		} catch (NumberFormatException exception) {
			Dimension dimension = (Dimension)property.getValue();
			widthField.setText(""+dimension.getWidth());
			heightField.setText(""+dimension.getHeight());
		}
	}

}

⌨️ 快捷键说明

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