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 13:13] – [Παράδειγμα 2ο] 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 |
| - | Όταν ορίζετε ένα text area ορίζετε | + | Τόσο |
| - | <code java> | + | |
| - | JTextArea textArea = new JTextArea(NUMBER_OF_ROWS, | + | |
| - | </ | + | |
| - | Ο χρήστης | + | |
| - | Scroll bars ορίζονται από | + | Οι μέθοδοι του interface |
| - | + | ||
| - | {{ : | + | |
| - | + | ||
| - | Για να ορίσετε ένα text area μέσα σε ένα JScrollPane απαιτείται μία δήλωση της μορφής | + | |
| <code java> | <code java> | ||
| - | JScrollPane scolledTextArea = new JScrollPane(textArea); | + | //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); | ||
| </ | </ | ||
| - | Σε κάθε JScrollPane object | + | Μπορείτε να δείτε το παρακάτω παράδειγμα χρήσης |
| - | <code java> | + | |
| - | public void setHorizontalScrollBarPolicy(int policy) | + | |
| - | + | ||
| - | // επιτρεπόμενες τιμές | + | |
| - | JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS | + | |
| - | JScrollPane.HORIZONTAL_SCROLLBAR_NEVER | + | |
| - | JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED | + | |
| - | </ | + | |
| - | + | ||
| - | Αντίστοιχα για το κάθετο ScrollBar | + | |
| - | <code java> | + | |
| - | public void setVerticalScrollBarPolicy(int policy) | + | |
| - | + | ||
| - | // επιτρεπόμενες τιμές | + | |
| - | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS | + | |
| - | JScrollPane.VERTICAL_SCROLLBAR_NEVER | + | |
| - | JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED | + | |
| - | </ | + | |
| - | + | ||
| - | ===== Παραδείγματα ===== | + | |
| - | + | ||
| - | ==== Παράδειγμα 1ο ==== | + | |
| - | + | ||
| - | Παράδειγμα χρήσης | + | |
| - | + | ||
| - | <code java TextSamplerDemo.java> | + | |
| - | package components; | + | |
| + | <code java DocumentEventDemo.java> | ||
| import javax.swing.*; | import javax.swing.*; | ||
| import javax.swing.text.*; | import javax.swing.text.*; | ||
| + | import javax.swing.event.*; | ||
| - | import java.awt.*; //for layout managers and more | + | import java.awt.Dimension; |
| - | import java.awt.event.*; //for action events | + | import java.awt.BorderLayout; |
| + | import java.awt.GridBagLayout; | ||
| + | import java.awt.GridBagConstraints; | ||
| - | import java.net.URL; | + | import java.awt.event.*; |
| - | import java.io.IOException; | + | |
| - | public class TextSamplerDemo | + | public class DocumentEventDemo |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | protected JLabel actionLabel; | + | public DocumentEventDemo() { |
| + | super(new GridBagLayout()); | ||
| + | GridBagLayout gridbag = (GridBagLayout)getLayout(); | ||
| + | GridBagConstraints c = new GridBagConstraints(); | ||
| - | public TextSamplerDemo() { | + | JButton button = new JButton(" |
| - | | + | |
| - | //Create a regular text field. | + | |
| - | JTextField | + | textField.addActionListener(new MyTextActionListener()); |
| - | textField.setActionCommand(textFieldString); | + | textField.getDocument().addDocumentListener(new MyDocumentListener()); |
| - | textField.addActionListener(this); | + | textField.getDocument().putProperty(" |
| - | //Create a password field. | + | textArea |
| - | JPasswordField passwordField | + | |
| - | | + | textArea.getDocument().putProperty(" |
| - | | + | |
| - | //Create some labels for the fields. | + | JScrollPane scrollPane |
| - | JLabel textFieldLabel | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | //Create a label to put messages during an action event. | + | displayArea = new JTextArea(); |
| - | | + | displayArea.setEditable(false); |
| - | | + | |
| + | | ||
| - | //Lay out the text controls and the labels. | + | c.gridx = 0; |
| - | | + | |
| - | | + | |
| - | | + | c.fill = GridBagConstraints.HORIZONTAL; |
| + | gridbag.setConstraints(textField, | ||
| + | add(textField); | ||
| - | textControlsPane.setLayout(gridbag); | + | c.gridx = 0; |
| + | c.gridy = 1; | ||
| + | c.weightx = 0.0; | ||
| + | c.gridheight = 2; | ||
| + | c.fill = GridBagConstraints.BOTH; | ||
| + | | ||
| + | add(scrollPane); | ||
| - | JLabel[] labels | + | c.gridx |
| - | | + | |
| - | | + | |
| + | c.weighty = 1.0; | ||
| + | gridbag.setConstraints(displayScrollPane, c); | ||
| + | add(displayScrollPane); | ||
| - | | + | |
| - | c.anchor | + | c.gridy = 2; |
| - | c.weightx = 1.0; | + | c.weightx = 0.0; |
| - | | + | c.gridheight = 1; |
| - | | + | c.weighty = 0.0; |
| - | | + | |
| - | BorderFactory.createTitledBorder(" | + | |
| - | | + | add(button); |
| - | //Create a text area. | + | |
| - | JTextArea textArea = new JTextArea( | + | setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); |
| - | "This is an editable JTextArea. " + | + | } |
| - | "A text area is a \" | + | |
| - | "which means that although it can display text " + | + | |
| - | "in any font, all of the text is in the same font." | + | |
| - | ); | + | |
| - | textArea.setFont(new Font(" | + | |
| - | textArea.setLineWrap(true); | + | |
| - | textArea.setWrapStyleWord(true); | + | |
| - | JScrollPane areaScrollPane = new JScrollPane(textArea); | + | |
| - | areaScrollPane.setVerticalScrollBarPolicy( | + | |
| - | JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | + | |
| - | areaScrollPane.setPreferredSize(new Dimension(250, 250)); | + | |
| - | | + | |
| - | BorderFactory.createCompoundBorder( | + | |
| - | BorderFactory.createCompoundBorder( | + | |
| - | BorderFactory.createTitledBorder(" | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | public void insertUpdate(DocumentEvent e) { |
| - | BorderLayout.PAGE_START); | + | updateLog(e, " |
| - | | + | } |
| - | BorderLayout.CENTER); | + | public void removeUpdate(DocumentEvent e) { |
| + | | ||
| + | } | ||
| + | public void changedUpdate(DocumentEvent e) { | ||
| + | // | ||
| + | } | ||
| - | add(leftPane, BorderLayout.LINE_START); | + | public void updateLog(DocumentEvent e, String action) { |
| - | } | + | Document doc = (Document)e.getDocument(); |
| - | + | int changeLength = e.getLength(); | |
| - | private void addLabelTextRows(JLabel[] labels, | + | |
| - | | + | " |
| - | | + | |
| - | | + | |
| - | | + | " |
| - | c.anchor = GridBagConstraints.EAST; | + | |
| - | | + | } |
| + | | ||
| - | | + | |
| - | c.gridwidth = GridBagConstraints.RELATIVE; | + | /** Handle the text field Return. */ |
| - | | + | |
| - | | + | int selStart |
| - | container.add(labels[i], c); | + | int selEnd |
| - | c.gridwidth = GridBagConstraints.REMAINDER; | + | textArea.replaceRange(textField.getText(), |
| - | | + | |
| - | c.weightx = 1.0; | + | |
| - | container.add(textFields[i], | + | } |
| } | } | ||
| - | } | ||
| - | | + | /** Handle button click. */ |
| - | | + | |
| - | 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()) | + | |
| - | + " | + | |
| } | } | ||
| - | } | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | private static void createAndShowGUI() { | + | private static void createAndShowGUI() { |
| - | //Create and set up the window. | + | //Create and set up the window. |
| - | JFrame frame = new JFrame(" | + | JFrame frame = new JFrame(" |
| - | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| - | | + | |
| - | | + | |
| + | newContentPane.setOpaque(true); | ||
| + | frame.setContentPane(newContentPane); | ||
| - | | + | |
| - | frame.pack(); | + | frame.pack(); |
| - | frame.setVisible(true); | + | frame.setVisible(true); |
| - | } | + | } |
| - | | + | |
| - | //Schedule a job for the event dispatching thread: | + | //Schedule a job for the event-dispatching thread: |
| - | //creating and showing this application' | + | //creating and showing this application' |
| - | SwingUtilities.invokeLater(new Runnable() { | + | |
| - | public void run() { | + | public void run() { |
| - | //Turn off metal' | + | createAndShowGUI(); |
| - | UIManager.put(" | + | } |
| - | createAndShowGUI(); | + | }); |
| - | } | + | } |
| - | }); | + | |
| - | } | + | |
| } | } | ||
| </ | </ | ||
| - | |||
| - | ==== Παράδειγμα 2ο ==== | ||
| - | |||
| - | Το 2ο παράδειγμα είναι από το βιβλίο του Savitch. | ||
| - | <code java ScrollBarDemo.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(" | ||
| - | setSize(WIDTH, | ||
| - | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| - | JPanel buttonPanel = new JPanel(); | ||
| - | buttonPanel.setBackground(Color.LIGHT_GRAY); | ||
| - | buttonPanel.setLayout(new FlowLayout()); | ||
| - | JButton memo1Button = new JButton(" | ||
| - | memo1Button.addActionListener(this); | ||
| - | buttonPanel.add(memo1Button); | ||
| - | 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); | ||
| - | |||
| - | memoDisplay = new JTextArea(LINES, | ||
| - | 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, | ||
| - | } | ||
| - | | ||
| - | public void actionPerformed(ActionEvent e) { | ||
| - | String actionCommand = e.getActionCommand(); | ||
| - | if (actionCommand.equals(" | ||
| - | memo1 = memoDisplay.getText(); | ||
| - | else if (actionCommand.equals(" | ||
| - | 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 ===== | ||
| - | |||
| - | Κάθε component ή κάθε κατηγορία component συνδέεται με κάποιο Listener Interface. Τα παραπάνω text related components συνδέονται με το interface [[http:// | ||
| - | <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. | ||
| - | </ | ||
| - | |||
| + | | Προηγούμενο: | ||
swing/text_fields.1521810825.txt.gz · Last modified: 2018/03/23 13:13 (external edit)
