当前位置: 技术问答>java相关
急!!!如何将JList和CheckBox一起使用?
来源: 互联网 发布时间:2015-10-04
本文导语: 我想在JList中显示一组CheckBox,然后根据不同的选项作不同的事情, | 来了呵呵费了半天事 分拿来 import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; im...
我想在JList中显示一组CheckBox,然后根据不同的选项作不同的事情,
|
来了呵呵费了半天事
分拿来
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class CheckBoxList
extends JFrame
{
protected JList m_list;
protected JLabel m_total;
public CheckBoxList()
{
super("Swing List [Check boxes]");
setSize(260, 240);
getContentPane().setLayout(new FlowLayout());
InstallData[] options = {
new InstallData("Program executable", 118),
new InstallData("Help files", 52),
new InstallData("Tools and converters", 83),
new InstallData("Source code", 133)
};
m_list = new JList(options);
CheckListCellRenderer renderer = new CheckListCellRenderer();
m_list.setCellRenderer(renderer);
m_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
CheckListener lst = new CheckListener(this);
m_list.addMouseListener(lst);
m_list.addKeyListener(lst);
JScrollPane ps = new JScrollPane();
ps.getViewport().add(m_list);
m_total = new JLabel("Space required: 0K");
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(ps, BorderLayout.CENTER);
p.add(m_total, BorderLayout.SOUTH);
p.setBorder(new TitledBorder(new EtchedBorder(),
"Please select options:") );
getContentPane().add(p);
WindowListener wndCloser = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
recalcTotal();
}
public void recalcTotal()
{
ListModel model = m_list.getModel();
int total = 0;
for (int k=0; k
分拿来
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class CheckBoxList
extends JFrame
{
protected JList m_list;
protected JLabel m_total;
public CheckBoxList()
{
super("Swing List [Check boxes]");
setSize(260, 240);
getContentPane().setLayout(new FlowLayout());
InstallData[] options = {
new InstallData("Program executable", 118),
new InstallData("Help files", 52),
new InstallData("Tools and converters", 83),
new InstallData("Source code", 133)
};
m_list = new JList(options);
CheckListCellRenderer renderer = new CheckListCellRenderer();
m_list.setCellRenderer(renderer);
m_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
CheckListener lst = new CheckListener(this);
m_list.addMouseListener(lst);
m_list.addKeyListener(lst);
JScrollPane ps = new JScrollPane();
ps.getViewport().add(m_list);
m_total = new JLabel("Space required: 0K");
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(ps, BorderLayout.CENTER);
p.add(m_total, BorderLayout.SOUTH);
p.setBorder(new TitledBorder(new EtchedBorder(),
"Please select options:") );
getContentPane().add(p);
WindowListener wndCloser = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
recalcTotal();
}
public void recalcTotal()
{
ListModel model = m_list.getModel();
int total = 0;
for (int k=0; k