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

📄 preferenceeditor.java

📁 java图形化界面编程 功能和记事本通讯录相似 记录通讯人的姓名和联系方法等信息并提供查询
💻 JAVA
字号:
package elegate.cn.edu.nju;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.*;
import javax.swing.*;

/**
 * a simple preference editor
 * @author Elegate,elegate@gmail.com
 * @author cs department of NJU
 *
 */
public class PreferenceEditor extends JDialog
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox comboLook;
	private JTextField txtSavePath;
	private JButton btnBrowser;
	private JButton btnOk;
	private JButton btnCancel;
	private JCheckBox usePswProtection;
	private JPasswordField pswField;
	private JPasswordField confirmPswField;
	
	private Properties properties;
	private static UIManager.LookAndFeelInfo arr[];
	
	public PreferenceEditor(JFrame parent,Properties properties)
	{
		super(parent,true);
		this.setTitle("Preference");
		this.properties=properties;
		comboLook=new JComboBox();
		arr=UIManager.getInstalledLookAndFeels();
		for(UIManager.LookAndFeelInfo ele:arr)
		{
			comboLook.addItem(ele.getName());
			if(ele.getClassName().equals(properties.get("look")))
				comboLook.setSelectedItem(ele.getName());
		}
		
		txtSavePath=new JTextField(properties.getProperty("path"),10);
		btnBrowser=new JButton("Browser");
		btnBrowser.setMnemonic('B');
		btnBrowser.setToolTipText("Click to selecte a file path");
		btnBrowser.addActionListener(new BtnBrowserL());
		
		usePswProtection=new JCheckBox("Use password protection",false);
		usePswProtection.setMnemonic('U');
		usePswProtection.addActionListener(new ActionListener()
				{

					public void actionPerformed(ActionEvent e)
					{
						if(usePswProtection.isSelected())
						{
							pswField.setEditable(true);
							confirmPswField.setEditable(true);
						}
						else
						{
							pswField.setEditable(false);
							confirmPswField.setEditable(false);
						}
					}
			
				});
		
		JLabel lblPswField=new JLabel("Enter password:");
		lblPswField.setDisplayedMnemonic('E');
		pswField=new JPasswordField();
		pswField.setEditable(false);
		lblPswField.setLabelFor(pswField);
		JLabel lblConfirmPswField = 
			new JLabel("Confirm password:");
		lblConfirmPswField.setDisplayedMnemonic('F');
		confirmPswField=new JPasswordField();
		confirmPswField.setEditable(false);
		lblConfirmPswField.setLabelFor(confirmPswField);
		
		
		
		btnOk=new JButton("Ok");
		btnOk.setMnemonic('O');
		btnOk.addActionListener(new BtnOkL());
		btnOk.setToolTipText("Submit the modification");
		btnCancel=new JButton("Cancel");
		btnCancel.setMnemonic('C');
		btnCancel.addActionListener(new BtnCancelL());
		btnCancel.setToolTipText("Cancel the modification");
		
		JLabel lblPreference=new JLabel("Preference");
		lblPreference.setHorizontalAlignment
		(SwingConstants.CENTER);
		
		lblPreference.setFont(
				new Font(
						lblPreference.getFont().getFontName()
						,Font.BOLD,24));
		JLabel lblLook=new JLabel("Look:");
		lblLook.setDisplayedMnemonic('L');
		lblLook.setLabelFor(comboLook);
		
		JLabel lblSavePath=new JLabel("Store path:");
		lblSavePath.setDisplayedMnemonic('S');
		lblSavePath.setLabelFor(txtSavePath);
		
		GridBagLayout gbLayout=new GridBagLayout();
		GridBagConstraints gbCon=new GridBagConstraints();
		this.getContentPane().setLayout(gbLayout);
		gbCon.weightx=100;
		gbCon.fill=GridBagConstraints.BOTH;
		this.addComponent(lblPreference,0,0,3,1,gbCon);
		this.addComponent(lblLook,0,1,1,1,gbCon);
		this.addComponent(comboLook,1,1,1,1,gbCon);
		this.addComponent(lblSavePath,0,2,1,1,gbCon);
		this.addComponent(txtSavePath,1,2,1,1,gbCon);
		this.addComponent(btnBrowser,2,2,1,1,gbCon);
		this.addComponent(usePswProtection,0,3,3,1,gbCon);
		this.addComponent(lblPswField,0,4,1,1,gbCon);
		this.addComponent(pswField,1,4,1,1,gbCon);
		this.addComponent(lblConfirmPswField,0,5,1,1,gbCon);
		this.addComponent(confirmPswField,1,5,1,1,gbCon);
		JPanel btnPanel=new JPanel();
		btnPanel.add(btnOk);
		btnPanel.add(btnCancel);
		this.addComponent(btnPanel,0,6,3,1,gbCon);
		this.pack();
		Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
		int width=this.getWidth();
		int height=this.getHeight();
		this.setLocation((d.width-width)/2,(d.height-height)/2);
		this.getRootPane().setDefaultButton(btnOk);
	}
	
	protected void update(Properties p)
	{
		if(p==null)
			return;
		this.properties=p;
		this.comboLook.setSelectedItem(p.get("look"));
		this.txtSavePath.setText(p.getProperty("path"));
	}
	
	public Properties getProperties()
	{
		return this.properties;
	}
	
	
	
	private void addComponent(Component c,int gridx,int gridy
			,int gridwidth,int gridheight,GridBagConstraints gbCon)
	{
		gbCon.gridx=gridx;
		gbCon.gridy=gridy;
		gbCon.gridheight=gridheight;
		gbCon.gridwidth=gridwidth;
		this.getContentPane().add(c,gbCon);
	}
	
	private void clearPasswordInput()
	{
		usePswProtection.setSelected(false);
		pswField.setEditable(false);
		confirmPswField.setEditable(false);
		pswField.setText("");
		confirmPswField.setText("");
	}
	
	class BtnBrowserL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			JFileChooser fileChooser=new JFileChooser();
			int option=fileChooser.showOpenDialog(PreferenceEditor.this);
			if(option==JFileChooser.APPROVE_OPTION)
			{
				if(properties==null)
					properties=new Properties(AddressBookManager.DEFAULT_PROPERTIES);
				properties.put("path",fileChooser.getSelectedFile().getAbsolutePath());
				txtSavePath.setText(fileChooser.getSelectedFile().getPath());
			}
		}
		
	}
	
	
	class BtnOkL implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(properties==null)
				properties=new Properties(AddressBookManager.DEFAULT_PROPERTIES);
			properties.put("look",arr[comboLook.getSelectedIndex()].getClassName());
			File file=new File(txtSavePath.getText().trim());
			String path=txtSavePath.getText().trim();
			if(!file.exists())
			{
				try
				{
					file.createNewFile();
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog
					(PreferenceEditor.this,ex.toString(),"Error"
							,JOptionPane.ERROR_MESSAGE);
					clearPasswordInput();
					path="config"+File.separator
					+"data"+File.separator+"Addresses";
				}
			}
			properties.put("path",path);
			if(usePswProtection.isSelected())
			{
				char[] psw=pswField.getPassword();
				char[] confirmPsw=confirmPswField.getPassword();
				if(!String.valueOf(psw).equals
						(String.valueOf(confirmPsw)))
				{
					JOptionPane.showMessageDialog(PreferenceEditor.this
							,"The passwords doesn't match"
							,"Warning",JOptionPane.WARNING_MESSAGE);
					return;
				}
				else
				{
					String pswStr=String.valueOf(psw);
					if(pswStr.length()!=0)
					{
						pswStr=Encryption.arrayEncrypt(pswStr);
						properties.put("psw",pswStr);
						clearPasswordInput();
					}
					else
					{
						JOptionPane.showMessageDialog
						(PreferenceEditor.this
								,"Blank password may has no protection effect"
								,"Warning",JOptionPane.WARNING_MESSAGE);
						properties.put("psw","");
						clearPasswordInput();
					}
				}
			}
			setVisible(false);
		}
		
	}
	
	class BtnCancelL implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			properties=null;
			setVisible(false);
		}
		
	}
}

⌨️ 快捷键说明

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