以下一段java程序,看不懂,求加下注释,讲解下,同时给下运行结果(原题是那样要求的)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以下一段java程序,看不懂,求加下注释,讲解下,同时给下运行结果(原题是那样要求的)相关的知识,希望对你有一定的参考价值。

import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Ch71 extends Frame
int n;
String dirname="/jdk1.4";
File file=new File(dirname);
String[] flist=new String[100];
Choice listFile=new Choice();
TextField tf=new TextField();
public Ch71()
setLayout(new BorderLayout(10,10));
flist=file.list();
for (n=0;n<flist.length;n++)
listFile.addItem(flist[n]);

add("North",listFile);
add("South",tf);
setSize(250,200);
setVisible(true);
listFile.addItemListener(new koListener());
addWindowListener(new koWindowListener());

public static void main(String args[])
new Ch71();

class koListener implements ItemListener
public void itemStateChanged(ItemEvent e)
String ko=(String)e.getItem();
for (n=0;n<flist.length;n++)
if (ko==flist[n])
File f=new File(file+System.getProperty("file.separator")+ko);
if (f.isFile()==true)
tf.setText(ko+"是个文件!");
if (f.isDirectory()==true)
tf.setText(ko+"是个目录!");



class koWindowListener extends WindowAdapter
public void windowClosing(WindowEvent e)
dispose();
System.exit(0);



仔细读下面的JAVA语言源程序,自己给出程序的运行结果,理解File类的使用。

希望能帮助你



import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

public class Ch71 extends Frame 

    int n;// 循环时使用的变量
    String dirname = "c:/";// 文件遍历目录路径
    File file = new File(dirname);// 打开文件
    String[] flist = new String[100];// 定义数组
    Choice listFile = new Choice();// 定义选择框
    TextField tf = new TextField();  // 定义文本框

    public Ch71() 
        setLayout(new BorderLayout(10, 10));// 设置布局方式
        flist = file.list();//获取指定目录下面的文件和文件夹
        for (n = 0; n < flist.length; n++) //遍历文件
            listFile.addItem(flist[n]);//选择框添加单元
        
        add("North", listFile);// 把选择框放置窗口北方
        add("South", tf);//把文本框放置在窗口南方
        setSize(250, 200);//设置窗口大小
        setVisible(true);// 设置窗口可见
        listFile.addItemListener(new koListener());// 选择框添加选择监听事件
        addWindowListener(new koWindowListener());// 添加窗口事件
    

    public static void main(String args[]) 
        new Ch71();// 启动小程序
    

    class koListener implements ItemListener // 选择框监听类

        public void itemStateChanged(ItemEvent e) // 选中状态发生变化
            String ko = (String) e.getItem();// 获取选择框选中文本
            for (n = 0; n < flist.length; n++)
                if (ko == flist[n]) // 判断文件是否是文件还是目录
                    File f = new File(file + System.getProperty("file.separator") + ko);
                    if (f.isFile() == true) tf.setText(ko + "是个文件!");
                    if (f.isDirectory() == true) tf.setText(ko + "是个目录!");
                
        
    

    class koWindowListener extends WindowAdapter // 窗口监听类

        public void windowClosing(WindowEvent e) // 窗口即将关闭时
            dispose();// 窗口隐藏
            System.exit(0);// 程序退出
        
    

参考技术A 我运行了一下,有异常!java.lang.NullPointerException
我是JAVA图形界面的内容,我工作近一年了,没有用到,我的同学朋友也没有谁用,我的建议是,不要研究它,否则英雄无用武之地,这些代码,我全部能看得懂。
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Ch71 extends Frame
int n;
String dirname = "/jdk1.4";//这应该是路径,你写不对了
File file = new File(dirname);
String[] flist = new String[100];
Choice listFile = new Choice();//选择的组件
TextField tf = new TextField();//文本框
public Ch71()
setLayout(new BorderLayout(10, 10));//布局管理器
flist = file.list();//这样会列出所有的文件及文件夹
for (n = 0; n < flist.length; n++) //这行报错了,主要是遍历添加到选项中
listFile.addItem(flist[n]);

add("North", listFile);//父类的方法
add("South", tf);
setSize(250, 200);
setVisible(true);
listFile.addItemListener(new koListener());//注册监听
addWindowListener(new koWindowListener());//注册监听

public static void main(String args[])
new Ch71();

class koListener implements ItemListener
public void itemStateChanged(ItemEvent e) //当选中某项后,触发
String ko = (String) e.getItem();
for (n = 0; n < flist.length; n++)//遍历所有的选项
if (ko == flist[n])
File f = new File(file
+ System.getProperty("file.separator") + ko);//类加载,找这个file.separator文件
if (f.isFile() == true)
tf.setText(ko + "是个文件!");
if (f.isDirectory() == true)
tf.setText(ko + "是个目录!");



class koWindowListener extends WindowAdapter
public void windowClosing(WindowEvent e) //关闭,就像浏览器的关闭那个叉
dispose();
System.exit(0);//把JVM关闭了,程序就彻底停了


追问

这个是一个学校的实验报告,我都无语了,什么玩意啊,就是要求写出程序的结果

追答

有高手回答了,这就是学校坑人的地方

参考技术B 路过,太难了,玩了一下搞晕了追问

我感觉出题的就有问题,这个代码都有问题,还问运行结果。在eclipse我都晕了,什么结果都没有

追答

有结果的

是个异常

帮忙注释下下面这段java各行的意思,完全看不懂

package lipei;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**********
* 该类继承自JApplet,可作为一个applet程序运行于web客户端*/
public class MyCalendar extends JApplet

//定义一些字符串常量,用来表示周日至周六
public static final String WEEK_SUN = "SUN";
public static final String WEEK_MON = "MON";
public static final String WEEK_TUE = "TUE";
public static final String WEEK_WED = "WED";
public static final String WEEK_THU = "THU";
public static final String WEEK_FRI = "FRI";
public static final String WEEK_SAT = "SAT";

//定义一些颜色常量,用来表示个部分组件的颜色
public static final Color background = Color.white;
public static final Color foreground = Color.black;
public static final Color headerBackground = Color.blue;
public static final Color headerForeground = Color.white;
public static final Color selectedBackground = Color.blue;
public static final Color selectedForeground = Color.white;

//定义一些成员变量,表示图形用户界面的各个主要部件
private JPanel cPane;
private JLabel yearsLabel;
private JSpinner yearsSpinner;
private JLabel monthsLabel;
private JComboBox monthsComboBox;
private JTable daysTable;
private AbstractTableModel daysModel;
private Calendar calendar;

//默认无参构造函数,在该类被载入时执行,对cPane进行初始化。
public MyCalendar()
cPane = (JPanel) getContentPane();
//获取该applet的工作界面,用以初始化cPane,cPane引用了该APPLET的工作界面。


//init()初始化方法,处于applet生命周期的第一步,在该applet被载入时,由applet容器自动调用。
public void init()
cPane.setLayout(new BorderLayout());//设置cPane的布局管理器为BorderLayout

calendar = Calendar.getInstance(); //获得日期类的实例,具体可参看API文档
yearsLabel = new JLabel("Year: "); //一个label,显示Year:
yearsSpinner = new JSpinner(); //一个JSpinner,就是带上下箭头选择年份的那个
//一下都是对这个年份选择器的定义,包括取值范围、跟月份的联动等。
yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));
yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
yearsSpinner.addChangeListener(new ChangeListener()
public void stateChanged(ChangeEvent changeEvent)
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
updateView();

);

//以下为一个Panel,就是最上方显示年月的那一条。。
JPanel yearMonthPanel = new JPanel();
cPane.add(yearMonthPanel, BorderLayout.NORTH);
yearMonthPanel.setLayout(new BorderLayout());
yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);
JPanel yearPanel = new JPanel();
yearMonthPanel.add(yearPanel, BorderLayout.WEST);
yearPanel.setLayout(new BorderLayout());
yearPanel.add(yearsLabel, BorderLayout.WEST);
yearPanel.add(yearsSpinner, BorderLayout.CENTER);

//以下为界面第一行右边显示月份的那个下拉框以及标签。
monthsLabel = new JLabel("Month: ");
monthsComboBox = new JComboBox();
for (int i = 1; i <= 12; i++)
monthsComboBox.addItem(new Integer(i));

monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
monthsComboBox.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent actionEvent)
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
updateView();

);
JPanel monthPanel = new JPanel();
yearMonthPanel.add(monthPanel, BorderLayout.EAST);
monthPanel.setLayout(new BorderLayout());
monthPanel.add(monthsLabel, BorderLayout.WEST);
monthPanel.add(monthsComboBox, BorderLayout.CENTER);

/****
* 以下这部分比较复杂,是实现了一个匿名内部类,用以做日期表的数据模型
* 关于这一部分内容,请参看JTable类以及TableModel等相关类的类文档
* JTable是swing中比较难掌握,也是很强大的一个组件*/
daysModel = new AbstractTableModel()
public int getRowCount()
return 7;


public int getColumnCount()
return 7;


public Object getValueAt(int row, int column)
if (row == 0)
return getHeader(column);

row--;
Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int index = row * 7 + column;
int dayIndex = index - moreDayCount + 1;
if (index < moreDayCount || dayIndex > dayCount)
return null;
else
return new Integer(dayIndex);


;

/*****
* 以下部分是日期表JTable的各项设置,具体去看API文档吧,在这儿不好解释*/
daysTable = new CalendarTable(daysModel, calendar);
daysTable.setCellSelectionEnabled(true);
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer()
//此处又用了一个匿名内部类
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
String text = (value == null) ? "" : value.toString();
JLabel cell = new JLabel(text);
cell.setOpaque(true);
if (row == 0)
cell.setForeground(headerForeground);
cell.setBackground(headerBackground);
else
if (isSelected)
cell.setForeground(selectedForeground);
cell.setBackground(selectedBackground);
else
cell.setForeground(foreground);
cell.setBackground(background);



return cell;

);
updateView();

//将日期表添加到该Applet的工作界面中
cPane.add(daysTable, BorderLayout.CENTER);


public static String getHeader(int index)
switch (index)
case 0:
return WEEK_SUN;
case 1:
return WEEK_MON;
case 2:
return WEEK_TUE;
case 3:
return WEEK_WED;
case 4:
return WEEK_THU;
case 5:
return WEEK_FRI;
case 6:
return WEEK_SAT;
default:
return null;



/***
* 以下方法提供刷新日期表
* */
public void updateView()
daysModel.fireTableDataChanged();
daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
calendar.get(Calendar.WEEK_OF_MONTH));
daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1,
calendar.get(Calendar.DAY_OF_WEEK) - 1);

/*
*该类继承了JTable类,使其更适合用来显示日期表
* */
public static class CalendarTable extends JTable
private Calendar calendar;
public CalendarTable(TableModel model, Calendar calendar)
super(model);
this.calendar = calendar;

public void changeSelection(int row, int column, boolean toggle, boolean extend)
super.changeSelection(row, column, toggle, extend);
if (row == 0)
return;

Object obj = getValueAt(row, column);
if (obj != null)
calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());




public static void main(String[] args)
JFrame frame = new JFrame("Calendar Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCalendar myCalendar = new MyCalendar();
myCalendar.init();
frame.getContentPane().add(myCalendar);
frame.setSize(240, 172);
frame.show();


参考技术A import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class MyCalendar extends JApplet

public static final String WEEK_SUN = "SUN";
public static final String WEEK_MON = "MON";
public static final String WEEK_TUE = "TUE";
public static final String WEEK_WED = "WED";
public static final String WEEK_THU = "THU";
public static final String WEEK_FRI = "FRI";
public static final String WEEK_SAT = "SAT";

public static final Color background = Color.white;
public static final Color foreground = Color.black;
public static final Color headerBackground = Color.blue;
public static final Color headerForeground = Color.white;
public static final Color selectedBackground = Color.blue;
public static final Color selectedForeground = Color.white;

private JPanel cPane;
private JLabel yearsLabel;
private JSpinner yearsSpinner;
private JLabel monthsLabel;
private JComboBox monthsComboBox;
private JTable daysTable;
private AbstractTableModel daysModel;
private Calendar calendar;

public MyCalendar()
cPane = (JPanel) getContentPane();


public void init()
cPane.setLayout(new BorderLayout());

calendar = Calendar.getInstance();
calendar = Calendar.getInstance();
yearsLabel = new JLabel("Year: ");
yearsSpinner = new JSpinner();
yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));
yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
yearsSpinner.addChangeListener(new ChangeListener()
public void stateChanged(ChangeEvent changeEvent)
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
updateView();

);

JPanel yearMonthPanel = new JPanel();
cPane.add(yearMonthPanel, BorderLayout.NORTH);
yearMonthPanel.setLayout(new BorderLayout());
yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);
JPanel yearPanel = new JPanel();
yearMonthPanel.add(yearPanel, BorderLayout.WEST);
yearPanel.setLayout(new BorderLayout());
yearPanel.add(yearsLabel, BorderLayout.WEST);
yearPanel.add(yearsSpinner, BorderLayout.CENTER);

monthsLabel = new JLabel("Month: ");
monthsComboBox = new JComboBox();
for (int i = 1; i <= 12; i++)
monthsComboBox.addItem(new Integer(i));

monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
monthsComboBox.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent actionEvent)
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
updateView();

);
JPanel monthPanel = new JPanel();
yearMonthPanel.add(monthPanel, BorderLayout.EAST);
monthPanel.setLayout(new BorderLayout());
monthPanel.add(monthsLabel, BorderLayout.WEST);
monthPanel.add(monthsComboBox, BorderLayout.CENTER);

daysModel = new AbstractTableModel()
public int getRowCount()
return 7;


public int getColumnCount()
return 7;


public Object getValueAt(int row, int column)
if (row == 0)
return getHeader(column);

row--;
Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int index = row * 7 + column;
int dayIndex = index - moreDayCount + 1;
if (index < moreDayCount || dayIndex > dayCount)
return null;
else
return new Integer(dayIndex);


;

daysTable = new CalendarTable(daysModel, calendar);
daysTable.setCellSelectionEnabled(true);
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer()
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
String text = (value == null) ? "" : value.toString();
JLabel cell = new JLabel(text);
cell.setOpaque(true);
if (row == 0)
cell.setForeground(headerForeground);
cell.setBackground(headerBackground);
else
if (isSelected)
cell.setForeground(selectedForeground);
cell.setBackground(selectedBackground);
else
cell.setForeground(foreground);
cell.setBackground(background);



return cell;

);
updateView();

cPane.add(daysTable, BorderLayout.CENTER);


public static String getHeader(int index)
switch (index)
case 0:
return WEEK_SUN;
case 1:
return WEEK_MON;
case 2:
return WEEK_TUE;
case 3:
return WEEK_WED;
case 4:
return WEEK_THU;
case 5:
return WEEK_FRI;
case 6:
return WEEK_SAT;
default:
return null;



public void updateView()
daysModel.fireTableDataChanged();
daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
calendar.get(Calendar.WEEK_OF_MONTH));
daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1,
calendar.get(Calendar.DAY_OF_WEEK) - 1);

public static class CalendarTable extends JTable
private Calendar calendar;
public CalendarTable(TableModel model, Calendar calendar)
super(model);
this.calendar = calendar;

public void changeSelection(int row, int column, boolean toggle, boolean extend)
super.changeSelection(row, column, toggle, extend);
if (row == 0)
return;

Object obj = getValueAt(row, column);
if (obj != null)
calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());




public static void main(String[] args)
JFrame frame = new JFrame("Calendar Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCalendar myCalendar = new MyCalendar();
myCalendar.init();
frame.getContentPane().add(myCalendar);
frame.setSize(240, 172);
frame.show();



这里这里!!!
参考技术B 怎么没看到代码追问

太多了,我把它发给你吧

以上是关于以下一段java程序,看不懂,求加下注释,讲解下,同时给下运行结果(原题是那样要求的)的主要内容,如果未能解决你的问题,请参考以下文章

求java泛型的详细讲解,最基础的,我去网上博客里的啥都比较高深,看不懂

java中 index怎么用啊,这个关于index的代码看不懂,求讲解~~

一段程序看不懂,高手帮忙读一下

代码要写注释吗?写你就输了

求java学生成绩管理系统(简单点好,小弟初学怕看不懂)

JAVA读写XML,加下注释