📄 tutorial.java
字号:
hide();
return;
}
if (key == KeyEvent.VK_UP) {
if (src == _chocolate)
_strawberry.requestFocus();
else if (src == _vanilla)
_chocolate.requestFocus();
else if (src == _pistachio)
_vanilla.requestFocus();
else if (src == _lime) _pistachio.requestFocus();
e_.consume();
repaint();
} else if (key == KeyEvent.VK_DOWN) {
if (src == _strawberry)
_chocolate.requestFocus();
else if (src == _chocolate)
_vanilla.requestFocus();
else if (src == _vanilla)
_pistachio.requestFocus();
else if (src == _pistachio)
_lime.requestFocus();
else if (src == _lime) _nutTopping.requestFocus();
e_.consume();
repaint();
}
}
public void keyTyped(KeyEvent e_) {
}
public void keyReleased(KeyEvent e_) {
}
private JPanel makeNorthPanel() {
JPanel northpan = new JPanel();
northpan.setBorder(new TitledBorder("Select a flavor"));
northpan.setLayout(new BoxLayout(northpan, BoxLayout.Y_AXIS));
northpan.add(_strawberry);
northpan.add(_chocolate);
northpan.add(_vanilla);
northpan.add(_pistachio);
northpan.add(_lime);
_strawberry.addItemListener(this);
_strawberry.setActionCommand("Strawberry");
_chocolate.addItemListener(this);
_chocolate.setActionCommand("Chocolate");
_vanilla.addItemListener(this);
_vanilla.setActionCommand("Vanilla");
_pistachio.addItemListener(this);
_pistachio.setActionCommand("Pistachio");
_lime.addItemListener(this);
_lime.setActionCommand("Lime");
JPanel panel = new JPanel();
panel.add(new JLabel("Selected flavor: "));
panel.add(_selectedFlavor);
_selectedFlavor.setEnabled(false);
panel.setBorder(new EmptyBorder(1, 1, 1, 1));
northpan.add(panel);
_buttons.add(_strawberry);
_strawberry.setSelected(true); // select one button in the group
_buttons.add(_chocolate);
_buttons.add(_vanilla);
_buttons.add(_pistachio);
_buttons.add(_lime);
return northpan;
}
private JPanel makeCenterPanel() {
JPanel centerpan = new JPanel();
centerpan.setBorder(new TitledBorder("Select one or more toppings"));
centerpan.add(_nutTopping);
centerpan.add(_syrupTopping);
centerpan.add(_candyTopping);
centerpan.add(_waferTopping);
return centerpan;
}
}
/**
* This class demonstrates how to use the JTable component in a JScrollPane.
*/
class JTableTest extends JDialog implements ActionListener, ItemListener {
private JTable _table;
private JTextField _selectedColumns = new JTextField(10);
private JTextField _selectedRows = new JTextField(10);
private JCheckBox _checkBoxAllowRowSelection = new JCheckBox(
"Allow row selection");
private JCheckBox _checkBoxAllowColumnSelection = new JCheckBox(
"Allow column selection");
private JCheckBox _checkBoxAllowMultipleSelection = new JCheckBox(
"Allow multiple selection");
private JButton _okButton;
//private JScrollBar _scrollbar;
JTableTest(Frame owner_) {
super(owner_, "JTable in a JScrollPane");
_insets = new Insets(3, 3, 3, 3);
Container contentPane = getContentPane();
JPanel northpan = new JPanel();
northpan.setBorder(new EmptyBorder(1, 1, 1, 1));
northpan.add(new JLabel("Press ENTER to select/deselect columns/rows"));
contentPane.add(northpan, BorderLayout.NORTH);
contentPane.add(makeCenterPanel(), BorderLayout.CENTER);
contentPane.add(makeEastPanel(), BorderLayout.EAST);
_okButton = new JButton("OK");
_okButton.addActionListener(this);
contentPane.add(_okButton, BorderLayout.SOUTH);
pack();
}
public void actionPerformed(ActionEvent e_) {
hide();
}
public void itemStateChanged(ItemEvent e_) {
Object source = e_.getSource();
if (source == _checkBoxAllowRowSelection) {
boolean allowed = _checkBoxAllowRowSelection.isSelected();
_table.setRowSelectionAllowed(allowed);
} else if (source == _checkBoxAllowColumnSelection) {
boolean allowed = _checkBoxAllowColumnSelection.isSelected();
_table.setColumnSelectionAllowed(allowed);
} else if (source == _checkBoxAllowMultipleSelection) {
boolean allowed = _checkBoxAllowMultipleSelection.isSelected();
if (allowed)
_table
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
else
_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
}
private JPanel makeCenterPanel() {
JPanel centerpan = new JPanel();
centerpan.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 1));
String[] headings = { "Name", "Color", "Composition", "Mass", "Radius",
"Orbit"};
String[][] data = {
{ "Mars", "Red", "Dust", "1.5e10", "2.7e6", "Elliptical"},
{ "Pluto", "Blue", "Rock", "2.3e11", "2.9e7", "Circular"},
{ "Luna", "Green", "Cheese", "1.3e5", "2.3e12", "Square"},
{ "Venus", "White", "Gas", "4.3e5", "2.3e12",
"A funny irregular shape whose name is longer than the table width"},
{ "Jupiter", "Black", "Marshmallow", "4.3e6", "2.3e12",
"Zigzag"},
{ "Neptune", "Purple", "Gas", "1.2e6", "2.4e2", "Elliptical"},
{ "Saturn", "Yellow", "Gas", "1.1e7", "1.4e6", "Circular"}};
/*
* The following inner class overrides the processKeyEvent() method of
* JTable, so that we can display the selected rows and columns.
*/
_table = new JTable(data, headings) {
/*
* Gets called when the user presses a key in the JTable.
*/
public void processKeyEvent(KeyEvent e_) {
super.processKeyEvent(e_);
if (e_.getKeyCode() != KeyEvent.VK_ENTER) return;
int[] rows = getSelectedRows();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < rows.length; i++) {
buf.append(rows[ i]);
buf.append(' ');
}
_selectedRows.setText(buf.toString());
int[] columns = getSelectedColumns();
buf = new StringBuffer();
for (int i = 0; i < columns.length; i++) {
buf.append(columns[ i]);
buf.append(' ');
}
_selectedColumns.setText(buf.toString());
}
};
_table.setPreferredScrollableViewportSize(new Dimension(30, 5));
//_table.setValueAt("Yellow", 5, 2);
//_table.setValueAt("Red", 7, 4);
//_table.setValueAt("Magenta", 1, 5);
JScrollPane scrollPane = new JScrollPane(_table);
TitledBorder border = new TitledBorder(new LineBorder(Color.cyan));
border.setTitle("The Heavenly Bodies");
scrollPane.setViewportBorder(border);
// scrollPane.setSize(25, 6);
centerpan.add(scrollPane);
return centerpan;
}
private JPanel makeEastPanel() {
JPanel eastpan = new JPanel();
eastpan.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 2;
eastpan.add(_checkBoxAllowRowSelection, gbc);
_checkBoxAllowRowSelection.addItemListener(this);
_checkBoxAllowRowSelection.setSelected(true);
gbc.gridy = 1;
eastpan.add(_checkBoxAllowColumnSelection, gbc);
_checkBoxAllowColumnSelection.addItemListener(this);
_checkBoxAllowColumnSelection.setSelected(true);
gbc.gridy = 2;
eastpan.add(_checkBoxAllowMultipleSelection, gbc);
_checkBoxAllowMultipleSelection.addItemListener(this);
_checkBoxAllowMultipleSelection.setSelected(false);
gbc.gridy = 3;
gbc.gridwidth = 1;
eastpan.add(new JLabel(""), gbc);
gbc.anchor = GridBagConstraints.EAST;
gbc.gridy = 4;
eastpan.add(new JLabel("selected columns: "), gbc);
gbc.gridy = 5;
eastpan.add(new JLabel("selected rows: "), gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.WEST;
_selectedColumns.setEnabled(false);
eastpan.add(_selectedColumns, gbc);
gbc.gridy = 5;
_selectedRows.setEnabled(false);
eastpan.add(_selectedRows, gbc);
return eastpan;
}
}
/**
* This class demonstrates how to listen for KeyEvents generated by a
* component, and modify the component's default reaction to such KeyEvents.
*/
class KeyEventTest extends JDialog implements ActionListener, KeyListener {
private JCheckBox _checkBox1 = new JCheckBox("System ON");
private JCheckBox _checkBox2 = new JCheckBox("Alarm ON");
private JCheckBox _checkBox3 = new JCheckBox("System Armed");
private charva.awt.util.CapsTextField _capsField;
KeyEventTest(Frame owner_) {
super(owner_, "KeyEvent Test");
Container contentPane = getContentPane();
contentPane.add(makeNorthPanel(), BorderLayout.NORTH);
contentPane.add(makeCenterPanel(), BorderLayout.CENTER);
JPanel southpan = new JPanel();
JButton okButton = new JButton("OK");
okButton.addActionListener(this);
southpan.add(okButton);
contentPane.add(southpan, BorderLayout.SOUTH);
pack();
}
/**
* Implements the ActionListener interface
*/
public void actionPerformed(ActionEvent e_) {
String cmd = e_.getActionCommand();
if (cmd.equals("OK")) hide();
}
/**
* Implements the KeyListener interface
*/
public void keyPressed(KeyEvent e_) {
Component src = (Component) e_.getSource();
int key = e_.getKeyCode();
if (key == KeyEvent.VK_DOWN
&& (src == _checkBox1 || src == _checkBox2 || src == _checkBox3)) {
/*
* Move the keyboard input focus to the textfield below.
*/
_capsField.requestFocus();
/*
* "Consume" the keystroke so that it is not interpreted further by
* the JCheckBox which generated it.
*/
e_.consume();
/*
* Repaint the dialog-box to update the cursor position.
*/
repaint();
}
}
/**
* Implements the KeyListener interface
*/
public void keyTyped(KeyEvent e_) {
}
public void keyReleased(KeyEvent e_) {
}
private JPanel makeNorthPanel() {
JPanel northpan = new JPanel();
northpan.setBorder(new TitledBorder("A set of JCheckBoxes"));
northpan.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.insets = new Insets(1, 1, 1, 1);
JLabel label = new JLabel(
"Press CURSOR-DOWN to move to the text-field below");
northpan.add(label, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
northpan.add(_ch
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -