读书人

- 点击按钮 运行 bat文件

发布时间: 2012-01-14 20:02:35 作者: rapoo

紧急求助-- 点击按钮 运行 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++

------解决方案--------------------


bat是windows操作系统的 所以不必读取bat文件了 直接调用cmd运行bat不就行了 你们太死脑筋了

Java code
try {            Runtime.getRuntime().exec( "cmd /c d:/1.bat");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();    } 

读书人网 >J2SE开发

热点推荐