?? listexample.java
字號:
//ListExample.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListExample
{
public static void main(String[] args)
{
ListFrame frame = new ListFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ListFrame extends JFrame
{
public ListFrame()
{
setTitle("ListExample");
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane();
//建立容納文本區(qū)的面板
JPanel textPanel = new JPanel();
//新建文本區(qū)
myTextArea = new JTextArea(checkedLabel, 5, 20);
JScrollPane textScrollPane = new JScrollPane(myTextArea);
textPanel.add(textScrollPane);
contentPane.add(textPanel);
//建立容納列表的面板
listPanel = new JPanel();
//新建列表框
String[] courses = {"Math", "English", "Physics", "Chemic",
"Biology", "Politics"};
courseList = new JList(courses);
//設(shè)置列表中想顯示的行數(shù)
courseList.setVisibleRowCount(4);
//增加事件監(jiān)聽器
courseList.addListSelectionListener(new courseListener());
JScrollPane listScrollPane = new JScrollPane(courseList);
listPanel.add(listScrollPane);
contentPane.add(listPanel, BorderLayout.SOUTH);
}
//事件監(jiān)聽器
private class courseListener
implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent event)
{
Object[] selectedCourses = courseList.getSelectedValues();
int[] selectedIndexCourses = courseList.getSelectedIndices();
StringBuffer tempSeletedText = new StringBuffer(checkedLabel);
for (int i = 0; i < selectedCourses.length; i++)
{
String str1 = new String(selectedIndexCourses[i] + ", ");
String str2 = (String)selectedCourses[i];
tempSeletedText.append(str1);
tempSeletedText.append(str2);
tempSeletedText.append("\n");
}
myTextArea.setText(tempSeletedText.toString());
}
}
public static final int WIDTH = 300;
public static final int HEIGHT = 250;
public static final String checkedLabel="You choosed: \nIndex, Course\n";
private JTextArea myTextArea;
private JList courseList;
private JPanel listPanel;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -