자바 스윙 정시 작업 도구(자바 창 스타일 변경 가능)

최근 들 어 한가 해서 일 을 할 시간 이 있어 자바 스윙 으로 완성 한 이 작은 도구 가 있 습 니 다.비교적 간단 하기 때문이다.그래서 모든 논리 적 처 리 를 한 가지 유형 에 두 었 기 때문에 규범 성 이 좋 지 않다.또한 이 작은 도 구 는 피 부 를 바 꿀 수 있 으 며 자바 에 내 장 된 5 가지 스타일 을 사용 하여 실행 가능 한 jar 파일 로 내 보 낼 수 있 습 니 다.ok,쓸데없는 말 말고 코드 를 보 세 요.
package com.yjck.timetask;


public class TimeTask extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private String path = null;
	private Map<Integer,String> map = new HashMap<Integer,String>();
	private JPanel mainPanel = null;
	private JPanel btPanel = null;
	private JRadioButton closeRadio = null;
	private JRadioButton resetRadio = null;
	private JRadioButton logoutRadio = null;
	private JRadioButton planRadio = null;
	private ButtonGroup btGroup = null;	
	private JLabel titleLabel = null;
	private JLabel timeLabel = null;	
	private JLabel lookAndFell = null;
	private JTextField timeField =null;
	private JTextField planField = null;
	private JButton browerBt = null;
	private JButton ConfirmBt = null;
	private JButton CancelBt = null;
	private JComboBox laf = null;
	
	public TimeTask(){
		init();
	}
	/**
	 *       
	 * @return Vector<String>
	 */
	private Vector<String> initComboBos() {
		//    java     
		LookAndFeelInfo[] lafInfo =  UIManager.getInstalledLookAndFeels();
		Vector<String> lafs = new Vector<String>();
		for (int i = 0; i < lafInfo.length; i++) {
			//     map ,           。
			map.put(i,lafInfo[i].getClassName());
			String[] temp = lafInfo[i].getClassName().split("\\.");
			String str = temp[temp.length-1].replace("LookAndFeel", "");
			lafs.addElement(str);
		}
		return lafs;
	}
	@SuppressWarnings("static-access")
	private void init(){
		this.mainPanel = (JPanel) this.getContentPane();
		this.mainPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		this.btGroup = new ButtonGroup();
		
		gbc.gridx = 1;
		gbc.gridy = 0;
		this.titleLabel = new JLabel("       ");
		this.mainPanel.add(this.titleLabel,gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		this.closeRadio = new JRadioButton("  ");
		this.closeRadio.addActionListener(this);
		this.closeRadio.setSelected(true);
		this.btGroup.add(this.closeRadio);
		this.mainPanel.add(this.closeRadio,gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 1;
		this.resetRadio = new JRadioButton("  ");
		this.resetRadio.addActionListener(this);
		this.btGroup.add(this.resetRadio);
		this.mainPanel.add(this.resetRadio,gbc);
		
		gbc.gridx = 2;
		gbc.gridy = 1;
		this.logoutRadio = new JRadioButton("  ");
		this.resetRadio.addActionListener(this);
		this.btGroup.add(this.logoutRadio);
		this.mainPanel.add(this.logoutRadio,gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 2;
		this.planRadio = new JRadioButton("    ");
		this.planRadio.addActionListener(this);
		this.btGroup.add(this.planRadio);
		this.mainPanel.add(this.planRadio,gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 2;
		this.planField = new JTextField(10);
		this.planField.setEditable(false);
		this.mainPanel.add(this.planField,gbc);
		
		gbc.gridx = 2;
		gbc.gridy = 2;
		this.browerBt = new JButton("  ");
		this.browerBt.setEnabled(false);
		this.browerBt.addActionListener(this);
		this.mainPanel.add(this.browerBt,gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 3;
		this.timeLabel= new JLabel("     (  )");
		this.mainPanel.add(this.timeLabel,gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 3;
		this.timeField= new JTextField(10);
		this.mainPanel.add(this.timeField,gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 4;
		this.lookAndFell= new JLabel("     ");
		this.mainPanel.add(this.lookAndFell,gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 4;
		this.laf = new JComboBox(initComboBos());
		this.laf.addActionListener(this);
		this.mainPanel.add(this.laf,gbc);
		
		
		gbc.gridx = 1;
		gbc.gridy = 5;
		this.btPanel = new JPanel();
		this.ConfirmBt = new JButton("  ");
		this.ConfirmBt.addActionListener(this);
		this.CancelBt = new JButton("  ");
		this.CancelBt.addActionListener(this);
		this.btPanel.add(this.ConfirmBt);
		this.btPanel.add(this.CancelBt);
		this.mainPanel.add(this.btPanel,gbc);
		
		
		
		
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (d.height - 200)/2;
		int y = (d.width - 350)/2;
		this.setBounds(x, y, 350, 200);
		this.setTitle("    ");
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	}
	

	@Override
	public void actionPerformed(ActionEvent event) {
		/**************************************
		 *           ,          
		 *      ,               
		 * ************************************
		 */
		if(this.planRadio.isSelected()){
			this.browerBt.setEnabled(true);
		}else{
			this.planField.setText("");
			this.browerBt.setEnabled(false);
		}
		/**************************************
		 *     ,              
		 * map            
		 * ************************************
		 */
		int index = this.laf.getSelectedIndex();
		try {
			UIManager.setLookAndFeel(this.map.get(index));
			//    
			SwingUtilities.updateComponentTreeUI(this);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		
		//                 。
		if(event.getActionCommand().equals("  ")){
			System.out.println("  ");
			 JFileChooser chooser = new JFileChooser();
			 //       ,        
			    FileNameExtensionFilter filter = new FileNameExtensionFilter(
			        "     bat&exe&msi", "bat", "exe","msi");
			    chooser.setFileFilter(filter);
			    int returnVal = chooser.showOpenDialog(null);
			    //         
			    if(returnVal == JFileChooser.APPROVE_OPTION) {
			       path = chooser.getSelectedFile().getPath();
			       this.planField.setText(path);
			      
			    }

		}
		//           
		if(event.getActionCommand().equals("  ")){
			System.out.println("  ");
			//        
			Runtime runTime = Runtime.getRuntime();
			String time = this.timeField.getText();
			//         
			if(time == null || time.equals("")){
				JOptionPane.showMessageDialog(this, "       ");
			}else{				
				if(this.planRadio.isSelected()){
					//                    
					if(this.path == null || this.path.equals("")){
						JOptionPane.showMessageDialog(this, "        ");
					}else{						
						try {						
							Thread.sleep((long) (Double.parseDouble(this.timeField.getText().trim())*60*1000));
							runTime.exec(path);
						}  catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
				//     
				if(this.closeRadio.isSelected()){
					try {
						Thread.sleep((long) (Double.parseDouble(this.timeField.getText().trim())*60*1000));
						runTime.exec("shutdown -s");
					}catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				//     
				if(this.resetRadio.isSelected()){
					try {
						Thread.sleep((long) (Double.parseDouble(this.timeField.getText().trim())*60*1000));
						runTime.exec("shutdown -r");
					}catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				//     
				if(this.logoutRadio.isSelected()){
					try {
						Thread.sleep((long) (Double.parseDouble(this.timeField.getText().trim())*60*1000));
						runTime.exec("shutdown -l");
					}catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}			
		}
		if(event.getActionCommand().equals("  ")){
			System.exit(0);
		}
		
	}
	
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args){
		new TimeTask();
	}

}

 모든 코드 에 주석 이 있어 서 알 아 볼 수 있 을 겁 니 다.레이아웃 은 자신 이 위치 한 gridbaglayot 입 니 다.다른 것 은 더 이상 할 말 이 없 을 것 입 니 다.자바 스윙 프로 그래 밍 의 일부 특징 을 기록 하여 나중에 사용 할 수 있 도록 합 니 다!

좋은 웹페이지 즐겨찾기