想按下堆栈按钮时,调用JTable让这个控件显示在界面上,即显示出堆栈顺序,十分谢谢
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//界面设计
public class MyFrame extends JFrame{
JButton dzbt,dlbt,pxbt,dgbt,sbt,tbt,sjczbt,qtbt;
MyFrame(){
//对窗体进行设计
this.setSize(1000,800);
this.setTitle("java数据结构");
this.setLayout(null);
//按钮
dzbt=new JButton("堆栈");
dlbt=new JButton("队列");
pxbt=new JButton("排序");
dgbt=new JButton("递归");
sbt=new JButton("树");
tbt=new JButton("图");
sjczbt=new JButton("数据操作");
qtbt=new JButton("表达式");
dzbt.setBounds(0,0,125,50);
dlbt.setBounds(125,0,125,50);
pxbt.setBounds(250,0,125,50);
dgbt.setBounds(375,0,125,50);
sbt.setBounds(500,0,125,50);
tbt.setBounds(625,0,125,50);
sjczbt.setBounds(750,0,125,50);
qtbt.setBounds(875,0,125,50);
this.add(dzbt);
this.add(dlbt);
this.add(pxbt);
this.add(dgbt);
this.add(sbt);
this.add(tbt);
this.add(sjczbt);
this.add(qtbt);
/*dzbt.addActionListener(this);
dlbt.addActionListener(this);
pxbt.addActionListener(this);
dgbt.addActionListener(this);
sbt.addActionListener(this);
tbt.addActionListener(this);
sjczbt.addActionListener(this);
qtbt.addActionListener(this);*/
JLabel dmlb=new JLabel("代码区域");
dmlb.setBounds(0,52,70,20);
this.add(dmlb);
//创建一个文本区
JTextArea cdtxt = new JTextArea();
//设置当文本字长度超过frame的宽度时自动换行()
cdtxt.setLineWrap(true);
//创建一个滑动窗格容器,并将刚刚创建的文本区放入其中,当文本区textArea中的内容超过弹出框框frame的长度时。滑动就可以查看没显示出来的内容
JScrollPane scrollpane=new JScrollPane(cdtxt);
scrollpane.setBounds(0,74,375,725);
this.add(scrollpane);
//加载一下窗体上的事件
this.addWindowListener(new WindowAdapter()//窗口监听
{
public void windowClosing(WindowEvent e)
{
System.out.println("窗体执行关闭!");
System.exit(0);
}
});
//堆栈按钮功能
dzbt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CodeShow dzcode=new CodeShow();
dzcode.fileName="F:\\SchoolStudy\\javawork\\BlueWork\\堆栈.txt";
cdtxt.setText((dzcode.show(dzcode.fileName)));
cdtxt.setEditable(false);
JLabel rzlb=new JLabel("入栈数据");
rzlb.setBounds(725,74,70,30);
add(rzlb);
}
});
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//程序入口
public static void main(String[] args){
new MyFrame();
}
}
//用IO流从文档中读取要显示的代码
class CodeShow{
String fileName ;//每个存有代码文档的地址
BufferedReader reader = null;
String tempString = null;
String temp="";
String show(String fileName){
try {
reader = new BufferedReader(new FileReader(fileName));
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
temp=temp+tempString+"\n";
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return temp;
}
}