Wednesday, 23 December 2009

Shutdown Dengan Java

Tampilan Aplikasinya Sebagai Berikut :














Ini Source Code Lengkapnya :

import javax.swing.JOptionPane;

public class utama extends javax.swing.JFrame{

Runtime rt = Runtime.getRuntime();
public utama() {
initComponents();
}
public void tampil()
{
if(JOptionPane.showConfirmDialog(rootPane, "Batal?", "Konfirmasi", JOptionPane.DEFAULT_OPTION)==0)
{
try
{
rt.exec("cmd /c shutdown -a");
}catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
@SuppressWarnings("unchecked")
//
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
bshut3 = new javax.swing.JButton();
bshut1 = new javax.swing.JButton();
bshut2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Shutdown Demo");
setLocationByPlatform(true);
setResizable(false);
getContentPane().setLayout(null);

jPanel1.setBackground(new java.awt.Color(255, 0, 0));
jPanel1.setLayout(null);

bshut3.setFont(new java.awt.Font("Tahoma", 0, 14));
bshut3.setText("Log Off");
bshut3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bshut3ActionPerformed(evt);
}
});
jPanel1.add(bshut3);
bshut3.setBounds(20, 120, 270, 30);

bshut1.setFont(new java.awt.Font("Tahoma", 0, 14));
bshut1.setText("Shutdown");
bshut1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bshut1ActionPerformed(evt);
}
});
jPanel1.add(bshut1);
bshut1.setBounds(20, 20, 270, 30);

bshut2.setFont(new java.awt.Font("Tahoma", 0, 14));
bshut2.setText("Restart");
bshut2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bshut2ActionPerformed(evt);
}
});
jPanel1.add(bshut2);
bshut2.setBounds(20, 70, 270, 30);

getContentPane().add(jPanel1);
jPanel1.setBounds(0, 0, 310, 170);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-315)/2, (screenSize.height-200)/2, 315, 200);
}//


private void bshut3ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
if(JOptionPane.showConfirmDialog(rootPane, "Yakin Ingin Log Off?", "Konfirmasi", JOptionPane.YES_NO_OPTION)==0)
{
rt.exec("cmd /c shutdown -l -t 30");
}
}catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void bshut1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
rt.exec("cmd /c shutdown -s -t 30");
tampil();
}catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void bshut2ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
rt.exec("cmd /c shutdown -r -t 30");
tampil();
}catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new utama().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton bshut1;
private javax.swing.JButton bshut2;
private javax.swing.JButton bshut3;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}

Sorry Sangat Simple Soalnya Baru Belajar Juga.... :-D

No comments:

Post a Comment