SHARE
TWEET

Untitled

a guest Oct 18th, 2017 37 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.lang.model.element.Element;
  4. import javax.swing.JButton;
  5. import javax.swing.JComboBox;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;
  9. import javax.swing.JTextArea;
  10. import javax.swing.JTextField;
  11. import javax.swing.event.DocumentEvent;
  12. import javax.swing.event.DocumentListener;
  13. import javax.swing.text.Caret;
  14. import javax.swing.text.Document;
  15.  
  16. class CaesarFrame{
  17. JFrame frame;
  18. JTextField textField1, textField2;
  19. JButton button;
  20. JPanel panel1, panel2;
  21. JComboBox<Object> comboBox1;
  22. Object[] ob = {'A','B','C','D','E','F','G','H','I','J','K','L'
  23.         ,'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  24. Container contentPane;
  25. JLabel label;
  26.  
  27. // Constructor
  28. CaesarFrame(){  
  29.     frame = new JFrame("SwingLab");
  30.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.     frame.setSize(400, 110);
  32.     frame.setLayout(new BorderLayout());
  33.     frame.setLocationRelativeTo(null);
  34.  
  35.     contentPane = frame.getContentPane();
  36.     textField1 = new JTextField(20);
  37.     textField2 = new JTextField(28);
  38.     textField2.setEditable(false);
  39.     button = new JButton("Code!");
  40.     comboBox1 = new JComboBox<Object>(ob);
  41.     label = new JLabel("Output:");
  42.     panel1 = new JPanel();
  43.     panel2 = new JPanel();
  44.  
  45.     panel1.add(comboBox1);
  46.     panel1.add(textField1);
  47.     panel1.add(button);
  48.     panel2.add(label);
  49.     panel2.add(textField2);
  50.  
  51.     contentPane.add(panel1, BorderLayout.NORTH);
  52.     contentPane.add(panel2,BorderLayout.WEST);
  53.  
  54.     frame.setVisible(true);
  55.  
  56.     ActionListener buttonCode = new OkButtonActionListener();
  57.     button.addActionListener(buttonCode);
  58.  
  59.     KeyAdapter key = new InputFieldKeyListener();
  60.     textField1.addKeyListener(key);
  61.     //この箇所がよくわかりません。
  62.     KeyAdapter key2 = new InputFieldKeyListener();
  63.     Document doc = ((DocumentEvent) key2).getDocument();
  64.     doc.addDocumentListener((DocumentListener) key);
  65.  
  66. }
  67.  
  68. class InputFieldKeyListener extends KeyAdapter implements DocumentListener{
  69.     public void keyTyped(KeyEvent e) {
  70.         char temp = e.getKeyChar();
  71.         String ch =String.valueOf(temp);
  72.         char offset = (char) comboBox1.getSelectedItem();
  73.         textField2.setText(Caesar.caesarCode(ch,offset));
  74.     }
  75.  
  76.     @Override
  77.     public void changedUpdate(DocumentEvent e) {
  78.         System.out.println("Changed");
  79.         }
  80.  
  81.     @Override
  82.     public void insertUpdate(DocumentEvent e) {
  83.         // TODO Auto-generated method stub
  84.         System.out.println("Inserted");
  85.         }
  86.  
  87.     @Override
  88.     public void removeUpdate(DocumentEvent e) {
  89.         // TODO Auto-generated method stub
  90.         System.out.println("Removed");
  91.         }
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98. private class OkButtonActionListener implements ActionListener{
  99.     @Override
  100.     public void actionPerformed(ActionEvent e) {
  101.         String message = textField1.getText();
  102.         char offset = (char) comboBox1.getSelectedItem();
  103.         textField2.setText(Caesar.caesarCode(message,offset));
  104.     }
  105. }
RAW Paste Data
Top