当前位置: 代码迷 >> J2SE >> - 点击按钮 运行 bat文件
  详细解决方案

- 点击按钮 运行 bat文件

热度:504   发布时间:2016-04-24 02:26:38.0
紧急求助-- 点击按钮 运行 bat文件,
我想把一个按钮做成 点击它就运行 bat文件,两部分代码都有 如何拼凑一起啊,小妹是初学者,还不太明白面向对象

代码1 运行bat 
import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Test {


public Test() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] command) {
Process process;
try {
process = Runtime.getRuntime().exec(command[0]);
BufferedReader read = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String str = null;
while ((str = read.readLine()) != null) {
System.out.println(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}




代码二 GUI 框架 点击按钮变背景

/**
  @version 1.32 2004-05-04
  @author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonTest
{  
  public static void main(String[] args)
  {  
  ButtonFrame frame = new ButtonFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }
}

/**
  A frame with a button panel
*/
class ButtonFrame extends JFrame
{
  public ButtonFrame()
  {
  setTitle("ButtonTest");
  setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

  // add panel to frame

  ButtonPanel panel = new ButtonPanel();
  add(panel);
  }

  public static final int DEFAULT_WIDTH = 300;
  public static final int DEFAULT_HEIGHT = 200;  
}

/**
  A panel with three buttons.
*/
class ButtonPanel extends JPanel
{  
  public ButtonPanel()
  {  
  // create buttons

  JButton yellowButton = new JButton("Yellow");
  JButton blueButton = new JButton("Blue");
  JButton redButton = new JButton("Red");

  // add buttons to panel

  add(yellowButton);
  add(blueButton);
  add(redButton);

  // create button actions

  ColorAction yellowAction = new ColorAction(Color.YELLOW);
  ColorAction blueAction = new ColorAction(Color.BLUE);
  ColorAction redAction = new ColorAction(Color.RED);

  // associate actions with buttons

  yellowButton.addActionListener(yellowAction);
  blueButton.addActionListener(blueAction);
  redButton.addActionListener(redAction);
  }

  /**
  An action listener that sets the panel's background color. 
  */
  private class ColorAction implements ActionListener
  {  
  public ColorAction(Color c)
  {  
  backgroundColor = c;
  }

  public void actionPerformed(ActionEvent event)
  {  
  setBackground(backgroundColor);
  }

  private Color backgroundColor;
  }
}





------解决方案--------------------
哈哈,和我近的话,我给你写好啦
探讨
你在哪个城市,问问先,嘿嘿


引用:

具体代码 怎么写 能否在我上面两个代码 写个类? 我先学习的是c 面向对象还不明白,java一上来就是c++

------解决方案--------------------
  相关解决方案