如何在Java[Swing]中创建Listview?
如何在Java中显示简单的ListView?你能给我一个关于如何创建ListView的简单示例代码吗?你的意思是什么?请参阅java教程:你的意思是什么?请参阅java教程:看一看和API参考-它是如何在Java[Swing]中创建Listview?,java,swing,user-interface,jframe,jlist,Java,Swing,User Interface,Jframe,Jlist,如何在Java中显示简单的ListView?你能给我一个关于如何创建ListView的简单示例代码吗?你的意思是什么?请参阅java教程:你的意思是什么?请参阅java教程:看一看和API参考-它是列表-的一个实现 样本: public class Sample extends JFrame { public Sample(){ JEditorPane pane = new JEditorPane(); pane.setContentType("tex
列表-的一个实现
样本:
public class Sample extends JFrame {
public Sample(){
JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
pane.setText("<ol id='foo'><li>One</li><li>Two</li></ol>");
HTMLDocument doc = (HTMLDocument) pane.getDocument();
add(pane);
//Get the ref of foo element
Element ele=doc.getElement("foo");
ListView view=new ListView(ele);
System.out.println(ele.getElementCount());
try{
doc.insertBeforeEnd(ele.getElement(0), "<ul><li>Test");
}catch(Exception ex){}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);
}
}
公共类示例扩展JFrame{
公共样本(){
JEditorPane=新的JEditorPane();
pane.setContentType(“text/html”);
窗格.setText(“One Two ”;
HTMLDocument文档=(HTMLDocument)窗格。getDocument();
添加(窗格);
//获取foo元素的ref
Element ele=doc.getElement(“foo”);
ListView视图=新ListView(ele);
System.out.println(ele.getElementCount());
试一试{
文档insertbeforeed(ele.getElement(0),“- 测试”);
}捕获(例外情况除外){}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(300300);
setVisible(真);
}
}
看一看API参考-它是
列表的一个实现-
样本:
public class Sample extends JFrame {
public Sample(){
JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
pane.setText("<ol id='foo'><li>One</li><li>Two</li></ol>");
HTMLDocument doc = (HTMLDocument) pane.getDocument();
add(pane);
//Get the ref of foo element
Element ele=doc.getElement("foo");
ListView view=new ListView(ele);
System.out.println(ele.getElementCount());
try{
doc.insertBeforeEnd(ele.getElement(0), "<ul><li>Test");
}catch(Exception ex){}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);
}
}
公共类示例扩展JFrame{
公共样本(){
JEditorPane=新的JEditorPane();
pane.setContentType(“text/html”);
窗格.setText(“One Two ”;
HTMLDocument文档=(HTMLDocument)窗格。getDocument();
添加(窗格);
//获取foo元素的ref
Element ele=doc.getElement(“foo”);
ListView视图=新ListView(ele);
System.out.println(ele.getElementCount());
试一试{
文档insertbeforeed(ele.getElement(0),“- 测试”);
}捕获(例外情况除外){}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(300300);
setVisible(真);
}
}
根据JavaDocs,在呈现HTML时使用它,因此使用它只需加载一些使用HTML列表的HTML(
或
)
import java.awt.*;
导入javax.swing.*;
类showthlist{
公共静态void main(字符串[]args){
最终StringBuilder sb=新StringBuilder();
//在HTML感知组件中调用HTML呈现
某人加上(“”);
//开始(正文&)一个有序列表
某人加上(“”);
字体[]字体=图形环境。
getLocalGraphicsEnvironment().getAllFonts();
用于(字体:字体){
字符串名称=font.getName();
//将使用***列表视图呈现***
sb.追加(“”;
某人(姓名);
}
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
JScrollPane sp=新的JScrollPane(
新JLabel(sb.toString());
维度d=sp.getPreferredSize();
sp.setPreferredSize(新尺寸(d.width,150));
showMessageDialog(null,sp);
}
});
}
}
或者,正如许多其他回复所建议的那样,数据最好以JList
的形式呈现。我很惊讶我第一次看到它时没有投赞成票!我怀疑我太专注于跟踪链接了,以至于没有发现什么是ListView
。)现在更正。:)如果你能改变你答案的一个方面,我可以删除我的低劣答案。将“…list-
”更改为类似。。“.list-一个
或
的
元素”。最重要的部分是or
。让我知道你的决定。当我第一次看到它时,我很惊讶我没有放弃投票!我怀疑我太专注于跟踪链接了,以至于没有发现什么是ListView
。)现在更正。:)如果你能改变你答案的一个方面,我可以删除我的低劣答案。将“…list-
”更改为类似。。“.list-一个
或
的
元素”。最重要的部分是or
。让我知道你的决定。非常感谢你,先生。。这真的很有帮助lot@jcom不客气:)非常感谢你,先生。。这真的很有帮助lot@jcom不客气:)