JList中能否添加 JLabel、JPanel组件
importjava.awt.BorderLayout;importjavax.swing.DefaultListModel;importjavax.swing.JFra...
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class myList extends JPanel
{
private static final long serialVersionUID = 1L;
JList mList;
DefaultListModel mode;
public myList()
{
setLayout(new BorderLayout());
mode = new DefaultListModel();
for(int i=0;i<10;i++)
mode.addElement(new JLabel("123"));
mList = new JList(mode);
add(new JScrollPane(mList),BorderLayout.CENTER);
}
public static void main(String[] args)
{
JFrame jf=new JFrame();
jf.add(new myList(),BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这个程序运行的结果是字串,怎么才能让他显示组件呢?
高手们能不能帮小的改一下,让它能够正确的显示组件的呢? 展开
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class myList extends JPanel
{
private static final long serialVersionUID = 1L;
JList mList;
DefaultListModel mode;
public myList()
{
setLayout(new BorderLayout());
mode = new DefaultListModel();
for(int i=0;i<10;i++)
mode.addElement(new JLabel("123"));
mList = new JList(mode);
add(new JScrollPane(mList),BorderLayout.CENTER);
}
public static void main(String[] args)
{
JFrame jf=new JFrame();
jf.add(new myList(),BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这个程序运行的结果是字串,怎么才能让他显示组件呢?
高手们能不能帮小的改一下,让它能够正确的显示组件的呢? 展开
展开全部
默认的就是JLabel了。
如果你要该我给你一个样板。
public class myList extends JPanel {
private static final long serialVersionUID = 1L;
JList mList;
DefaultListModel mode;
public myList() {
setLayout(new BorderLayout());
mode = new DefaultListModel();
for (int i = 0; i < 10; i++) {
mode.addElement("123");
}
mList = new JList(mode);
mList.setCellRenderer(new MyCellRenderer());
add(new JScrollPane(mList), BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.add(new myList(), BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MyCellRenderer extends JButton implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
setText(value.toString());
Color background;
Color foreground;
// check if this cell represents the current DnD drop location
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null && !dropLocation.isInsert() && dropLocation.getIndex() == index) {
background = Color.BLUE;
foreground = Color.WHITE;
// check if this cell is selected
} else if (isSelected) {
background = Color.RED;
foreground = Color.WHITE;
// unselected, and not the DnD drop location
} else {
background = Color.WHITE;
foreground = Color.BLACK;
}
setBackground(background);
setForeground(foreground);
return this;
}
}
}
listCellRenderer是控制显示的,我这里是button,你也可以换成别的,不建议使用jPanel,panel无法显示字体,所以你要自己弄个控件显示。
Model只负责保存数据,和界面无关,不要把界面也放到这里了。
如果你要该我给你一个样板。
public class myList extends JPanel {
private static final long serialVersionUID = 1L;
JList mList;
DefaultListModel mode;
public myList() {
setLayout(new BorderLayout());
mode = new DefaultListModel();
for (int i = 0; i < 10; i++) {
mode.addElement("123");
}
mList = new JList(mode);
mList.setCellRenderer(new MyCellRenderer());
add(new JScrollPane(mList), BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.add(new myList(), BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MyCellRenderer extends JButton implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
setText(value.toString());
Color background;
Color foreground;
// check if this cell represents the current DnD drop location
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null && !dropLocation.isInsert() && dropLocation.getIndex() == index) {
background = Color.BLUE;
foreground = Color.WHITE;
// check if this cell is selected
} else if (isSelected) {
background = Color.RED;
foreground = Color.WHITE;
// unselected, and not the DnD drop location
} else {
background = Color.WHITE;
foreground = Color.BLACK;
}
setBackground(background);
setForeground(foreground);
return this;
}
}
}
listCellRenderer是控制显示的,我这里是button,你也可以换成别的,不建议使用jPanel,panel无法显示字体,所以你要自己弄个控件显示。
Model只负责保存数据,和界面无关,不要把界面也放到这里了。
展开全部
那个你直接把那几个new JLabel("123") 放入集合里边了 当然只能打印它的toString方法了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这就已经是显示在jpanel这个组件里面了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询