JAVA程序问题 10
packageapple6;importjavax.swing.*;importjava.awt.Event.*;importjava.awt.event.ItemEve...
package apple6;
import javax.swing.*;
import java.awt.Event.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class LJFrame extends JFrame implements ItemListener{
private JComboBox jc;
private URL url;
private JTextArea ta;
private JTextField tf;
public LJFrame(){
super("浏览器");
this.setSize(640,480);
this.setLocation(300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
String[] urls={"file://localhost/C:/Program Files/Java/docs/api/index.html","http://www.baidu.com"};
jc=new JComboBox(urls);
jc.setEditable(true);
jc.addItemListener(this);
this.add(jc,"North");
ta=new JTextArea();
this.add(ta);
tf=new JTextField();
this.add(tf,"South");
this.setVisible(true);
try{
this.url=new URL(urls[0]);
this.readFromFile();
tf.setText(this.getAtribute());}
catch(MalformedURLException nurle){
System.out.println(nurle);
}
}
public void readFromFile(){
try{
InputStreamReader in=new InputStreamReader(this.url.openStream());
BufferedReader bin=new BufferedReader(in);
String aline="";
do{
aline=bin.readLine();
if(aline!=null)
ta.append(aline+"\n");
}while(aline!=null);
bin.close();
in.close();
}
catch(IOException ioe){
System.out.println(ioe);
}
}
public String getAtribute(){
String str="文件 :";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm");
if(url.getProtocol().equals("file")){
File afile = new File(url.getFile());
str +=afile.getAbsolutePath()+"\t";
str +=afile.length()+"B\t";
str +=sdf.format(new Date(afile.lastModified()));
}
if(url.getProtocol().equals("http")){
try{
URLConnection uc = url.openConnection();
str+=url.toString()+"\t";
str+=uc.getContentLength()+"B\t";
str+= uc.getContentType()+"\t";
str+=sdf.format(new Date(uc.getLastModified()));
}
catch(IOException ioe){
System.out.println(ioe);
}
}
return str;
}
public void iteamStateChanged(ItemEvent e){
String un =(String)jc.getSelectedItem();
try{
this.url=new URL(un);
this.readFromFile();
tf.setText(this.getAtribute());
}
catch(MalformedURLException nurle){
System.out.println(nurle);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new LJFrame();
}
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
}
谁能帮我看哈,好像那个复选框没有起到作用。。。
不好意思,我找到原因了,public void iteamStateChanged(ItemEvent e){
String un =(String)jc.getSelectedItem();
try{
this.url=new URL(un);
this.readFromFile();
tf.setText(this.getAtribute());
}
catch(MalformedURLException nurle){
System.out.println(nurle);
}
}
中,方法名出错了,应该是itemStateChanged.麻烦各位了 展开
import javax.swing.*;
import java.awt.Event.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class LJFrame extends JFrame implements ItemListener{
private JComboBox jc;
private URL url;
private JTextArea ta;
private JTextField tf;
public LJFrame(){
super("浏览器");
this.setSize(640,480);
this.setLocation(300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
String[] urls={"file://localhost/C:/Program Files/Java/docs/api/index.html","http://www.baidu.com"};
jc=new JComboBox(urls);
jc.setEditable(true);
jc.addItemListener(this);
this.add(jc,"North");
ta=new JTextArea();
this.add(ta);
tf=new JTextField();
this.add(tf,"South");
this.setVisible(true);
try{
this.url=new URL(urls[0]);
this.readFromFile();
tf.setText(this.getAtribute());}
catch(MalformedURLException nurle){
System.out.println(nurle);
}
}
public void readFromFile(){
try{
InputStreamReader in=new InputStreamReader(this.url.openStream());
BufferedReader bin=new BufferedReader(in);
String aline="";
do{
aline=bin.readLine();
if(aline!=null)
ta.append(aline+"\n");
}while(aline!=null);
bin.close();
in.close();
}
catch(IOException ioe){
System.out.println(ioe);
}
}
public String getAtribute(){
String str="文件 :";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm");
if(url.getProtocol().equals("file")){
File afile = new File(url.getFile());
str +=afile.getAbsolutePath()+"\t";
str +=afile.length()+"B\t";
str +=sdf.format(new Date(afile.lastModified()));
}
if(url.getProtocol().equals("http")){
try{
URLConnection uc = url.openConnection();
str+=url.toString()+"\t";
str+=uc.getContentLength()+"B\t";
str+= uc.getContentType()+"\t";
str+=sdf.format(new Date(uc.getLastModified()));
}
catch(IOException ioe){
System.out.println(ioe);
}
}
return str;
}
public void iteamStateChanged(ItemEvent e){
String un =(String)jc.getSelectedItem();
try{
this.url=new URL(un);
this.readFromFile();
tf.setText(this.getAtribute());
}
catch(MalformedURLException nurle){
System.out.println(nurle);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new LJFrame();
}
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
}
谁能帮我看哈,好像那个复选框没有起到作用。。。
不好意思,我找到原因了,public void iteamStateChanged(ItemEvent e){
String un =(String)jc.getSelectedItem();
try{
this.url=new URL(un);
this.readFromFile();
tf.setText(this.getAtribute());
}
catch(MalformedURLException nurle){
System.out.println(nurle);
}
}
中,方法名出错了,应该是itemStateChanged.麻烦各位了 展开
3个回答
展开全部
你在添加选框的时候用的是什么布局?如果是边框布局,那个要放在NORTH位置就得调用BorderLayout.NORTH这样的格式了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
找到原因了!恭喜了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询