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

📄 mydialog4.java

📁 SWTJFace篇项目源程序该项目包含 包含了Eclipse下构建swt的基本工程
💻 JAVA
字号:
package cn.com.chengang.jface.dialog;

import java.io.IOException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MyDialog4 extends Dialog {
	private Human human;
	private Text nameText;
	private Text oldText;
	private Button ggButton, mmButton;

	public MyDialog4(Shell parentShell) {
		super(parentShell);
	}

	public Human getInput() {
		return this.human;
	}

	public void setInput(Human human) {
		this.human = human;
	}

	// 在这个方法里构建Dialog中的界面内容
	protected Control createDialogArea(Composite parent) {
		Composite topComp = new Composite(parent, SWT.NONE);
		topComp.setLayout(new GridLayout(2, false));
		new Label(topComp, SWT.NONE).setText("姓名:");
		nameText = new Text(topComp, SWT.BORDER);
		nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		new Label(topComp, SWT.NONE).setText("年龄:");
		oldText = new Text(topComp, SWT.BORDER);// 省略了只能输入数值的限制
		oldText.setLayoutData(new GridData(20, -1));

		new Label(topComp, SWT.NONE).setText("性别:");
		Composite c = new Composite(topComp, SWT.None);
		c.setLayout(new RowLayout());
		ggButton = new Button(c, SWT.RADIO);
		ggButton.setText("男");
		mmButton = new Button(c, SWT.RADIO);
		mmButton.setText("女");

		// 如果没有手动设置初始值,则取出以前保存在文件里的值更新到Human对象
		if (human == null)
			restoreState();
		
		if (human != null) {
			nameText.setText(human.getName() == null ? "" : human.getName());
			oldText.setText(String.valueOf(human.getOld()));
			ggButton.setSelection(human.isSex());
			mmButton.setSelection(!human.isSex());
		}
		return topComp;
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {// 如果单击确定按钮,则将值保存到Human对象中
			if (human == null)
				human = new Human();
			human.setName(nameText.getText());
			human.setOld(Integer.parseInt(oldText.getText()));
			human.setSex(ggButton.getSelection());
			saveState();//将Human对象保存到文件
		}
		super.buttonPressed(buttonId);
	}

	// 将Human对象保存到文件
	public void saveState() {
		if (human == null)
			return;
		IDialogSettings topSettings = getTopSettings();
		IDialogSettings settings = topSettings.getSection("MyDialog4");
		if (settings == null)
			settings = topSettings.addNewSection("MyDialog4");
		settings.put("name", human.getName());
		settings.put("old", human.getOld());
		settings.put("sex", human.isSex());
		try {
			topSettings.save("icons/system.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 取出保存的值并更新到Human对象
	public void restoreState() {
		IDialogSettings topSettings = getTopSettings();
		IDialogSettings settings = topSettings.getSection("MyDialog4");
		if (settings == null)
			return;
		if (human == null)
			human = new Human();
		human.setName(settings.get("name"));
		human.setOld(settings.getInt("old"));
		human.setSex(settings.getBoolean("sex"));
	}

	// 取得顶级的IDialogSettings
	public IDialogSettings getTopSettings() {
		IDialogSettings topSettings = new DialogSettings("system");
		try {
			topSettings.load("icons/system.xml");
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return topSettings;
	}
}

⌨️ 快捷键说明

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