我有一个程序应该显示 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),然后说这 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系统显示:“程序没有响应,你要关闭它吗?”)
我错过了什么?是eclipse的问题吗?为什么日食这么慢?
Normaly 它工作正常(我刺激了 NullPointerException)
使用这 3 个类,我可以让它再次出错(使用 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);
}
}
框架:
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();
}
}