This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swing:text_fields [2015/05/10 17:24] gthanos [Text Fields, Text Areas & Scroll Panes (Scroll Bars)] |
swing:text_fields [2016/02/26 11:15] (current) |
||
---|---|---|---|
Line 140: | Line 140: | ||
</code> | </code> | ||
+ | ===== Παραδείγματα ===== | ||
+ | ==== Παράδειγμα 1ο ==== | ||
+ | |||
+ | Παράδειγμα χρήσης των παραπάνω components δίνεται παρακάτω (__απλουστευμένο__ από το [[http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html|site της Oracle]]). | ||
+ | |||
+ | <code java TextSamplerDemo.java> | ||
+ | package components; | ||
+ | |||
+ | import javax.swing.*; | ||
+ | import javax.swing.text.*; | ||
+ | |||
+ | import java.awt.*; //for layout managers and more | ||
+ | import java.awt.event.*; //for action events | ||
+ | |||
+ | import java.net.URL; | ||
+ | import java.io.IOException; | ||
+ | |||
+ | public class TextSamplerDemo extends JPanel | ||
+ | implements ActionListener { | ||
+ | private String newline = "\n"; | ||
+ | protected static final String textFieldString = "JTextField"; | ||
+ | protected static final String passwordFieldString = "JPasswordField"; | ||
+ | |||
+ | protected JLabel actionLabel; | ||
+ | |||
+ | public TextSamplerDemo() { | ||
+ | setLayout(new BorderLayout()); | ||
+ | |||
+ | //Create a regular text field. | ||
+ | JTextField textField = new JTextField(10); | ||
+ | textField.setActionCommand(textFieldString); | ||
+ | textField.addActionListener(this); | ||
+ | |||
+ | //Create a password field. | ||
+ | JPasswordField passwordField = new JPasswordField(10); | ||
+ | passwordField.setActionCommand(passwordFieldString); | ||
+ | passwordField.addActionListener(this); | ||
+ | |||
+ | //Create some labels for the fields. | ||
+ | JLabel textFieldLabel = new JLabel(textFieldString + ": "); | ||
+ | textFieldLabel.setLabelFor(textField); | ||
+ | JLabel passwordFieldLabel = new JLabel(passwordFieldString + ": "); | ||
+ | passwordFieldLabel.setLabelFor(passwordField); | ||
+ | |||
+ | //Create a label to put messages during an action event. | ||
+ | actionLabel = new JLabel("Type text in a field and press Enter."); | ||
+ | actionLabel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0)); | ||
+ | |||
+ | //Lay out the text controls and the labels. | ||
+ | JPanel textControlsPane = new JPanel(); | ||
+ | GridBagLayout gridbag = new GridBagLayout(); | ||
+ | GridBagConstraints c = new GridBagConstraints(); | ||
+ | |||
+ | textControlsPane.setLayout(gridbag); | ||
+ | |||
+ | JLabel[] labels = {textFieldLabel, passwordFieldLabel }; | ||
+ | JTextField[] textFields = {textField, passwordField }; | ||
+ | addLabelTextRows(labels, textFields, gridbag, textControlsPane); | ||
+ | |||
+ | c.gridwidth = GridBagConstraints.REMAINDER; //last | ||
+ | c.anchor = GridBagConstraints.WEST; | ||
+ | c.weightx = 1.0; | ||
+ | textControlsPane.add(actionLabel, c); | ||
+ | textControlsPane.setBorder( | ||
+ | BorderFactory.createCompoundBorder( | ||
+ | BorderFactory.createTitledBorder("Text Fields"), | ||
+ | BorderFactory.createEmptyBorder(5,5,5,5))); | ||
+ | |||
+ | //Create a text area. | ||
+ | JTextArea textArea = new JTextArea( | ||
+ | "This is an editable JTextArea. " + | ||
+ | "A text area is a \"plain\" text component, " + | ||
+ | "which means that although it can display text " + | ||
+ | "in any font, all of the text is in the same font." | ||
+ | ); | ||
+ | textArea.setFont(new Font("Serif", Font.ITALIC, 16)); | ||
+ | textArea.setLineWrap(true); | ||
+ | textArea.setWrapStyleWord(true); | ||
+ | JScrollPane areaScrollPane = new JScrollPane(textArea); | ||
+ | areaScrollPane.setVerticalScrollBarPolicy( | ||
+ | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | ||
+ | areaScrollPane.setPreferredSize(new Dimension(250, 250)); | ||
+ | areaScrollPane.setBorder( | ||
+ | BorderFactory.createCompoundBorder( | ||
+ | BorderFactory.createCompoundBorder( | ||
+ | BorderFactory.createTitledBorder("Plain Text"), | ||
+ | BorderFactory.createEmptyBorder(5,5,5,5)), | ||
+ | areaScrollPane.getBorder())); | ||
+ | |||
+ | //Put everything together. | ||
+ | JPanel leftPane = new JPanel(new BorderLayout()); | ||
+ | leftPane.add(textControlsPane, | ||
+ | BorderLayout.PAGE_START); | ||
+ | leftPane.add(areaScrollPane, | ||
+ | BorderLayout.CENTER); | ||
+ | |||
+ | add(leftPane, BorderLayout.LINE_START); | ||
+ | } | ||
+ | |||
+ | private void addLabelTextRows(JLabel[] labels, | ||
+ | JTextField[] textFields, | ||
+ | GridBagLayout gridbag, | ||
+ | Container container) { | ||
+ | GridBagConstraints c = new GridBagConstraints(); | ||
+ | c.anchor = GridBagConstraints.EAST; | ||
+ | int numLabels = labels.length; | ||
+ | |||
+ | for (int i = 0; i < numLabels; i++) { | ||
+ | c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last | ||
+ | c.fill = GridBagConstraints.NONE; //reset to default | ||
+ | c.weightx = 0.0; //reset to default | ||
+ | container.add(labels[i], c); | ||
+ | |||
+ | c.gridwidth = GridBagConstraints.REMAINDER; //end row | ||
+ | c.fill = GridBagConstraints.HORIZONTAL; | ||
+ | c.weightx = 1.0; | ||
+ | container.add(textFields[i], c); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void actionPerformed(ActionEvent e) { | ||
+ | String prefix = "You typed \""; | ||
+ | if (textFieldString.equals(e.getActionCommand())) { | ||
+ | JTextField source = (JTextField)e.getSource(); | ||
+ | actionLabel.setText(prefix + source.getText() + "\""); | ||
+ | } else if (passwordFieldString.equals(e.getActionCommand())) { | ||
+ | JPasswordField source = (JPasswordField)e.getSource(); | ||
+ | actionLabel.setText(prefix + new String(source.getPassword()) | ||
+ | + "\""); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Create the GUI and show it. For thread safety, | ||
+ | * this method should be invoked from the | ||
+ | * event dispatch thread. | ||
+ | */ | ||
+ | private static void createAndShowGUI() { | ||
+ | //Create and set up the window. | ||
+ | JFrame frame = new JFrame("TextSamplerDemo"); | ||
+ | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | |||
+ | //Add content to the window. | ||
+ | frame.add(new TextSamplerDemo()); | ||
+ | |||
+ | //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. | ||
+ | SwingUtilities.invokeLater(new Runnable() { | ||
+ | public void run() { | ||
+ | //Turn off metal's use of bold fonts | ||
+ | UIManager.put("swing.boldMetal", Boolean.FALSE); | ||
+ | createAndShowGUI(); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | ==== Παράδειγμα 2ο ==== | ||
+ | |||
+ | Το 2ο παράδειγμα είναι από το βιβλίο του Savitch. | ||
+ | <code java ScollBarDemo.java> | ||
+ | import javax.swing.*; | ||
+ | import java.awt.*; | ||
+ | import java.awt.event.*; | ||
+ | |||
+ | public class ScrollBarDemo extends JFrame implements ActionListener { | ||
+ | public static final int WIDTH = 600; | ||
+ | public static final int HEIGHT = 400; | ||
+ | public static final int LINES = 15; | ||
+ | public static final int CHAR_PER_LINE = 30; | ||
+ | private JTextArea memoDisplay; | ||
+ | private String memo1; | ||
+ | private String memo2; | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | ScrollBarDemo gui = new ScrollBarDemo(); | ||
+ | gui.setVisible(true); | ||
+ | } | ||
+ | | ||
+ | public ScrollBarDemo() { | ||
+ | super("Scroll Bars Demo"); | ||
+ | setSize(WIDTH, HEIGHT); | ||
+ | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | JPanel buttonPanel = new JPanel(); | ||
+ | buttonPanel.setBackground(Color.LIGHT_GRAY); | ||
+ | buttonPanel.setLayout(new FlowLayout()); | ||
+ | JButton memo1Button = new JButton("Save Memo 1"); | ||
+ | memo1Button.addActionListener(this); | ||
+ | buttonPanel.add(memo1Button); | ||
+ | JButton memo2Button = new JButton("Save Memo 2"); | ||
+ | memo2Button.addActionListener(this); | ||
+ | buttonPanel.add(memo2Button); | ||
+ | JButton clearButton = new JButton("Clear"); | ||
+ | clearButton.addActionListener(this); | ||
+ | buttonPanel.add(clearButton); | ||
+ | JButton get1Button = new JButton("Get Memo 1"); | ||
+ | get1Button.addActionListener(this); | ||
+ | buttonPanel.add(get1Button); | ||
+ | JButton get2Button = new JButton("Get Memo 2"); | ||
+ | get2Button.addActionListener(this); | ||
+ | buttonPanel.add(get2Button); | ||
+ | add(buttonPanel, BorderLayout.SOUTH); | ||
+ | JPanel textPanel = new JPanel(); | ||
+ | textPanel.setBackground(Color.BLUE); | ||
+ | |||
+ | memoDisplay = new JTextArea(LINES, CHAR_PER_LINE); | ||
+ | memoDisplay.setBackground(Color.WHITE); | ||
+ | JScrollPane scrolledText = new JScrollPane(memoDisplay); | ||
+ | scrolledText.setHorizontalScrollBarPolicy( | ||
+ | JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); | ||
+ | scrolledText.setVerticalScrollBarPolicy( | ||
+ | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | ||
+ | textPanel.add(scrolledText); | ||
+ | add(textPanel, BorderLayout.CENTER); | ||
+ | } | ||
+ | | ||
+ | public void actionPerformed(ActionEvent e) { | ||
+ | String actionCommand = e.getActionCommand(); | ||
+ | if (actionCommand.equals("Save Memo 1")) | ||
+ | memo1 = memoDisplay.getText(); | ||
+ | else if (actionCommand.equals("Save Memo 2")) | ||
+ | memo2 = memoDisplay.getText(); | ||
+ | else if (actionCommand.equals("Clear")) | ||
+ | memoDisplay.setText(""); | ||
+ | else if (actionCommand.equals("Get Memo 1")) | ||
+ | memoDisplay.setText(memo1); | ||
+ | else if (actionCommand.equals("Get Memo 2")) | ||
+ | memoDisplay.setText(memo2); | ||
+ | else | ||
+ | memoDisplay.setText("Error in memo interface"); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ===== Document Listeners ===== | ||
+ | Κάθε component ή κάθε κατηγορία component συνδέεται με κάποιο Listener Interface. Τα παραπάνω text related components συνδέονται με το interface [[http://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentListener.html|javax.swing.event.DocumentListener]]. Οι μέθοδοι του συγκεκριμένου interface δίνονται παρακάτω. | ||
+ | <code java> | ||
+ | void changedUpdate(DocumentEvent e); | ||
+ | //Gives notification that an attribute or set of attributes changed. | ||
+ | void insertUpdate(DocumentEvent e); | ||
+ | //Gives notification that there was an insert into the document. | ||
+ | void removeUpdate(DocumentEvent e); | ||
+ | //Gives notification that a portion of the document has been removed. | ||
+ | </code> | ||