swing:text_fields
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
swing:text_fields [2018/03/23 14:39] – [Παράδειγμα 1ο] gthanos | swing:text_fields [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Text Fields, Text Areas & Scroll Panes (Scroll Bars) ====== | + | ====== Text Fields |
<WRAP info 70% round center> | <WRAP info 70% round center> | ||
Line 106: | Line 106: | ||
</ | </ | ||
- | ===== Scroll Panes (Scroll Bars) ===== | + | ===== Η κλάση Document |
- | Όταν ορίζετε ένα | + | Τόσο |
+ | |||
+ | Οι μέθοδοι του interface [[http:// | ||
<code java> | <code java> | ||
- | JTextArea textArea = new JTextArea(NUMBER_OF_ROWS, | + | //Gives notification that an attribute or set of attributes changed. |
+ | void changedUpdate(DocumentEvent e); | ||
+ | //Gives notification that there was an insert into the document. | ||
+ | void insertUpdate(DocumentEvent e); | ||
+ | //Gives notification that a portion of the document has been removed. | ||
+ | void removeUpdate(DocumentEvent e); | ||
</ | </ | ||
- | Ο χρήστης μπορεί να εισάγει κείμενο που εκτείνεται έξω από τα παραπάνω όρια. Εάν δεν έχετε ορίσει την ιδιότητα της αναδίπλωσης γραμμών στο text area, το κείμενο που βρίσκεται εκτός των παραπάνω ορίων μπορεί να φανεί μόνο αν ορίσετε | ||
- | Scroll bars ορίζονται από το component | + | Μπορείτε να δείτε το παρακάτω παράδειγμα χρήσης |
- | {{ :swing: | + | <code java DocumentEventDemo.java> |
+ | import javax.swing.*; | ||
+ | import javax.swing.text.*; | ||
+ | import javax.swing.event.*; | ||
- | Για να ορίσετε ένα text area μέσα σε ένα JScrollPane απαιτείται μία δήλωση της μορφής | + | import java.awt.Dimension; |
- | < | + | import |
- | JScrollPane scolledTextArea = new JScrollPane(textArea); | + | import java.awt.GridBagLayout; |
- | </ | + | import java.awt.GridBagConstraints; |
- | Σε κάθε JScrollPane object ορίζεται την συμπεριφορά του οριζόντιου ScrollBar από την παρακάτω μέθοδο οι οποία λαμβάνει ως ορίσματα τις τιμές που ακολουθούν. | + | import |
- | < | + | |
- | public void setHorizontalScrollBarPolicy(int policy) | + | |
- | // επιτρεπόμενες τιμές | + | public class DocumentEventDemo extends JPanel |
- | JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS | + | implements ActionListener { |
- | JScrollPane.HORIZONTAL_SCROLLBAR_NEVER | + | |
- | JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED | + | |
- | </ | + | |
- | Αντίστοιχα για το κάθετο ScrollBar | + | public DocumentEventDemo() { |
- | <code java> | + | |
- | public void setVerticalScrollBarPolicy(int policy) | + | |
+ | GridBagConstraints c = new GridBagConstraints(); | ||
- | // επιτρεπόμενες τιμές | + | JButton button = new JButton(" |
- | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS | + | |
- | JScrollPane.VERTICAL_SCROLLBAR_NEVER | + | |
- | JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED | + | |
- | </ | + | |
- | ===== Παραδείγματα ===== | + | textField |
+ | textField.addActionListener(new MyTextActionListener()); | ||
+ | textField.getDocument().addDocumentListener(new MyDocumentListener()); | ||
+ | textField.getDocument().putProperty(" | ||
- | ==== Παράδειγμα 1ο ==== | + | textArea |
+ | textArea.getDocument().addDocumentListener(new MyDocumentListener()); | ||
+ | textArea.getDocument().putProperty(" | ||
+ | textArea.setLineWrap(true); | ||
- | Ένα παράδειγμα χρήστης των παραπάνω components δίνεται παρακάτω: | + | JScrollPane scrollPane = new JScrollPane(textArea); |
- | <code java TextComponentsDemo.java> | + | |
- | import java.awt.*; | + | |
- | import java.awt.event.*; | + | |
- | import javax.swing.*; | + | |
- | public class TextComponentsDemo { | + | displayArea = new JTextArea(); |
+ | displayArea.setEditable(false); | ||
+ | JScrollPane displayScrollPane = new JScrollPane(displayArea); | ||
+ | displayScrollPane.setPreferredSize(new Dimension(200, | ||
- | JFrame frame; | + | c.gridx = 0; |
- | | + | |
- | JPasswordField passwdField; | + | c.weightx = 1.0; |
- | JLabel msgLabel; | + | |
- | JTextArea textArea; | + | |
- | JCheckBox textFieldCheck, | + | add(textField); |
- | + | ||
- | public TextComponentsDemo() { | + | c.gridx |
- | frame = new JFrame(" | + | c.gridy = 1; |
- | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | |
- | | + | c.gridheight = 2; |
- | | + | |
- | | + | |
- | | + | add(scrollPane); |
- | | + | |
- | | + | c.gridx = 1; |
- | | + | |
- | public void actionPerformed(ActionEvent e) { | + | c.weightx = 1.0; |
- | | + | |
- | | + | |
- | } | + | add(displayScrollPane); |
- | }); | + | |
- | textFieldPanel.add(new JLabel(" | + | c.gridx = 1; |
- | | + | |
- | | + | c.weightx = 0.0; |
- | | + | |
- | public void itemStateChanged(ItemEvent e) { | + | c.weighty = 0.0; |
- | | + | |
- | textField.setEditable(true); | + | |
- | | + | add(button); |
- | textField.setEditable(false); | + | |
- | } | + | |
- | }); | + | |
- | textFieldPanel.add(textFieldCheck); | + | |
- | + | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | } |
- | public void itemStateChanged(ItemEvent e) { | + | |
- | | + | |
- | passwdField.setEditable(true); | + | } |
- | | + | public void changedUpdate(DocumentEvent |
- | passwdField.setEditable(false); | + | |
- | } | + | |
- | }); | + | |
- | textFieldPanel.add(passwdFieldCheck); | + | |
- | + | ||
- | | + | |
- | | + | |
- | + | ||
- | String []font = {" | + | |
- | | + | |
- | | + | |
- | public void actionPerformed(ActionEvent | + | |
- | | + | |
- | | + | |
- | | + | |
- | }); | + | |
- | + | ||
- | + | ||
- | String []fontColor = { " | + | |
- | JComboBox< | + | |
- | | + | |
- | public void actionPerformed(ActionEvent | + | |
- | Color c; | + | |
- | switch(fontColorCombo.getSelectedIndex()) { | + | |
- | case 0: | + | |
- | | + | |
- | break; | + | |
- | case 1: | + | |
- | c = Color.BLUE; | + | |
- | break; | + | |
- | case 2: | + | |
- | c = Color.CYAN; | + | |
- | break; | + | |
- | case 3: | + | |
- | c = Color.DARK_GRAY; | + | |
- | break; | + | |
- | default: | + | |
- | c = Color.YELLOW; | + | |
} | } | ||
- | textArea.setForeground(c); | ||
- | } | ||
- | }); | ||
- | textFieldPanel.add(fontCombo); | ||
- | textFieldPanel.add(fontColorCombo); | ||
- | | ||
- | textArea = new JTextArea(10, | ||
- | // | ||
- | textArea.setFont(new Font(" | ||
- | textArea.setForeground(new Color(250, 30, 30)); | ||
- | framePanel.add(textArea); | ||
- | | ||
- | frame.pack(); | ||
- | frame.setVisible(true); | ||
- | } | ||
- | | ||
- | public static void main(String []args) { | ||
- | javax.swing.SwingUtilities.invokeLater( new Runnable() { | ||
- | public void run() { | ||
- | new TextComponentsDemo(); | ||
- | } | ||
- | }); | ||
- | } | ||
- | } | ||
- | </ | ||
- | <WRAP tip 80% center round> | + | public void updateLog(DocumentEvent e, String action) { |
- | Παρατηρήστε ότι μπορείτε να χρησιμοποιήσετε έναν [[https:// | + | |
- | </ | + | int changeLength = e.getLength(); |
- | + | displayArea.append( | |
- | ==== Παράδειγμα 2ο ==== | + | "["+doc.getProperty(" |
+ | action + changeLength + " character" | ||
+ | ((changeLength == 1) ? " | ||
+ | " | ||
+ | displayArea.setCaretPosition(displayArea.getDocument().getLength()); | ||
+ | } | ||
+ | } | ||
- | Το 2ο παράδειγμα είναι από το βιβλίο του Savitch. | + | class MyTextActionListener implements ActionListener { |
- | <code java ScrollBarDemo.java> | + | /** Handle the text field Return. */ |
- | import javax.swing.*; | + | |
- | import java.awt.*; | + | int selStart = textArea.getSelectionStart(); |
- | import java.awt.event.*; | + | int selEnd = textArea.getSelectionEnd(); |
- | public class ScrollBarDemo extends JFrame implements ActionListener { | + | textArea.replaceRange(textField.getText(), |
- | | + | |
- | | + | |
- | | + | } |
- | | + | } |
- | private JTextArea memoDisplay; | + | |
- | private String memo1; | + | |
- | private String memo2; | + | |
- | public static void main(String[] args) { | + | /** Handle button click. */ |
- | | + | public |
- | gui.setVisible(true); | + | |
- | } | + | |
- | + | | |
- | | + | |
- | | + | |
- | setSize(WIDTH, | + | |
- | 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"); | + | |
- | | + | |
- | | + | |
- | JButton memo2Button = new JButton(" | + | |
- | memo2Button.addActionListener(this); | + | |
- | buttonPanel.add(memo2Button); | + | |
- | JButton clearButton = new JButton(" | + | |
- | clearButton.addActionListener(this); | + | |
- | buttonPanel.add(clearButton); | + | |
- | JButton get1Button = new JButton(" | + | |
- | get1Button.addActionListener(this); | + | |
- | buttonPanel.add(get1Button); | + | |
- | JButton get2Button = new JButton(" | + | |
- | get2Button.addActionListener(this); | + | |
- | buttonPanel.add(get2Button); | + | |
- | add(buttonPanel, | + | |
- | JPanel textPanel = new JPanel(); | + | |
- | textPanel.setBackground(Color.BLUE); | + | |
- | | + | |
- | | + | * Create the GUI and show it. For thread safety, |
- | | + | * this method should be invoked from the |
- | | + | * event-dispatching thread. |
- | | + | */ |
- | | + | |
- | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | + | // |
- | textPanel.add(scrolledText); | + | |
- | add(textPanel, | + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
- | } | + | |
- | + | ||
- | public | + | |
- | | + | |
- | if (actionCommand.equals(" | + | |
- | | + | |
- | else if (actionCommand.equals("Save Memo 2")) | + | |
- | memo2 = memoDisplay.getText(); | + | |
- | else if (actionCommand.equals(" | + | |
- | memoDisplay.setText("" | + | |
- | else if (actionCommand.equals(" | + | |
- | memoDisplay.setText(memo1); | + | |
- | else if (actionCommand.equals(" | + | |
- | memoDisplay.setText(memo2); | + | |
- | else | + | |
- | memoDisplay.setText(" | + | |
- | } | + | |
- | + | ||
- | } | + | |
- | </ | + | |
- | ===== Document Listeners ===== | + | //Create and set up the content pane. |
+ | JComponent newContentPane | ||
+ | newContentPane.setOpaque(true); | ||
+ | frame.setContentPane(newContentPane); | ||
- | Κάθε component ή κάθε κατηγορία component συνδέεται με κάποιο Listener Interface. Τα παραπάνω text related components συνδέονται με το interface [[http://docs.oracle.com/ | + | |
- | <code java> | + | frame.pack(); |
- | void changedUpdate(DocumentEvent e); | + | frame.setVisible(true); |
- | //Gives notification that an attribute or set of attributes changed. | + | } |
- | void insertUpdate(DocumentEvent e); | + | public static |
- | //Gives notification that there was an insert into the document. | + | //Schedule a job for the event-dispatching thread: |
+ | //creating and showing this application' | ||
+ | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
+ | public void run() { | ||
+ | createAndShowGUI(); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | } | ||
- | void removeUpdate(DocumentEvent e); | ||
- | //Gives notification that a portion of the document has been removed. | ||
</ | </ | ||
+ | | Προηγούμενο: | ||
swing/text_fields.1521815994.txt.gz · Last modified: 2018/03/23 14:39 by gthanos