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

📄 converter.java

📁 本压缩文件中含有线程的控制
💻 JAVA
字号:
package labs;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Converter extends JFrame implements ActionListener{

	public static final int WIDTH = 300;

	public static final int HEIGHT = 200;

	public static final int HORIZONTAL_STRUT_SIZE = 15;

	public static final int VERTICAL_STRUT_SIZE = 10;

	private JLabel decimalLabel;

	private JLabel binaryLabel;

	private JTextField decimalField;

	private JTextField binaryField;

	private JButton convertToB, convertToD, clear;

	public Converter() {
	
		setTitle("Converter");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(300, 200);
		Container contentPane = this.getContentPane();

		//decimal box
		Box decimalBox = Box.createHorizontalBox();
		Component horizontalStrut = Box
				.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		decimalBox.add(horizontalStrut);
		decimalLabel = new JLabel("十进制数:");
		decimalBox.add(decimalLabel);
		
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		decimalBox.add(horizontalStrut);		
		
		decimalField = new JTextField(15);
		decimalBox.add(decimalField);
		
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		decimalBox.add(horizontalStrut);
		
		//binary box
		Box binaryBox = Box.createHorizontalBox();
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		binaryBox.add(horizontalStrut);
		binaryLabel = new JLabel("二进制数:");
		binaryBox.add(binaryLabel);
		
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		binaryBox.add(horizontalStrut);		
		
		binaryField = new JTextField(15);
		binaryBox.add(binaryField);
		
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		binaryBox.add(horizontalStrut);
		
		//button box
		Box buttonBox = Box.createHorizontalBox();
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		buttonBox.add(horizontalStrut);
		convertToB = new JButton("转二进制");
		convertToB.addActionListener(this);
		buttonBox.add(convertToB);
		
		
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		buttonBox.add(horizontalStrut);
		convertToD = new JButton("转十进制");
		convertToD.addActionListener(this);
		buttonBox.add(convertToD);
		
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		buttonBox.add(horizontalStrut);
		clear = new JButton("清除");
		clear.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				decimalField.setText("");
				binaryField.setText("");
			}
			
			
		});
		buttonBox.add(clear);
		
		horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
		buttonBox.add(horizontalStrut);
		

		//main box
		Box verticalBox = Box.createVerticalBox();
		Component verticalStrut = Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
		verticalBox.add(verticalStrut);	
		
		verticalBox.add(decimalBox);
		
		verticalStrut = Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
		verticalBox.add(verticalStrut);	
		verticalBox.add(binaryBox);
		
		verticalStrut = Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
		verticalBox.add(verticalStrut);	
		verticalBox.add(buttonBox);
		
		verticalStrut = Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
		verticalBox.add(verticalStrut);	
		
		//
		
		contentPane.add(verticalBox);

	}
	
	public void actionPerformed(ActionEvent e){
		String command = e.getActionCommand();
		if(command.equals("转二进制")){
			String str = decimalField.getText();
			int i;
			try{
				i = Integer.parseInt(str);
				
				binaryField.setText(this.toBinary(i));
			}catch(NumberFormatException ex){
				binaryField.setText("Illegal Input!!");
			}
		}else if(command.equals("转十进制")){
			
		}
	}
	
	//将参数整数转化为二进制
	public String toBinary(int d){
		StringBuffer strB = new StringBuffer();
		String str = "";
		for(; d/2!=0; d = d/2){
			strB.append(d%2);			
		}
		strB.append(d%2);
		for(int i = strB.length() - 1; i >= 0; i --){
			str += strB.charAt(i);
		}
		return str;
	}
	
	//将参数二进制转化为十进制
	public int toDecimal(){
		return 0;
	}

	
	public static void main(String[] args){
		Converter c = new Converter();
		c.pack();
		c.setVisible(true);
	}
	
	
	
	
}

⌨️ 快捷键说明

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