Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JComboBox;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JPanel;
  12. import javax.swing.JTextField;
  13.  
  14. public class MyApp extends JFrame {
  15. private JLabel lab;
  16. private JTextField tf;
  17. private JButton but;
  18. private JComboBox<String> combo;
  19. private JButton but2;
  20.  
  21. public MyApp() {
  22. setSize(800, 600);
  23. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. setTitle("Mon application");
  25.  
  26. JPanel panel = new JPanel(new BorderLayout());
  27. setContentPane(panel);
  28.  
  29. JPanel top = new JPanel(new GridLayout(1,5));
  30. panel.add(top,BorderLayout.NORTH);
  31.  
  32. lab = new JLabel("Input something:");
  33. tf = new JTextField();
  34. but = new JButton("OK");
  35. but2 = new JButton("ZZZZ");
  36.  
  37. combo = new JComboBox<>(new String[]{"Blue","Green","Yellow"});
  38. top.add(lab);
  39. top.add(tf);
  40. top.add(but);
  41. top.add(but2);
  42. top.add(combo);
  43.  
  44. but.addActionListener(this::doOK);
  45. but2.addActionListener(this::showOpts);
  46. combo.addActionListener(this::comboChanged);
  47.  
  48. }
  49.  
  50. public void showOpts(ActionEvent e) {
  51. String fav = (String) JOptionPane.showInputDialog(this,
  52. "What is your favorite pet?",
  53. "Favorite pet",
  54. JOptionPane.QUESTION_MESSAGE,
  55. null,
  56. new String[]{"Dog","Cat","Fish"},
  57. "Dog");
  58.  
  59. }
  60. public void comboChanged(ActionEvent e) {
  61. tf.setText((String)combo.getSelectedItem());
  62.  
  63. }
  64. public void doOK(ActionEvent e) {
  65. String txt = JOptionPane.showInputDialog("Entrez du texte");
  66. lab.setText(txt);
  67. }
  68.  
  69.  
  70. public static void main(String[] args) {
  71. MyApp f =new MyApp();
  72. f.setVisible(true);
  73. }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement