📄 tutorial.java
字号:
}
public void itemStateChanged(ItemEvent e_) {
if (e_.getSource() == _enabledCb) {
_textfield.setEnabled(_enabledCb.isSelected());
} else {
_textfield.setVisible(_visibleCb.isSelected());
}
}
}
/**
* An inner class to display a JTextArea.
*/
class TextAreaPanel extends JPanel implements ItemListener {
private JCheckBox _linewrap;
private JCheckBox _linewrapstyle;
private JTextArea _textarea;
TextAreaPanel() {
setLayout(new BorderLayout());
setBorder(new TitledBorder("JTextArea in a JScrollPane"));
JPanel northpan = new JPanel();
_linewrap = new JCheckBox("Line Wrap ");
_linewrap.setSelected(false);
_linewrap.addItemListener(this);
northpan.add(_linewrap);
_linewrapstyle = new JCheckBox("Line Wrap Style = Word");
_linewrapstyle.setSelected(false);
_linewrapstyle.setEnabled(false);
_linewrapstyle.addItemListener(this);
northpan.add(_linewrapstyle);
_textarea = new JTextArea("Contents of the JTextArea...", 8, 50);
JScrollPane scrollpane = new JScrollPane(_textarea);
scrollpane.setViewportBorder(new TitledBorder("Text Area"));
add(northpan, BorderLayout.NORTH);
add(scrollpane, BorderLayout.SOUTH);
}
public void itemStateChanged(ItemEvent e_) {
Component source = (Component) e_.getSource();
if (source == _linewrap) {
_textarea.setLineWrap(_linewrap.isSelected());
if (_textarea.getLineWrap() == false)
_linewrapstyle.setSelected(false);
_linewrapstyle.setEnabled(_textarea.getLineWrap());
} else {
_textarea.setWrapStyleWord(_linewrapstyle.isSelected());
}
}
}
}
/**
* This class demonstrates how to use the JComboBox and the JList components.
*/
class SelectionTest extends JDialog implements ActionListener {
SelectionTest(Frame owner_) {
super(owner_, "JComboBox and JList");
Container contentPane = getContentPane();
JPanel northpan = new ComboBoxPanel();
JPanel centerpan = new JListPanel();
JPanel southpan = new JPanel();
JButton okButton = new JButton("OK");
okButton.addActionListener(this);
southpan.add(okButton);
contentPane.add(northpan, BorderLayout.NORTH);
contentPane.add(centerpan, BorderLayout.CENTER);
contentPane.add(southpan, BorderLayout.SOUTH);
pack();
}
public void actionPerformed(ActionEvent e_) {
if (e_.getActionCommand().equals("OK")) {
hide();
}
}
/**
* An inner class that displays a JComboBox.
*/
class ComboBoxPanel extends JPanel implements ItemListener {
private JComboBox _comboBox;
private JTextField _comboBoxSelection;
ComboBoxPanel() {
setLayout(new BorderLayout());
setBorder(new TitledBorder("JComboBox"));
add(new JLabel("Press ENTER to pop up the JComboBox"),
BorderLayout.NORTH);
String[] colors = { "Red", "Blue", "Green", "Magenta", "Mauve",
"Orange", "Black", "White", "Brown"};
_comboBox = new JComboBox(colors);
_comboBox.setMaximumRowCount(5);
_comboBox.addItemListener(this);
add(_comboBox, BorderLayout.CENTER);
JPanel southpan = new JPanel();
southpan.add(new JLabel(" Selected item is: "));
_comboBoxSelection = new JTextField(15);
_comboBoxSelection.setEnabled(false);
southpan.add(_comboBoxSelection);
add(southpan, BorderLayout.SOUTH);
}
public void itemStateChanged(ItemEvent e_) {
_comboBoxSelection.setText((String) _comboBox.getSelectedItem());
}
}
/**
* An inner class that displays a JList.
*/
class JListPanel extends JPanel implements ListSelectionListener,
ListDataListener, ItemListener, ActionListener {
private JCheckBox _selectionMode;
private JList _vehicleList;
private JTextField _listSelection;
private JButton _deleteButton;
JListPanel() {
setBorder(new TitledBorder("JList"));
setLayout(new BorderLayout());
add(new JLabel("Use UP, DOWN, PGUP, PGDN, HOME & END to navigate"),
BorderLayout.NORTH);
String[] vehicles = { "Volkswagen", "Rolls-Royce", "Toyota",
"Chrysler", "Mercedes Benz", "Bentley", "Bugatti",
"Maserati", "Porsche"};
DefaultListModel model = new DefaultListModel();
int columns = 0;
for (int i = 0; i < vehicles.length; i++) {
model.addElement(vehicles[ i]);
if (vehicles[ i].length() > columns)
columns = vehicles[ i].length();
}
model.addListDataListener(this);
_vehicleList = new JList(model);
_vehicleList.setVisibleRowCount(5);
_vehicleList.setColumns(columns);
_vehicleList.addListSelectionListener(this);
JScrollPane scrollpane = new JScrollPane(_vehicleList);
scrollpane.setViewportBorder(new TitledBorder("Vehicles"));
add(scrollpane, BorderLayout.WEST);
_selectionMode = new JCheckBox("Selection Mode = Multiple");
_selectionMode.addItemListener(this);
_deleteButton = new JButton("Delete selected item(s)");
_deleteButton.setActionCommand("Delete");
_deleteButton.addActionListener(this);
JPanel eastpan = new JPanel();
eastpan.setLayout(new BoxLayout(eastpan, BoxLayout.Y_AXIS));
eastpan.add(new JLabel(""));
eastpan.add(_selectionMode);
eastpan.add(new JLabel(""));
eastpan.add(_deleteButton);
add(eastpan, BorderLayout.EAST);
JPanel southpan = new JPanel();
southpan.add(new JLabel("Selected item(s):"));
_listSelection = new JTextField(30);
_listSelection.setEnabled(false);
southpan.add(_listSelection);
add(southpan, BorderLayout.SOUTH);
pack();
}
/**
* This method implements the ListSelectionListener interface, and is
* called when an item is selected or deselected in the JList.
*/
public void valueChanged(ListSelectionEvent e_) {
Object[] items = _vehicleList.getSelectedValues();
String s = "";
for (int i = 0; i < items.length; i++) {
if (i != 0) s += ",";
s += (String) items[ i];
}
_listSelection.setText(s);
}
public void actionPerformed(ActionEvent e_) {
String cmd = e_.getActionCommand();
if (cmd.equals("Delete")) {
int[] indices = _vehicleList.getSelectedIndices();
if (indices.length == 0) return; // there is no selected item
DefaultListModel model = (DefaultListModel) _vehicleList
.getModel();
// We must remove the last elements first, otherwise
// (if we remove an element with a low index), the
// higher indices will be invalid.
for (int i = indices.length - 1; i >= 0; i--) {
model.removeElementAt(indices[ i]);
}
// Having deleted some elements from the list, we must
// ensure that:
// (a) the first index inside the visible area is >= 0
// (b) the "current row" is inside the visible area.
// What constitutes the "current row" after a deletion is
// debatable; we will assume that the last index to be
// deleted is a close approximation.
_vehicleList.ensureIndexIsVisible(indices[ 0]);
}
}
/**
* This method implements the ListDataListener interface, and is called
* when an item is added to or removed from the list, or the value of
* an item in the list changes.
*/
public void contentsChanged(ListDataEvent e_) {
_vehicleList
.removeSelectionInterval(e_.getIndex0(), e_.getIndex1());
_vehicleList.repaint();
}
/**
* These methods are defined for compatibilty with Swing, but are not
* used in CHARVA.
*/
public void intervalAdded(ListDataEvent e_) {
}
public void intervalRemoved(ListDataEvent e_) {
}
/**
* This method implements the ItemListener interface, and is called
* when the SelectionMode checkbox is changed.
*/
public void itemStateChanged(ItemEvent e_) {
if (e_.getSource() == _selectionMode) {
if (_selectionMode.isSelected()) {
_vehicleList
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
} else {
_vehicleList
.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
}
}
}
}
/**
* This class demonstrates how to use the various types of Buttons.
*/
class ButtonTest extends JDialog implements ActionListener, KeyListener,
ItemListener {
private ButtonGroup _buttons = new ButtonGroup();
private JRadioButton _strawberry = new JRadioButton("Strawberry");
private JRadioButton _chocolate = new JRadioButton("Chocolate");
private JRadioButton _vanilla = new JRadioButton("Vanilla");
private JRadioButton _pistachio = new JRadioButton("Pistachio");
private JRadioButton _lime = new JRadioButton("Lime");
private JTextField _selectedFlavor = new JTextField(15);
private JCheckBox _nutTopping = new JCheckBox("Nuts ");
private JCheckBox _syrupTopping = new JCheckBox("Syrup ");
private JCheckBox _candyTopping = new JCheckBox("Candy ");
private JCheckBox _waferTopping = new JCheckBox("Wafer ");
ButtonTest(Frame owner_) {
super(owner_, "Button Test");
Container contentPane = getContentPane();
contentPane.add(makeNorthPanel(), BorderLayout.NORTH);
contentPane.add(makeCenterPanel(), BorderLayout.CENTER);
JPanel southpan = new JPanel();
JButton okButton = new JButton("OK (F9)");
okButton.setActionCommand("OK");
okButton.addActionListener(this);
southpan.add(okButton);
contentPane.add(southpan, BorderLayout.SOUTH);
/*
* Add a KeyListener for this entire window; any key pressed on any
* component in this window will cause keyPressed (or keyTyped) to be
* called.
*/
addKeyListener(this);
pack();
}
/**
* Implements the ActionListener interface.
*/
public void actionPerformed(ActionEvent e_) {
String cmd = e_.getActionCommand();
if (cmd.equals("OK")) {
hide();
}
}
/**
* Implements ItemListener interface
*/
public void itemStateChanged(ItemEvent e_) {
int statechange = e_.getStateChange();
Component source = (Component) e_.getSource();
if (statechange == ItemEvent.SELECTED) {
JRadioButton button = (JRadioButton) source;
_selectedFlavor.setText(button.getText());
}
}
public void keyPressed(KeyEvent e_) {
int key = e_.getKeyCode();
Object src = e_.getSource();
if (key == KeyEvent.VK_F9) {
/*
* Consume the event so it doesn't get processed further by the
* component that generated it.
*/
e_.consume();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -