📄 tutorial.java
字号:
gbc.gridy = 4;
centerpan.add(new JLabel("Postal code: "), gbc);
gbc.gridy = 5;
centerpan.add(new JLabel("State: "), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
centerpan.add(lastnameField, gbc);
gbc.gridx = 2;
gbc.anchor = GridBagConstraints.EAST;
centerpan.add(new JLabel(" Initials: "), gbc);
gbc.gridx = 3;
gbc.anchor = GridBagConstraints.WEST;
centerpan.add(initialsField, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
centerpan.add(address1Field, gbc);
gbc.gridy = 2;
centerpan.add(address2Field, gbc);
gbc.gridy = 3;
centerpan.add(cityField, gbc);
gbc.gridy = 4;
centerpan.add(postcodeField, gbc);
gbc.gridy = 5;
centerpan.add(stateField, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.gridheight = 6;
gbc.insets = new Insets(1, 1, 1, 1);
String[] countries = { "Portugal", "Spain", "France", "Italy",
"Germany", "Poland", "Austria", "Belgium", "Denmark", "Norway",
"Sweden"};
_countryList = new JList(countries);
_countryList.setVisibleRowCount(6);
_countryList.setColumns(12);
_countryList.addListSelectionListener(this);
JScrollPane scrollpane = new JScrollPane(_countryList);
TitledBorder viewportBorder = new TitledBorder("Countries");
scrollpane.setViewportBorder(viewportBorder);
centerpan.add(scrollpane, gbc);
JPanel southpan = new JPanel();
JButton okButton = new JButton("OK");
okButton.addActionListener(this);
southpan.add(okButton);
contentPane.add(centerpan, BorderLayout.CENTER);
contentPane.add(southpan, BorderLayout.SOUTH);
pack();
}
public void actionPerformed(ActionEvent ae_) {
if (ae_.getActionCommand().equals("OK")) {
hide();
}
}
/**
* This method implements the ListSelectionListener interface, and is
* called when an item is selected or deselected in the JList.
*/
public void valueChanged(ListSelectionEvent e_) {
_countryList.repaint();
}
private JList _countryList;
}
/**
* This class demonstrates how to use the JTabbedPane.
*/
class JTabbedPaneTest extends JDialog implements ActionListener, KeyListener {
JTabbedPaneTest(Frame owner_) {
super(owner_, "JTabbedPane Test");
_insets = new Insets(2, 3, 2, 3);
Container contentPane = getContentPane();
JPanel toppan = new JPanel();
toppan.setBorder(new EmptyBorder(1, 1, 1, 1));
toppan.add(new JLabel(
"Press the F5, F6 and F7 keys to switch between panes"));
JPanel centerpan = new JPanel();
centerpan.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 1));
_tabpane = new JTabbedPane();
_tabpane.addTab("General", null, new GeneralPane(), "F5");
_tabpane.addTab("Device Manager", null, new DevicePane(), "F6");
_tabpane.addTab("Performance", null, new PerformancePane(), "F7");
addKeyListener(this);
centerpan.add(_tabpane);
_okButton = new JButton("OK");
_okButton.addActionListener(this);
contentPane.add(toppan, BorderLayout.NORTH);
contentPane.add(centerpan, BorderLayout.CENTER);
contentPane.add(_okButton, BorderLayout.SOUTH);
pack();
}
public void actionPerformed(ActionEvent e_) {
hide();
}
/**
* This method implements the KeyListener interface and handles the
* interactive selection of tabs.
*/
public void keyPressed(KeyEvent evt_) {
int key = evt_.getKeyCode();
if (key == KeyEvent.VK_F5)
_tabpane.setSelectedIndex(0);
else if (key == KeyEvent.VK_F6)
_tabpane.setSelectedIndex(1);
else if (key == KeyEvent.VK_F7) _tabpane.setSelectedIndex(2);
}
public void keyTyped(KeyEvent evt_) {
}
public void keyReleased(KeyEvent evt_) {
}
class GeneralPane extends JPanel {
public GeneralPane() {
setLayout(new BorderLayout());
JPanel northpan = new JPanel();
northpan.setLayout(new BoxLayout(northpan, BoxLayout.Y_AXIS));
northpan.setBorder(new TitledBorder("System"));
northpan.add(new JLabel("Red Hat Linux 9.0"));
JPanel centerpan = new JPanel();
centerpan.setLayout(new BoxLayout(centerpan, BoxLayout.Y_AXIS));
centerpan.setBorder(new TitledBorder("Registered to"));
centerpan.add(new JLabel("Rob Pitman"));
centerpan.add(new JLabel("8 Pickwood Road"));
centerpan.add(new JLabel("Centurion, South Africa"));
JPanel southpan = new JPanel();
southpan.setLayout(new BoxLayout(southpan, BoxLayout.Y_AXIS));
southpan.setBorder(new TitledBorder("Computer"));
southpan.add(new JLabel("GenuineIntel"));
southpan.add(new JLabel("x86 Family 15 Model 1 Stepping 2"));
southpan.add(new JLabel("256 MB RAM"));
add(northpan, BorderLayout.NORTH);
add(centerpan, BorderLayout.CENTER);
add(southpan, BorderLayout.SOUTH);
pack();
}
}
class DevicePane extends JPanel {
public DevicePane() {
setLayout(new BorderLayout());
JPanel northpan = new JPanel();
JRadioButton button1 = new JRadioButton("View devices by type");
JRadioButton button2 = new JRadioButton(
"View devices by connection");
ButtonGroup buttons = new ButtonGroup();
buttons.add(button1);
buttons.add(button2);
button1.setSelected(true);
northpan.add(button1);
northpan.add(button2);
JPanel centerpan = new JPanel();
String[] devices = { "Computer", "CD-ROM", "Disk drives",
"Display adapters", "Floppy disk controllers",
"Imaging devices", "Keyboard", "Modem", "Monitors", "Mouse"};
JList deviceList = new JList(devices);
deviceList.setBorder(new TitledBorder("Devices"));
centerpan.add(deviceList);
JPanel southpan = new JPanel();
southpan.add(new JButton("Properties"));
southpan.add(new JButton("Refresh"));
southpan.add(new JButton("Remove"));
southpan.add(new JButton("Print..."));
add(northpan, BorderLayout.NORTH);
add(centerpan, BorderLayout.CENTER);
add(southpan, BorderLayout.SOUTH);
pack();
}
}
class PerformancePane extends JPanel {
public PerformancePane() {
setLayout(new BorderLayout());
JPanel centerpan = new JPanel();
centerpan.setBorder(new TitledBorder("Performance Status"));
centerpan.setLayout(new BoxLayout(centerpan, BoxLayout.Y_AXIS));
centerpan.add(new JLabel("Memory: 256.0 MB of RAM"));
centerpan.add(new JLabel("System Resources: 50% free"));
centerpan.add(new JLabel("File System: 32 bit"));
centerpan.add(new JLabel("Virtual Memory: 32 bit"));
centerpan.add(new JLabel("Disk Compression: Not Installed"));
centerpan.add(new JLabel(
"Your system is configured for optimum performance"));
JPanel southpan = new JPanel();
southpan.setBorder(new TitledBorder("Advanced Settings"));
southpan.add(new JButton("File System..."));
southpan.add(new JButton("Graphics..."));
southpan.add(new JButton("Virtual Memory..."));
add(centerpan, BorderLayout.CENTER);
add(southpan, BorderLayout.SOUTH);
pack();
}
}
private JButton _okButton;
private JTabbedPane _tabpane;
}
/**
* This class demonstrates how to use the JTextField, the JPasswordField and
* the JTextArea.
*/
class TextWidgetTest extends JDialog implements ActionListener {
TextWidgetTest(Frame owner_) {
super(owner_, "Text Widget Test");
Container contentPane = getContentPane();
JPanel centerpan = new JPanel();
centerpan.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
centerpan.add(new TextFieldPanel(), gbc);
gbc.gridx = 1;
centerpan.add(new PasswordFieldPanel(), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
centerpan.add(new TextAreaPanel(), gbc);
JPanel southpan = new JPanel();
JButton okButton = new JButton("OK");
okButton.addActionListener(this);
southpan.add(okButton);
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 to display a JTextField.
*/
class TextFieldPanel extends JPanel implements ItemListener {
private JCheckBox _enabledCb;
private JCheckBox _visibleCb;
private JTextField _textfield;
TextFieldPanel() {
setLayout(new BorderLayout());
setBorder(new TitledBorder("JTextField"));
JPanel northpan = new JPanel();
_enabledCb = new JCheckBox("Enabled");
_enabledCb.setSelected(true);
_enabledCb.addItemListener(this);
northpan.add(_enabledCb);
_visibleCb = new JCheckBox("Visible");
_visibleCb.setSelected(true);
_visibleCb.addItemListener(this);
northpan.add(_visibleCb);
JPanel southpan = new JPanel();
_textfield = new JTextField("This is some text.....");
_textfield.setBorder(new EmptyBorder(1, 1, 1, 1));
southpan.add(_textfield);
add(northpan, BorderLayout.NORTH);
add(southpan, BorderLayout.SOUTH);
}
public void itemStateChanged(ItemEvent e_) {
if (e_.getSource() == _enabledCb) {
_textfield.setEnabled(_enabledCb.isSelected());
} else {
_textfield.setVisible(_visibleCb.isSelected());
}
}
}
/**
* An inner class to display a JPasswordField.
*/
class PasswordFieldPanel extends JPanel implements ItemListener {
private JCheckBox _enabledCb;
private JCheckBox _visibleCb;
private JPasswordField _textfield;
PasswordFieldPanel() {
setLayout(new BorderLayout());
setBorder(new TitledBorder("JPasswordField"));
JPanel northpan = new JPanel();
_enabledCb = new JCheckBox("Enabled");
_enabledCb.setSelected(true);
_enabledCb.addItemListener(this);
northpan.add(_enabledCb);
_visibleCb = new JCheckBox("Visible");
_visibleCb.setSelected(true);
_visibleCb.addItemListener(this);
northpan.add(_visibleCb);
JPanel southpan = new JPanel();
_textfield = new JPasswordField("This is some text.....");
_textfield.setBorder(new EmptyBorder(1, 1, 1, 1));
southpan.add(_textfield);
add(northpan, BorderLayout.NORTH);
add(southpan, BorderLayout.SOUTH);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -