자바 스윙 정시 작업 도구(자바 창 스타일 변경 가능)
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 입 니 다.다른 것 은 더 이상 할 말 이 없 을 것 입 니 다.자바 스윙 프로 그래 밍 의 일부 특징 을 기록 하여 나중에 사용 할 수 있 도록 합 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.