初学java看不懂关于接口的一个问题,请各位同僚帮忙,谢谢了

zhuliqiufeng 2004-02-02 01:36:35
package jlistdemo;

import javax.swing.UIManager;
import java.awt.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class JListDemo {
private boolean packFrame = false;

//Construct the application
public JListDemo() {
MainFrame frame = new MainFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
/* try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
*/
new JListDemo();
}
}









package jlistdemo;

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

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class MainFrame extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JList jList1 = new JList();
private String[] iconnames={"电脑","软件包","软盘驱动器","画图","公文包","网上邻居"};

private Icon icon1=new ImageIcon(ClassLoader.getSystemResource("images/1-3-1.jpg"));
private Icon icon2=new ImageIcon(ClassLoader.getSystemResource("images/1-3-2.jpg"));
private Icon icon3=new ImageIcon(ClassLoader.getSystemResource("images/1-3-3.jpg"));
private Icon icon4=new ImageIcon(ClassLoader.getSystemResource("images/1-3-4.jpg"));
private Icon icon5=new ImageIcon(ClassLoader.getSystemResource("images/1-3-5.jpg"));
private Icon icon6=new ImageIcon(ClassLoader.getSystemResource("images/1-3-6.jpg"));
//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(395, 243));
this.setTitle("JListDemo");
Icon[] icons={icon1,icon2,icon3,icon4,icon5,icon6};
jList1.setListData(this.iconnames);
jList1.setCellRenderer(new OwnCellRenderer(icons));
jList1.setFont(new java.awt.Font("Dialog", 0, 14));
contentPane.add(new JScrollPane(jList1), BorderLayout.CENTER);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}





package jlistdemo;

import javax.swing.ListCellRenderer;
import java.awt.Component;
import javax.swing.JList;
import javax.swing.*;
import java.awt.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class OwnCellRenderer extends JLabel implements ListCellRenderer {
private Icon[] icons;

public OwnCellRenderer(Icon[] icons) {
this.icons = icons;
this.setOpaque(true);
this.setFont(new Font("Dialog",Font.PLAIN,16));

}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
/**@todo Implement this javax.swing.ListCellRenderer method*/
if(value!=null){
String text = value.toString();
this.setText(text);
}
this.setIcon(icons[index]);
if(isSelected){
this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
}
else{

this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
return this;
//throw new java.lang.UnsupportedOperationException("Method getListCellRendererComponent() not yet implemented.");
}
}


问题是:方法 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)

是如何调用的?


...全文
65 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ustbzhangwei 2004-02-02
  • 打赏
  • 举报
回复
我告诉你一个办法,就可以知道某个方法的调有过程

以你的程序为例
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

new Exception().printStackTrace("我想知道这个方法的调用过程");
//这是利用制造一个异常,然后输出它的堆栈,从下向上是调用过程
//....
}





eric_uugames 2004-02-02
  • 打赏
  • 举报
回复
我以前用c开发项目,从头到尾就没看见工程目录里有main函数,我就猜测main函数在函数库.a文件里。

函数调用,由使用他的界面那部分的程序自动调用,他不管这个函数getListCellRendererComponent是干什么的,反正他调用这个函数。

你只需要把这个函数定义了就是了,否则等到界面程序向调用时就会报错说getListCellRendererComponent() not yet implemented

//------------------------------------------
理解了接口的作用,你就可以理解这个调用了,你可以再看看接口方面的文档
tan_jianhui 2004-02-02
  • 打赏
  • 举报
回复
有接口吗???

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧