java代码练习第8课
发布于 2021-09-22 04:11
test035.java
package pack02;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
public class test035 extends JFrame
{
public static void main(String[] args)
{
new test035();
}
public test035()
{
this.setTitle("列表框");
Container container1 = this.getContentPane();
this.setLayout(new GridLayout(3,1,5,5));
String[] contents1 = new String[] {"String1","String2","String3","String4","String5"};
JList<String> list1 = new JList<>(contents1);
JScrollPane scrollPane1 = new JScrollPane(list1);
Vector<String> content2 = new Vector<>();
JList<String> list2 = new JList<>(content2);
content2.add("Vector1");
content2.add("Vector2");
content2.add("Vector3");
content2.add("Vector4");
content2.add("Vector5");
JScrollPane scrollPane2 = new JScrollPane(list2);
JList<String> list3 = new JList<>(new MyListModel1());
JScrollPane scrollPane3 = new JScrollPane(list3);
container1.add(scrollPane1);
container1.add(scrollPane2);
container1.add(scrollPane3);
this.setSize(500,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class MyListModel1 extends AbstractListModel<String>
{
private String[] contents = new String[] {"AbstractListModel1","AbstractListModel2","AbstractListModel3","AbstractListModel4","AbstractListModel5"};
public String getElementAt(int x)
{
if ( x < contents.length)
{
return contents[x++];
}
else
{
return null;
}
}
public int getSize()
{
return contents.length;
}
}
test036.java
package pack02;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
public class test036 extends JFrame
{
public static void main(String[] args)
{
new test036();
}
public test036()
{
this.setTitle("DefaultListModel");
this.setLayout(new GridLayout(2,1,5,5));
Container container1 = this.getContentPane();
JList<String> list1 = new JList<>(new MyDefaultListModel());
JScrollPane scrollPane1 = new JScrollPane(list1);
container1.add(scrollPane1);
final String[] flavors = new String[] {"列表1","列表2","列表3","列表4","列表5"};
final DefaultListModel<String> items = new DefaultListModel<>();
final JList<String> list2 = new JList<>(items);
for (int i = 0; i < flavors.length; i++)
{
items.add(i, flavors[i]);
}
JScrollPane scrollPane2 = new JScrollPane(list2);
container1.add(scrollPane2);
this.setSize(500,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class MyDefaultListModel extends DefaultListModel<String>
{
private String[] contents = new String[] {"DefaultListModel1","DefaultListModel2","DefaultListModel3","DefaultListModel4","DefaultListModel5"};
public String getElementAt(int x)
{
if (x < contents.length)
{
return contents[x++];
}
else
{
return null;
}
}
public int getSize()
{
return contents.length;
}
}
码农新势力
码农新势力是分享学习程序开发和代码编写的个人,今后会主打虾米大王教你学编程系列教程,让我们在代码的世界里共同成长。
力哥理财
理财就是理生活,投资就是投人性;反对一切极端化,平衡才是真王道~
本文来自网络或网友投稿,如有侵犯您的权益,请发邮件至:aisoutu@outlook.com 我们将第一时间删除。
相关素材