我有一个应该显示4 x X元素列表的程序。我对这些列表并不是很好,所以我结合了几个例子做了很多研究。我终于为它做了一个名为TabListCellRenderer
的课程:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
class TabListCellRenderer extends JLabel implements ListCellRenderer{
protected static Border m_noFocusBorder;
protected FontMetrics m_fm = null;
protected Insets m_insets = new Insets(0, 0, 0, 0);
protected int m_defaultTab = 50;
protected int[] m_tabs = null;
public TabListCellRenderer() {
super();
m_noFocusBorder = new EmptyBorder(1, 1, 1, 1);
setOpaque(true);
setBorder(m_noFocusBorder);
}
public Component getListCellRendererComponent(JList list,Object value, int index, boolean isSelected, boolean cellHasFocus){
setText(value.toString());
setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
setFont(list.getFont());
setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : m_noFocusBorder);
return this;
}
public void setDefaultTab(int defaultTab) {
m_defaultTab = defaultTab;
}
public int getDefaultTab() {
return m_defaultTab;
}
public void setTabs(int[] tabs) {
m_tabs = tabs;
}
public int[] getTabs() {
return m_tabs;
}
public int getTab(int index) {
if (m_tabs == null)
return m_defaultTab*index;
int len = m_tabs.length;
if (index>=0 && index<len)
return m_tabs[index];
return m_tabs[len-1] + m_defaultTab*(index-len+1);
}
public void paint(Graphics g) {
m_fm = g.getFontMetrics();
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());
g.setColor(getForeground());
g.setFont(getFont());
m_insets = getInsets();
int x = m_insets.left;
int y = m_insets.top + m_fm.getAscent();
StringTokenizer st = new StringTokenizer(getText(), "\t");
while (st.hasMoreTokens()){
String sNext = st.nextToken();
g.drawString(sNext, x, y);
x += m_fm.stringWidth(sNext);
if (!st.hasMoreTokens())
break;
int index = 0;
while (x >= getTab(index))
index++;
x = getTab(index);
}
}
}
然后我通过在JPanel上运行下一段代码来初始化列表:
TabListCellRenderer renderer = new TabListCellRenderer();
renderer.setTabs(new int[] {50, 200, 300});
listaAnimali.setCellRenderer(renderer);
如果我运行这3行,我的Eclipse会冻结几分钟(3-5)然后sais表示3行中的最后一行导致了NullPointerException
:
Exception in thread "main" java.lang.NullPointerException
at classe_di_interfaccia.MyPanel.<init>(MyPanel.java:76)
at classe_di_interfaccia.TestClass.main(TestClass.java:9)
嗨,你可以看到列表中没有对类的参考。我试着在脑子里倒退,但据我所知,一切都要检查出来。
我在eclipse上尝试了调试选项但是崩溃了程序。 (没有错误代码。它只是Windows系统显示如下:“程序没有响应,你要关闭它吗?”)
我错过了什么?这是日食的问题吗?为什么日食这么慢?
Normaly它工作正常(我刺激了NullPointerException)
使用这三个类我可以让它再次出错(使用geany但速度更快,如3秒)
面板示例:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Panel extends JPanel {
private JPanel PanelOnPanel
private JList listaAnimali;
public PanelGrafica(){
TabListCellRenderer renderer = new TabListCellRenderer();
renderer.setTabs(new int[] {50, 200, 300});
listaAnimali.setCellRenderer(renderer);
panelList.add(listaAnimali);
}
}
的JFrame:
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
public MyFrame(){
super();
setBounds(100,100,350,600);
}
public MyFrame(String titolo){
super(titolo);
setBounds(100,100, 350,600);
}
}
要测试的课程:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JTabbedPane;
public class Test{
public static void main(String[] v){
JFrame f = new MyFrame("Una Mensola Automatico");
Container c = f.getContentPane();
JPanel panel = new PanelGrafica();
f.setResizable(false);
f.show();
}
}