JAVA 如何通过按钮打开另一个程序
这是我编的一个程序importjava.io.*;importjava.awt.*;importjava.awt.event.*;publicclassjtxtfm{pu...
这是我编的一个程序
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class jtxtfm{
public static void main(String args[]){
jtxtfrm fm=new jtxtfrm();
}
}
class jtxtfrm extends Frame implements ActionListener{
FileDialog op,sv;
Button btn1,btn2,btn3;
TextArea tarea;
jtxtfrm(){
super("读写文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600,300);
setVisible(true);
btn1=new Button("打开");
btn2=new Button("保存");
btn3=new Button("关闭");
tarea=new TextArea("");
add(btn1);add(btn2);add(btn3);add(tarea);
tarea.setBounds(30,50,460,220);
btn1.setBounds(520,60,50,30);
btn2.setBounds(520,120,50,30);
btn3.setBounds(520,180,50,30);
op=new FileDialog(this,"打开",FileDialog.LOAD);
sv=new FileDialog(this,"保存",FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
String str;
op.setVisible(true);
try{
File f1=new File(op.getDirectory(),op.getFile());
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
tarea.setText("");
while((str=br.readLine())!=null)tarea.append(str+'\n');
fr.close();
}
catch(Exception e1)
{}
}
if(e.getSource()==btn2){
sv.setVisible(true);
try{
File f1=new File(sv.getDirectory(),sv.getFile());
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
String gt=tarea.getText();
bw.write(gt,0,gt.length());
bw.flush();
fw.close();
}
catch ( Exception e2)
{}
}
if(e.getSource()==btn3){
System.exit(0);
}
}
}
我想通过一个按钮点击打开它
求大神帮忙 越详细越好,谢谢! 展开
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class jtxtfm{
public static void main(String args[]){
jtxtfrm fm=new jtxtfrm();
}
}
class jtxtfrm extends Frame implements ActionListener{
FileDialog op,sv;
Button btn1,btn2,btn3;
TextArea tarea;
jtxtfrm(){
super("读写文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600,300);
setVisible(true);
btn1=new Button("打开");
btn2=new Button("保存");
btn3=new Button("关闭");
tarea=new TextArea("");
add(btn1);add(btn2);add(btn3);add(tarea);
tarea.setBounds(30,50,460,220);
btn1.setBounds(520,60,50,30);
btn2.setBounds(520,120,50,30);
btn3.setBounds(520,180,50,30);
op=new FileDialog(this,"打开",FileDialog.LOAD);
sv=new FileDialog(this,"保存",FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
String str;
op.setVisible(true);
try{
File f1=new File(op.getDirectory(),op.getFile());
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
tarea.setText("");
while((str=br.readLine())!=null)tarea.append(str+'\n');
fr.close();
}
catch(Exception e1)
{}
}
if(e.getSource()==btn2){
sv.setVisible(true);
try{
File f1=new File(sv.getDirectory(),sv.getFile());
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
String gt=tarea.getText();
bw.write(gt,0,gt.length());
bw.flush();
fw.close();
}
catch ( Exception e2)
{}
}
if(e.getSource()==btn3){
System.exit(0);
}
}
}
我想通过一个按钮点击打开它
求大神帮忙 越详细越好,谢谢! 展开
展开全部
package kuohao;
import java.awt.Button;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class jtxtfm {
public static void main(String args[]) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JButton jb = new JButton("打开对话框");
jf.add(jp);
jp.add(jb);
jf.setVisible(true);
jf.setSize(200,200);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtxtfrm fm = new jtxtfrm();
}
});
}
}
class jtxtfrm extends Frame implements ActionListener {
FileDialog op, sv;
Button btn1, btn2, btn3;
TextArea tarea;
jtxtfrm() {
super("读写文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600, 300);
setVisible(true);
btn1 = new Button("打开");
btn2 = new Button("保存");
btn3 = new Button("关闭");
tarea = new TextArea("");
add(btn1);
add(btn2);
add(btn3);
add(tarea);
tarea.setBounds(30, 50, 460, 220);
btn1.setBounds(520, 60, 50, 30);
btn2.setBounds(520, 120, 50, 30);
btn3.setBounds(520, 180, 50, 30);
op = new FileDialog(this, "打开", FileDialog.LOAD);
sv = new FileDialog(this, "保存", FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
String str;
op.setVisible(true);
try {
File f1 = new File(op.getDirectory(), op.getFile());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
tarea.setText("");
while ((str = br.readLine()) != null)
tarea.append(str + '\n');
fr.close();
} catch (Exception e1) {
}
}
if (e.getSource() == btn2) {
sv.setVisible(true);
try {
File f1 = new File(sv.getDirectory(), sv.getFile());
FileWriter fw = new FileWriter(f1);
BufferedWriter bw = new BufferedWriter(fw);
String gt = tarea.getText();
bw.write(gt, 0, gt.length());
bw.flush();
fw.close();
} catch (Exception e2) {
}
}
if (e.getSource() == btn3) {
System.exit(0);
}
}
}
只要将你的代码写在button下的监听里面就可以了,你看一下我的这个代码,只是做了个简单的改动,就实现了
import java.awt.Button;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class jtxtfm {
public static void main(String args[]) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JButton jb = new JButton("打开对话框");
jf.add(jp);
jp.add(jb);
jf.setVisible(true);
jf.setSize(200,200);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtxtfrm fm = new jtxtfrm();
}
});
}
}
class jtxtfrm extends Frame implements ActionListener {
FileDialog op, sv;
Button btn1, btn2, btn3;
TextArea tarea;
jtxtfrm() {
super("读写文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600, 300);
setVisible(true);
btn1 = new Button("打开");
btn2 = new Button("保存");
btn3 = new Button("关闭");
tarea = new TextArea("");
add(btn1);
add(btn2);
add(btn3);
add(tarea);
tarea.setBounds(30, 50, 460, 220);
btn1.setBounds(520, 60, 50, 30);
btn2.setBounds(520, 120, 50, 30);
btn3.setBounds(520, 180, 50, 30);
op = new FileDialog(this, "打开", FileDialog.LOAD);
sv = new FileDialog(this, "保存", FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
String str;
op.setVisible(true);
try {
File f1 = new File(op.getDirectory(), op.getFile());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
tarea.setText("");
while ((str = br.readLine()) != null)
tarea.append(str + '\n');
fr.close();
} catch (Exception e1) {
}
}
if (e.getSource() == btn2) {
sv.setVisible(true);
try {
File f1 = new File(sv.getDirectory(), sv.getFile());
FileWriter fw = new FileWriter(f1);
BufferedWriter bw = new BufferedWriter(fw);
String gt = tarea.getText();
bw.write(gt, 0, gt.length());
bw.flush();
fw.close();
} catch (Exception e2) {
}
}
if (e.getSource() == btn3) {
System.exit(0);
}
}
}
只要将你的代码写在button下的监听里面就可以了,你看一下我的这个代码,只是做了个简单的改动,就实现了
展开全部
你在自己的程序里是使用
public static void main(String args[]){
jtxtfrm fm=new jtxtfrm();
}
这样来运行的 你只要在 一个按钮注册的事件里写上
jtxtfrm fm=new jtxtfrm();
就能实现效果
public static void main(String args[]){
jtxtfrm fm=new jtxtfrm();
}
这样来运行的 你只要在 一个按钮注册的事件里写上
jtxtfrm fm=new jtxtfrm();
就能实现效果
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
按钮触发事件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询