edgeframe.java

来自「petrinets小程序」· Java 代码 · 共 130 行

JAVA
130
字号
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

import javax.swing.JFrame;


public class EdgeFrame extends JFrame{

	Panel Input, Buttons;
	static edge actual;
	TextField WeightField;
	Checkbox Negated;
	int WeightBackup;
	
	public EdgeFrame(edge e){
		
		super("Edges' characteristic");
		
		this.setLayout(new BorderLayout());
		
		actual = e;
	
		this.add("North", new Label("Edge"));
		
		Input = new Panel();
		Input.setLayout(new GridLayout(0,2,2,2));
		WeightBackup = actual.getWeight();
		WeightField = new TextField(Integer.toString(WeightBackup), 1);
		Negated = new Checkbox("Negated");
		Negated.setState(actual.isNegated());
		Input.add(new Label(""));
		Input.add(new Label(""));
		Input.add(new Label("Weight",Label.RIGHT));
		Input.add(WeightField);
		Input.add(new Label(""));
		Input.add(Negated);
		this.add("Center", Input);
		
		Buttons = new Panel();
		Buttons.setLayout(new FlowLayout());
		Buttons.add(new Button("Ok"));
		Buttons.add(new Button("Cancel"));
		this.add("South",Buttons);
		this.pack();
		this.resize(250,180);
		testModifyable();
//		move(parent.location().x + 50, parent.location().y + 50);
		this.setResizable(false);
	}
	

	public static void main(String[] args) {
		
		EdgeFrame application = new EdgeFrame(actual);
		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
	}

	

	private void testModifyable(){
		
		if (actual.isNegated()) {
	          WeightField.setText(Integer.toString(1));
	          WeightField.disable();
	      } else {
	          WeightField.enable();
	      }
		// TODO Auto-generated method stub
		
	}

	public void setEdge(edge e){
		actual = e;
		WeightBackup = actual.getWeight();
		WeightField.setText(Integer.toString(WeightBackup));
		Negated.setState(actual.isNegated());
		testModifyable();
	}
	
	public boolean handleEvent(Event e){
		switch (e.id) {
		case Event.ACTION_EVENT:
			if (e.target instanceof Checkbox) {
				if (! Negated.getState()) {
					WeightField.setText(Integer.toString(WeightBackup));
					WeightField.enable();
				} 
				else {
					WeightBackup = Integer.parseInt(WeightField.getText());
					WeightField.setText(Integer.toString(1));
					WeightField.disable();
				}
				return false;
			}
			if (e.target instanceof Button) {
				if (((String)e.arg).equals("Ok")) {
					try {
						actual.setWeight(Integer.parseInt(WeightField.getText()));
					}
					catch (NumberFormatException n) {}
					actual.setNegated(Negated.getState());
					this.hide();
					return true;
				}
				if (((String)e.arg).equals("Cancel")) {
					this.hide();
					return true;
				}
				return false;
			}
			default:
				return super.handleEvent(e);
		}
	}
	   
	/**
	 * 
	 */
	//private static final long serialVersionUID = 1L;
	
}

⌨️ 快捷键说明

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