package com.swing01;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.UIManager;
import java.awt.GridLayout;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ListSelectionModel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
public class JListSelectModelTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = -1203078038045289207L;
//面板
private JPanel contentPane;
//列表1
private JList list1;
private JList list2;
private JList list3;
//标签
private JLabel label1;
private JLabel label2;
private JLabel label3;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JListSelectModelTest frame = new JListSelectModelTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JListSelectModelTest() {
addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
do_this_windowActivated(e);
}
});
//标题
setTitle("力天教育:列表框的显示模式");
//退出时关闭虚拟机
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//大小及显示位置
setBounds(100, 100, 450, 300);
//面板容器
contentPane = new JPanel();
//面板容器的边框
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//布局格式
contentPane.setLayout(new BorderLayout(0, 0));
//将面板容器加入到我们的窗体中
setContentPane(contentPane);
//面板创建
JPanel panel = new JPanel();
//将面板另入到容器面板中
contentPane.add(panel, BorderLayout.CENTER);
//面板布局格式
panel.setLayout(new GridLayout(1, 3, 5, 5));
//滚动条
JScrollPane scrollPane1 = new JScrollPane();
//为面板添加滚动条
panel.add(scrollPane1);
//列表的创建
list1 = new JList();
//设置列表的标题
list1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
//setSelectionMode确定允许单项选择还是多项选择
//ListSelectionModel该组件显示一个具有稳定索引的值列表
//SINGLE_SELECTION一次选择一个列表索引
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//创建一个视口(如果有必要)并设置其视图
scrollPane1.setViewportView(list1);
label1 = new JLabel("单项选择按钮");
label1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
//setHorizontalAlignment设置标签内容沿 X 轴的对齐方式
//SwingConstants通常用于在屏幕上定位或定向组件的常量的集合
label1.setHorizontalAlignment(SwingConstants.CENTER);
//设置其视图然后将此列标题视口添加到滚动窗格
scrollPane1.setColumnHeaderView(label1);
JScrollPane scrollPane2 = new JScrollPane();
panel.add(scrollPane2);
list2 = new JList();
list2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
//SINGLE_INTERVAL_SELECTION次选择一个连续的索引范围
list2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
scrollPane2.setViewportView(list2);
label2 = new JLabel("连续选择列表");
label2.setHorizontalAlignment(SwingConstants.CENTER);
label2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
scrollPane2.setColumnHeaderView(label2);
JScrollPane scrollPane3 = new JScrollPane();
panel.add(scrollPane3);
list3 = new JList();
list3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
scrollPane3.setViewportView(list3);
label3 = new JLabel("默认选择列表");
label3.setHorizontalAlignment(SwingConstants.CENTER);
label3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
scrollPane3.setColumnHeaderView(label3);
}
protected void do_this_windowActivated(WindowEvent e) {
String[] listData = new String[12];
for(int i=0;i
}
list1.setListData(listData);
list2.setListData(listData);
list3.setListData(listData);
}
}
add没指明是加到哪个容器中。。。
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Frame1 extends JFrame {
JPanel contentPane;
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton(new ImageIcon("c:/add.png"));
JButton jButton2 = new JButton(new ImageIcon("c:/remove.png"));
JButton jButton3 = new JButton(new ImageIcon("c:/setting.png"));
JButton jButton4 = new JButton(new ImageIcon("c:/website.png"));
JButton jButton5 = new JButton(new ImageIcon("c:/exit.png"));
JButton jButton6 = new JButton(new ImageIcon("c:/start.png"));
JProgressBar jProgressBar1 = new JProgressBar();
JTextField jTextField1 = new JTextField();
String[] words= { "quick", "brown", "hungry", "wild"};
JList jList1 = new JList(words);
JScrollPane scrollPane = new JScrollPane(jList1);
public Frame1(){
add(scrollPane);
setVisible(true);
setBounds(100,100,400,400);
}
public static void main(String[]args){
new Frame1();
}
}
add()前面, 你全部是属性的定义
你应该把add()..即是方法的调用 放在某个方法里面的
比如在构造方法内