欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用JavaFX编写了一个进制小助手

程序员文章站 2022-03-30 22:00:47
...

效果如下:

使用JavaFX编写了一个进制小助手

动图效果:

使用JavaFX编写了一个进制小助手 

 使用JavaFX编写了一个进制小助手

 编写耗时:一个半小时。

一共有7个类,代码大概300-400行左右。

使用JavaFX编写了一个进制小助手

 部分代码:


public class Main extends Application
{
	public static void main(String[] args) throws Exception
	{		
		Application.launch(args);
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception 
	{
		Pane pane = new Pane();
		
		Value value = new Value();
		value.pane = pane;
		
		value.decimal = new Decimal(value);
		value.decimal.show();
		
		value.hex = new Hex(value);
		value.hex.show();
		
		value.displayResult = new DisplayResult(value);
		value.displayResult.show();
		
		CopyResult copyResult = new CopyResult(value);
		copyResult.show();
		
		HexSep hexSep = new HexSep(value);
		hexSep.show();
		
		// 背景颜色
		pane.setBackground(new Background(new BackgroundFill(Color.rgb(200, 200, 200), null, null)));
		
		// ---------------------------窗口----------------------------
		Scene scene = new Scene(pane, 350, 400);
		primaryStage.setTitle("进制小助手");
		primaryStage.setScene(scene);
		primaryStage.show();
		
		// 右上角点击关闭时, 结束子线程
		primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() 
		{
		    public void handle(WindowEvent event) 
		    {
		        System.exit(0);
		    }
		});
		
		// 两次ESC退出程序
		primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() 
		{
			int count = 0;
			
			@Override
			public void handle(KeyEvent event)
			{
				if (event.getCode() == KeyCode.ESCAPE)
				{
					++count;
					if (count == 2)
					{
						System.exit(0);
					}
				}
			}
		});
		
		primaryStage.setResizable(false);
		// -----------------------------------------------------------
	}
}
public class Decimal
{
	public Value value;
	
	public TextPane inputPrompt;
	public TextFieldPane inputBox;
	
	public Decimal(Value value)
	{
		this.value = value;
	}
	
	public void show()
	{
		inputPrompt = new TextPane(value.pane, "请输入10进制:");
		inputPrompt.setFont("Courier New", 16);
		inputPrompt.setLayoutXY(50, 100);
		inputPrompt.addToPane();
		
		inputBox = new TextFieldPane(value.pane, "", 100, 30);
		inputBox.setFont("Courier New", 16);
		inputBox.setLayoutXY(170, 80);
		inputBox.addToPane();
		inputBox.textField.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
		{
			@Override
			public void handle(MouseEvent event)
			{
				if (event.getClickCount() == 1)
				{
					value.hex.inputBox.textField.clear();
				}
				
				if (event.getClickCount() == 2)
				{
					inputBox.textField.clear();
				}
			}	
		});
		
		inputBox.textField.textProperty().addListener(new ChangeListener<String>()
		{
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
			{
				value.displayResult.calc(newValue, 10);
			}
		});
	}
}

public class Hex
{
	public Value value;
	
	public TextPane inputPrompt;
	public TextFieldPane inputBox;
	
	public Hex(Value value)
	{
		this.value = value;
	}
	
	public void show()
	{
		inputPrompt = new TextPane(value.pane, "请输入16进制:");
		inputPrompt.setFont("Courier New", 16);
		inputPrompt.setLayoutXY(50, 150);
		inputPrompt.addToPane();
		
		inputBox = new TextFieldPane(value.pane, "", 100, 30);
		inputBox.setFont("Courier New", 16);
		inputBox.setLayoutXY(170, 130);
		inputBox.addToPane();
		inputBox.textField.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
		{
			@Override
			public void handle(MouseEvent event)
			{
				if (event.getClickCount() == 1)
				{
					value.decimal.inputBox.textField.clear();
				}
				
				if (event.getClickCount() == 2)
				{
					inputBox.textField.clear();
				}
			}	
		});
		
		inputBox.textField.textProperty().addListener(new ChangeListener<String>()
		{
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
			{
				value.displayResult.calc(newValue, 16);
			}
		});
	}
}

 

相关标签: JavaFX