📄 keywordspanel.java
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;import java.io.*;/** * TextInputDemo.java is a 1.4 application that uses * these additional files: * SpringUtilities.java * ... */public class KeyWordsPanel extends JPanel implements ActionListener, FocusListener{ private JTextField fileField, algorithmField; Font regularFont, italicFont; JTextArea resultDisplay; final static int GAP = 10; public KeyWordsPanel() { setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); JPanel leftHalf = new JPanel() { //Don't allow us to stretch vertically. public Dimension getMaximumSize() { Dimension pref = getPreferredSize(); return new Dimension(Integer.MAX_VALUE,pref.height); } }; leftHalf.setLayout(new BoxLayout(leftHalf, BoxLayout.PAGE_AXIS)); leftHalf.add(createEntryFields()); leftHalf.add(createButtons()); add(leftHalf); add(createOutputDiaplay()); } protected JComponent createButtons() { JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING)); JButton button1 = new JButton("Run Program"); button1.setActionCommand("run"); button1.addActionListener(this); panel.add(button1); JButton button2 = new JButton("Reset"); button2.addActionListener(this); button2.setActionCommand("clear"); panel.add(button2); //Match the SpringLayout's gap, subtracting 5 to make //up for the default gap FlowLayout provides. panel.setBorder(BorderFactory.createEmptyBorder(0, 0, GAP-5, GAP-5)); return panel; } /** * 监听者,当你按按钮时,它将根据你按哪个按钮,决定做什么动作 */ public void actionPerformed(ActionEvent e) { if ("clear".equals(e.getActionCommand())) { fileField.setText(""); algorithmField.setText(""); resultDisplay.setText(""); } else if ("run".equals(e.getActionCommand())) { String file = fileField.getText(); String algorithmChosen = algorithmField.getText(); try { KWIC kw= new KWIC(file, algorithmChosen, resultDisplay); } catch (IOException ex) { } } } protected JComponent createOutputDiaplay() { JPanel panel = new JPanel(new BorderLayout()); resultDisplay=new JTextArea(); regularFont = resultDisplay.getFont().deriveFont(Font.PLAIN, 16.0f); italicFont = regularFont.deriveFont(Font.ITALIC); //Lay out the panel. panel.setBorder(BorderFactory.createEmptyBorder( GAP/2, //top 0, //left GAP/2, //bottom 0)); //right panel.add(new JSeparator(JSeparator.VERTICAL), BorderLayout.LINE_START); panel.add(resultDisplay, BorderLayout.CENTER); panel.setPreferredSize(new Dimension(200, 150)); return panel; } /** * Called when one of the fields gets the focus so that * we can select the focused field. */ public void focusGained(FocusEvent e) { Component c = e.getComponent(); if (c instanceof JFormattedTextField) { selectItLater(c); } else if (c instanceof JTextField) { ((JTextField)c).selectAll(); } } //Workaround for formatted text field focus side effects. protected void selectItLater(Component c) { if (c instanceof JFormattedTextField) { final JFormattedTextField ftf = (JFormattedTextField)c; SwingUtilities.invokeLater(new Runnable() { public void run() { ftf.selectAll(); } }); } } //Needed for FocusListener interface. public void focusLost(FocusEvent e) { } //ignore protected JComponent createEntryFields() { JPanel panel = new JPanel(new SpringLayout()); String[] labelStrings = { "File:", "Filter Chosen: "}; JLabel[] labels = new JLabel[labelStrings.length]; JComponent[] fields = new JComponent[labelStrings.length]; int fieldNum = 0; //Create the text field and set it up. fileField = new JTextField(); fileField.setColumns(20); fields[fieldNum++] = fileField; algorithmField = new JTextField(); algorithmField.setColumns(20); fields[fieldNum++] = algorithmField; //Associate label/field pairs, add everything, and lay it out. for (int i = 0; i < labelStrings.length; i++) { labels[i] = new JLabel(labelStrings[i], JLabel.TRAILING); labels[i].setLabelFor(fields[i]); panel.add(labels[i]); panel.add(fields[i]); } SpringUtilities.makeCompactGrid(panel, labelStrings.length, 2, GAP, GAP, //init x,y GAP, GAP/2);//xpad, ypad return panel; } public JFormattedTextField getTextField(JSpinner spinner) { JComponent editor = spinner.getEditor(); if (editor instanceof JSpinner.DefaultEditor) { return ((JSpinner.DefaultEditor)editor).getTextField(); } else { System.err.println("Unexpected editor type: " + spinner.getEditor().getClass() + " isn't a descendant of DefaultEditor"); return null; } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("KWIC"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new KeyWordsPanel(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -