◆幻岁◆ liuhan65731587
既然宇宙由黑暗凝成,却为何允许群星的光芒闪烁?既然光明能照亮一切,却为何无法离开黑暗的怀抱?
关注数: 12 粉丝数: 31 发帖数: 1,221 关注贴吧数: 41
如何在在jar包中引用资源 import java.awt.*;//布局 import javax.swing.*;//事件 import javax.xml.soap.Text; import java.awt.event.*;//组件 import java.io.*; public class Ex1 extends JFrame implements ActionListener,Runnable{ JLabel jl;// JTextField jtf;//放待选人员 JTextArea jta;//选中的人 JScrollPane jsp; JButton jb[];//三个按钮 开始 结束 清空 JPanel jp1,jp2;//次级容器 String fileName="人员名单.txt"; String jbName[]={"选择","清空","退出"}; String str1[],str=null; int key=0; public Ex1() throws IOException{ setTitle("随机选人系统"); jtf=new JTextField(5); jta=new JTextArea(5,5); jsp=new JScrollPane(jta); jl=new JLabel("待选人员"); jb=new JButton[3];//大小申明 str1=readFile(fileName); for(int i=0;i<3;i++){ jb[i]=new JButton(jbName[i]);//创建按钮 jb[i].addActionListener(this);//注册事件监听器 } jp1=new JPanel(); jp2=new JPanel(); jp1.setLayout(new GridLayout(2,1)); jp2.setLayout(new GridLayout(3,1)); jp1.add(jl); jp1.add(jtf); for(int i=0;i<3;i++){ jp2.add(jb[i]); } setLayout(new BorderLayout()); add(jp1,BorderLayout.WEST); add(jp2,BorderLayout.CENTER); add(jsp,BorderLayout.EAST); pack();//合适大小 setVisible(true);//可见 setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭有效 } public static void main(String[] args) throws IOException{ new Thread(new Ex1()).start(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jb[0]){//点击 选择 时 jta.append(str+"\n"); } else if(e.getSource()==jb[1]){//点击 清空时 jta.setText(""); } else if(e.getSource()==jb[2]){ dispose(); } } public String[] readFile(String fileName) throws IOException//读取菜单 { //读取文本文件fileName的内容,并将内容返回 try{ File file=new File("./src/Name/",fileName); //File file=new File(Text.class.getResource("/Name").getFile(),fileName); //System.out.println(Text.class.getResource("/Name/")); //System.out.println(fileName); FileReader f=new FileReader(file); BufferedReader reader=new BufferedReader(f); int i=0; String tempString= null,str2[]=new String[50]; while((tempString=reader.readLine())!=null) { str2[i]=tempString; i++; } return str2; } catch(FileNotFoundException e){ String str[]={"没找人员名单到文件"}; return str; } } public void run() { for(int i=0;;i++){ try { str=str1[i]; jtf.setText(str); if(str1[i+1]==null)i=0; Thread.sleep(200); } catch (InterruptedException e1) { e1.printStackTrace(); } } } }
1 下一页