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

zhuliqiufeng zhuliqiufeng 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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)

是如何调用的?


...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
zhuliqiufeng
Java SE 62,620 成员 307,251 内容