Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class MyApp extends JFrame {
- private JLabel lab;
- private JTextField tf;
- private JButton but;
- private JComboBox<String> combo;
- private JButton but2;
- public MyApp() {
- setSize(800, 600);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setTitle("Mon application");
- JPanel panel = new JPanel(new BorderLayout());
- setContentPane(panel);
- JPanel top = new JPanel(new GridLayout(1,5));
- panel.add(top,BorderLayout.NORTH);
- lab = new JLabel("Input something:");
- tf = new JTextField();
- but = new JButton("OK");
- but2 = new JButton("ZZZZ");
- combo = new JComboBox<>(new String[]{"Blue","Green","Yellow"});
- top.add(lab);
- top.add(tf);
- top.add(but);
- top.add(but2);
- top.add(combo);
- but.addActionListener(this::doOK);
- but2.addActionListener(this::showOpts);
- combo.addActionListener(this::comboChanged);
- }
- public void showOpts(ActionEvent e) {
- String fav = (String) JOptionPane.showInputDialog(this,
- "What is your favorite pet?",
- "Favorite pet",
- JOptionPane.QUESTION_MESSAGE,
- null,
- new String[]{"Dog","Cat","Fish"},
- "Dog");
- }
- public void comboChanged(ActionEvent e) {
- tf.setText((String)combo.getSelectedItem());
- }
- public void doOK(ActionEvent e) {
- String txt = JOptionPane.showInputDialog("Entrez du texte");
- lab.setText(txt);
- }
- public static void main(String[] args) {
- MyApp f =new MyApp();
- f.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement