当前位置: 技术问答>java相关
在JList里加CHECKBOX
来源: 互联网 发布时间:2015-11-11
本文导语: 在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_l...
在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.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