自己写的一个简单的Java GUI源码

这是我自己写的一个Java GUI的小例子,虽然用Java做GUI的可能不是很多,但是这是我们的一个实验,所以我就做了一下,觉得还挺好玩的。源码等作为我的第一篇博客传上来,只为那些有类似实验但是不想浪费时间的同学提供一点方便。作为一个弱弱的晚辈,我的源码肯定有很多错误和不足的地方,希望大家看到之后能够及时批评指正。正是因为批评我们才能相互学习相互进步嘛

这个程序的作用是定制Moto X的,哈哈,这让我过了一把定制的瘾啊!

哦,对了,这个要装SWT Designer才行的

自己写的一个简单的Java GUI源码_第1张图片

自己写的一个简单的Java GUI源码_第2张图片

自己写的一个简单的Java GUI源码_第3张图片


GUI.java
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;



public class GUI{

	protected Shell shlMotoX;
	//JButton button;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			GUI window = new GUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlMotoX.open();
		shlMotoX.layout();
		
		while (!shlMotoX.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlMotoX = new Shell();
		shlMotoX.setImage(SWTResourceManager.getImage("Moto Logo2.jpg"));
		shlMotoX.setTouchEnabled(true);
		shlMotoX.setSize(450, 450);
		shlMotoX.setText("Design Your Own Moto X");
		
		Label lblWelcom = new Label(shlMotoX, SWT.NONE);
		lblWelcom.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblWelcom.setTouchEnabled(true);
		lblWelcom.setBounds(10, 22, 422, 310);
		lblWelcom.setText("Hello!\r\nWelcome to use this program!\r\nWe will help you to design your own Moto X"
						+ "\r\n\r\nParts you can customize include:\r\n\tCPU Cores: x2/x4\r\n\tRAM: 1GB/2GB/3GB"
						+ "\r\n\tROM: 16GB/32GB\r\n\tBattery: 2000mAh/2500mAh"
						+ "\r\n\tFront panel color: total of 18 colors\r\n\tBehind panel color: total of 18 colors and 2 materials"
						+ "\r\n\tThe side frame button color: total of 18 colors\r\n\tYou can sign your name or a Sentence on the behind shell..."
						+ "\r\n\r\nWish you a pleasant use!"
						+ "\r\n\t\t\t\t-----motorola, A Google Company");
		
		Button btnDesignYoursNow = new Button(shlMotoX, SWT.NONE);
		btnDesignYoursNow.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				shlMotoX.close();
				Designer.main(null);
			}
		});
		btnDesignYoursNow.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.NORMAL));
		btnDesignYoursNow.setBounds(143, 346, 157, 35);
		btnDesignYoursNow.setText("Design yours now!");
		
		
	}
}



Designer.java

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Designer {

	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;
	private Text text_3;
	
	private String CPUCores;
	private String RAM;
	private String ROM;
	private String Battery;
	private String frontColor;
	private String behindShellMaterial;
	private String behindColor;
	private String words;
	
	private String name;
	private String phoneNumber;
	private String address;
	private boolean flag = false;
	
	private int totalPrice = 99;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Designer window = new Designer();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage("Moto Logo2.jpg"));
		shell.setSize(500, 500);
		shell.setText("Design Your Own Moto X");
		
		Label lblNowYouCan = new Label(shell, SWT.NONE);
		lblNowYouCan.setForeground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));
		lblNowYouCan.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.BOLD));
		lblNowYouCan.setBounds(17, 10, 291, 23);
		lblNowYouCan.setText("Now, you can make your own moto X:");
		
		//CPU Cores
		Composite composite_4 = new Composite(shell, SWT.NONE);
		composite_4.setBounds(17, 39, 164, 64);
				
		Label lblCpuCores = new Label(composite_4, SWT.NONE);
		lblCpuCores.setText("CPU Cores:");
		lblCpuCores.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblCpuCores.setBounds(0, 15, 69, 23);
		
		Button btnX = new Button(composite_4, SWT.RADIO);
		btnX.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				CPUCores = "x2";
				totalPrice += 50;
			}
		});
		btnX.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btnX.setBounds(75, 16, 79, 17);
		btnX.setText("x2");
		
		Button btnX_1 = new Button(composite_4, SWT.RADIO);
		btnX_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				CPUCores = "x4";
				totalPrice += 80;
			}
		});
		btnX_1.setText("x4");
		btnX_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btnX_1.setBounds(75, 37, 79, 17);
		
		//RAM
		Composite composite_3 = new Composite(shell, SWT.NONE);
		composite_3.setBounds(17, 109, 164, 88);
				
		Label lblRam = new Label(composite_3, SWT.NONE);
		lblRam.setText("RAM:");
		lblRam.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblRam.setBounds(0, 10, 43, 27);
				
		Button btngb_2 = new Button(composite_3, SWT.RADIO);
		btngb_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				RAM = "1GB";
				totalPrice += 20;
			}
		});
		btngb_2.setText("1GB");
		btngb_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btngb_2.setBounds(75, 11, 79, 17);
				
		Button btngb_3 = new Button(composite_3, SWT.RADIO);
		btngb_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				RAM = "2GB";
				totalPrice += 40;
			}
		});
		btngb_3.setText("2GB");
		btngb_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btngb_3.setBounds(75, 34, 79, 17);
				
		Button btngb_4 = new Button(composite_3, SWT.RADIO);
		btngb_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				RAM = "3GB";
				totalPrice += 50;
			}
		});
		btngb_4.setText("3GB");
		btngb_4.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btngb_4.setBounds(75, 57, 79, 17);
		
		//ROM
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(17, 206, 164, 56);
		
		Label lblRow = new Label(composite, SWT.NONE);
		lblRow.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblRow.setBounds(0, 10, 43, 27);
		lblRow.setText("ROM:");
		
		Button btngb = new Button(composite, SWT.RADIO);
		btngb.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ROM = "16GB";
				totalPrice += 20;
			}
		});
		btngb.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btngb.setBounds(75, 11, 79, 17);
		btngb.setText("16GB");
		
		Button btngb_1 = new Button(composite, SWT.RADIO);
		btngb_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ROM = "32GB";
				totalPrice += 40;
			}
		});
		btngb_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btngb_1.setBounds(75, 34, 79, 17);
		btngb_1.setText("32GB");
		
		
		//Battery
		Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setBounds(17, 268, 164, 64);
		
		Label lblBattery = new Label(composite_1, SWT.NONE);
		lblBattery.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblBattery.setBounds(0, 10, 55, 23);
		lblBattery.setText("Battery:");
		
		Button btnmah = new Button(composite_1, SWT.RADIO);
		btnmah.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Battery = "2000mAh";
				totalPrice += 30;
			}
		});
		btnmah.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btnmah.setBounds(75, 10, 89, 17);
		btnmah.setText("2000mAh");
		
		Button btnmah_1 = new Button(composite_1, SWT.RADIO);
		btnmah_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Battery = "2500mAh";
				totalPrice += 35;
			}
		});
		btnmah_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btnmah_1.setBounds(75, 39, 89, 17);
		btnmah_1.setText("2500mAh");
		
		//Behind Color
		Label label_1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setBounds(0, 354, 492, 2);
		
		Composite composite_2 = new Composite(shell, SWT.NONE);
		composite_2.setBounds(211, 39, 271, 197);
		
		Label lblBehindColor = new Label(composite_2, SWT.NONE);
		lblBehindColor.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblBehindColor.setBounds(0, 158, 94, 25);
		lblBehindColor.setText("Behind Color:");
		
		final Combo combo_3 = new Combo(composite_2, SWT.NONE);
		combo_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				behindColor = combo_3.getItem(combo_3.getSelectionIndex());
			}
		});
		
		combo_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		combo_3.setBounds(106, 155, 71, 27);
		combo_3.setItems(new String[] {"Black", "White", "Blue", "Red", "Green", "Yellow", "Gray", "Golden"});
		combo_3.setText("Black");
		
		//Behind Shell Material
		Label lblBehindShell = new Label(composite_2, SWT.NONE);
		lblBehindShell.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblBehindShell.setBounds(0, 49, 139, 25);
		lblBehindShell.setText("Behind Shell Material:");
		
		Button btnRadioButton = new Button(composite_2, SWT.RADIO);
		btnRadioButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				behindShellMaterial = "High quality Plastics";
				totalPrice += 10;
			}
		});
		btnRadioButton.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btnRadioButton.setBounds(106, 72, 150, 17);
		btnRadioButton.setText("High Quality Plastics");
		
		Button btnWooden = new Button(composite_2, SWT.RADIO);
		btnWooden.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				behindShellMaterial = "Wooden";
				totalPrice += 20;
			}
		});
		btnWooden.setText("Wooden");
		btnWooden.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btnWooden.setBounds(106, 95, 150, 17);
		
		Button btnAluminiumAlloy = new Button(composite_2, SWT.RADIO);
		btnAluminiumAlloy.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				behindShellMaterial = "Aluminium Alloy";
				totalPrice += 30;
			}
		});
		btnAluminiumAlloy.setText("Aluminium Alloy");
		btnAluminiumAlloy.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btnAluminiumAlloy.setBounds(106, 118, 150, 17);
		
		//Front Color
		Label lblFrontColor = new Label(composite_2, SWT.NONE);
		lblFrontColor.setText("Front Color:");
		lblFrontColor.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblFrontColor.setBounds(0, 13, 94, 25);
		
		final Combo combo = new Combo(composite_2, SWT.NONE);
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				frontColor = combo.getItem(combo.getSelectionIndex());
			}
		});
		combo.setItems(new String[] {"Black", "White", "Blue", "Red", "Green", "Yellow", "Gray", "Golden"});
		combo.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		combo.setBounds(106, 10, 71, 27);
		combo.setText("Black");
		
		
		//Words On Behind Shell
		Composite composite_5 = new Composite(shell, SWT.NONE);
		composite_5.setBounds(211, 242, 271, 90);
		
		Button btnWordsOnBehind = new Button(composite_5, SWT.CHECK);
		btnWordsOnBehind.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				flag = true;
			}
		});
		btnWordsOnBehind.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		btnWordsOnBehind.setBounds(0, 10, 177, 17);
		btnWordsOnBehind.setText("Words On Behind Shell:");
		
		text_1 = new Text(composite_5, SWT.BORDER);
		text_1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(flag == true){
					words = text_1.getText();
				}
			}
		});
		text_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		text_1.setBounds(20, 33, 241, 30);
		
		//name
		Label lblName = new Label(shell, SWT.NONE);
		lblName.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblName.setText("Name:");
		lblName.setBounds(17, 362, 45, 19);
		
		text = new Text(shell, SWT.BORDER);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				name = text.getText();
			}
		});
		text.setBounds(68, 361, 73, 23);
		
		
		//Phone Number
		Label lblTel = new Label(shell, SWT.NONE);
		lblTel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblTel.setBounds(210, 364, 98, 17);
		lblTel.setText("Phone Number:");
		
		text_2 = new Text(shell, SWT.BORDER);
		text_2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				phoneNumber = text_2.getText();
			}
		});
		text_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		text_2.setBounds(314, 362, 156, 23);
		
		
		//Address
		Label lblAddress = new Label(shell, SWT.NONE);
		lblAddress.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		lblAddress.setBounds(17, 401, 61, 17);
		lblAddress.setText("Address:");
		
		text_3 = new Text(shell, SWT.BORDER);
		text_3.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				address = text_3.getText();
			}
		});
		text_3.setBounds(80, 401, 390, 23);
		
		//button
		Button btnDesign = new Button(shell, SWT.NONE);
		btnDesign.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				shell.close();
				String infomation = "Mr./Mrs. " + name + ",  Your phone number: " + phoneNumber + "\r\nYour address: " + address
								  + "\r\n\r\nYour Moto X:\r\n\tCPU Cores: " + CPUCores + "\r\n\tRAM: " + RAM + "\r\n\tROM: " + ROM 
								  + "\r\n\tBattery: " + Battery + "\r\n\tPhone Front Color: " + frontColor 
								  + "\r\n\tBehind Shell Material: " + behindShellMaterial + "\r\n\tPhone Behind Color: " + behindColor
								  + "\r\n\tWords On The Behind Shell:\r\n\t\t" + words + "\r\nTotal Price: " + totalPrice + "$";
				ResultDisplay.main(infomation);
			}
		});
		btnDesign.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		btnDesign.setBounds(179, 432, 110, 27);
		btnDesign.setText("Design");
		
	}
}

ResultDisplay.java

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;


public class ResultDisplay {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 * @wbp.parser.entryPoint
	 */
	public static void main(String args) {
		try {
			ResultDisplay window = new ResultDisplay();
			window.open(args);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open(String args) {
		Display display = Display.getDefault();
		createContents(args);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents(String args) {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage("Moto Logo2.jpg"));
		shell.setSize(500, 500);
		shell.setText("Design Your Own Moto X");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.NORMAL));
		lblNewLabel.setBounds(10, 55, 472, 281);
		lblNewLabel.setText(args);
		
		Label lblHereIsYour = new Label(shell, SWT.NONE);
		lblHereIsYour.setForeground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));
		lblHereIsYour.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 15, SWT.BOLD));
		lblHereIsYour.setBounds(10, 10, 216, 27);
		lblHereIsYour.setText("Here is your moto X:");
		
		Label lblThankYour = new Label(shell, SWT.NONE);
		lblThankYour.setForeground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));
		lblThankYour.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 15, SWT.BOLD));
		lblThankYour.setBounds(342, 342, 126, 37);
		lblThankYour.setText("Thank your!");
		
		Button btnBuyItNow = new Button(shell, SWT.NONE);
		btnBuyItNow.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				shell.close();
			}
		});
		btnBuyItNow.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		btnBuyItNow.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 13, SWT.BOLD));
		btnBuyItNow.setBounds(184, 406, 114, 27);
		btnBuyItNow.setText("Buy It Now!");

	}
}


                                                                                                                                           -------------Aollo


你可能感兴趣的:(自己写的一个简单的Java GUI源码)