package JList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
public class JListDemo extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JList _list;//list
private JLabel _label;//显示选中item
private JButton _button;//获取选中item
private JTextField _textField;//输入框
private DefaultListModel _listModel;
private JPanel _containerPanel;
private JPanel _panel02;
private JPanel _panel03;
private JPanel _panel04;
public JListDemo(String title){
super(title);
this.setLayout(new GridLayout(1,2));
this.setBounds(200,300,300,200);
//添加子组件
this.createComponents();
//窗口事件监听
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
this.pack();
}
private void createComponents (){
//1.添加list(用scrollPane封装)
_listModel = new DefaultListModel();
_list = new JList ();
_list.setBorder(BorderFactory.createTitledBorder("你身在哪个国家?"));
JScrollPane scrollPane = new JScrollPane (_list);
//2.创建容器面板
_containerPanel = new JPanel();
_containerPanel.setLayout(new GridLayout(3,1,5,5));
//3.添加以上两个组件
this.add(scrollPane);
this.add(_containerPanel);
//4.配置容器面板
this.configContainerPanel();
}
/**配置容器面板*/
private void configContainerPanel(){
_panel02 = new JPanel();
_panel02.setLayout(new GridLayout(2,1));
_panel03 = new JPanel();
_panel03.setLayout(new GridLayout(2,1));
_panel04 = new JPanel();
_panel04.setLayout(new GridLayout(2,1));
_containerPanel.add(_panel02);
_containerPanel.add(_panel03);
_containerPanel.add(_panel04);
this.configPanel02();
this.configPanel03();
this.configPanel04();
}
/**配置子面板02*/
private void configPanel02(){
//1.添加显示的label
_label = new JLabel ("显示结果");
_label.setHorizontalAlignment(SwingConstants.CENTER);
//2.添加按钮
_button = new JButton("获取");
//->添加监听
_button.addActionListener(this);
//3.添加面板装label&&button
_panel02.add(_label);
_panel02.add(_button);
}
/**配置子面板03*/
private void configPanel03(){
_textField = new JTextField("输入内容");
_textField.setHorizontalAlignment(SwingConstants.CENTER);
JButton button = new JButton("添加");
button.addActionListener(this);
_panel03.add(_textField);
_panel03.add(button);
}
/**配置子面板04*/
private void configPanel04(){
JButton delete = new JButton("删除");
delete.addActionListener(this);
JButton clear = new JButton("清空");
clear.addActionListener(this);
_panel04.add(delete);
_panel04.add(clear);
}
@Override
public void actionPerformed(ActionEvent e) {
if (false == e.getSource() instanceof JButton){
return;
}
JButton btn = (JButton)e.getSource();
switch(btn.getText()){
case "获取":
StringBuffer strBuffer = new StringBuffer();
List<String> values=_list.getSelectedValuesList();
for (int i=0;i<values.size();i++){
String obj = values.get(i);
strBuffer.append(String.format("%s ",obj));
}
_label.setText(strBuffer.toString());
break;
case "添加":
_listModel.addElement(_textField.getText());
_list.setModel(_listModel);
break;
case "删除":
int index = _list.getSelectedIndex();
_listModel.remove(index);
_list.setModel(_listModel);
break;
case "清空":
_listModel.clear();
_list.setModel(_listModel);
break;
}
this.pack();
}
public static void main(String[] args) {
new JListDemo("JList").setVisible(true);
}
}
JList
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Scarlett Johansson had a new baby girl, she says that she...
- 虽然今何在是编剧之一,但严格来说,这不是今何在的《悟空传》,而是以《悟空传》在网文界大神级地位来吸引观众的电影。以...