Crea una interfaz de computadora simple

La imagen del efecto es como arriba

 

Entorno operativo: eclipse bajo win8

 

Pasos de producción:

1. Crea una interfaz

2. Establezca las propiedades de la interfaz (tamaño, título, posición de apariencia, diseño del formulario y visibilidad)

3. Cree el componente de botón que necesitamos

4. Agregue el componente de botón a la interfaz

5. Cree un detector de eventos

6. Agregue un detector de eventos para el botón.

7. Utilice instrucciones if para configurar diferentes eventos para diferentes botones

 

El código fuente es el siguiente:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Practice
{

 public static void main(String[]args)
 {
  //新建一个窗体
  JFrame jf=new JFrame();
  
  //设置窗体属性
  jf.setTitle("电脑操作界面");
  jf.setSize(300,200);
  jf.setLocationRelativeTo(null);
  jf.setLayout(new FlowLayout());
  jf.setDefaultCloseOperation(3);
  
  //新建窗体组件
  JButton jb1=new JButton("关机");
  JButton jb2=new JButton("打开浏览器");
  JButton jb3=new JButton("打开CB");
  JButton jb4=new JButton("打开酷狗");
  
  //添加组件到窗体
  jf.add(jb1);
  jf.add(jb2);
  jf.add(jb3);
  jf.add(jb4);
  
  //设置窗体可见
  jf.setVisible(true);
  
  //创建事件监听
  ActionListener action=new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    //监听过程,获取文本
    String str=e.getActionCommand();
    //获取系统操作对象
    if("关机".equals(str))
    {
     Runtime run=Runtime.getRuntime();
        try 
        {
          run.exec("shutdown -s -t 5000");
        } 
        catch (IOException e1)
        {
     e1.printStackTrace();
     }
        jb1.setText("取消关机");
    }
    if("打开浏览器".equals(str))
    {
     Runtime run=Runtime.getRuntime();
        try 
        {
          run.exec("explorer http:\\www.qq.com");
        } 
        catch (IOException e1)
        {
     e1.printStackTrace();
     }
        
    }
    if("打开CB".equals(str))
    {
     Runtime run=Runtime.getRuntime();
        try 
        {
          run.exec("D:/程序/CodeBlocks/codeblocks.exe");
        } 
        catch (IOException e1)
        {
     e1.printStackTrace();
     }
        
    } 
    if("打开酷狗".equals(str))
    {
     Runtime run=Runtime.getRuntime();
        try 
        {
          run.exec("C:/Program Files (x86)/KuGou/KGMusic/KuGou.exe");
        } 
        catch (IOException e1)
        {
     e1.printStackTrace();
     }
        
    }    
    if("取消关机".equals(str))
    {
     Runtime run=Runtime.getRuntime();
        try 
        {
          run.exec("shutdown -a");
        } 
        catch (IOException e1)
        {
     e1.printStackTrace();
     }
        
    }    
   }
  };
  //给按钮添加事件监听器
  jb1.addActionListener(action);
  jb2.addActionListener(action);
  jb3.addActionListener(action);
  jb4.addActionListener(action);
 }
}

 

Nota:

"shutdown -s -t 1000" significa apagado con un retraso de 1000 segundos

"Shutdown -a" es cancelar el apagado

Si desea abrir cierto software, copie la ruta del archivo .exe de este software entre las comillas de run.exec ("") y cambie todo "\" a "/"

 

 

 

 



Supongo que te gusta

Origin blog.csdn.net/dream_18/article/details/51530937
Recomendado
Clasificación