Java by API/javax.swing/JDialog

Материал из Java эксперт
Перейти к: навигация, поиск

extends JDialog (Press Escape Key to close a dialog)

  
import java.awt.Frame;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 300);
    frame.setVisible(true);
    
    EscapeDialog esc = new EscapeDialog(frame);
    esc.setSize(200,200);
    esc.setVisible(true);
    
  }
}
class EscapeDialog extends JDialog { 
  public EscapeDialog(Frame owner) { 
    super(owner, true);
    getContentPane().add(new JComboBox());
  } 
  protected JRootPane createRootPane() { 
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() { 
      public void actionPerformed(ActionEvent actionEvent) { 
        setVisible(false);
      } 
    } ;
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);
    return rootPane;
  } 
}





JDialog: setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)

  

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class MyActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    JButton source = (JButton) e.getSource();
    String buttonText = source.getText();
    JOptionPane.showMessageDialog(null, "You clicked" + buttonText);
  }
}
public class Main {
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("ActionListener Test 1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Register");
    button.addActionListener(new MyActionListener());
    frame.getContentPane().add(button);
    frame.pack();
    frame.setVisible(true);
  }
}





JDialog: setModalityType(ModalityType type)

  
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Main {
  public static void main(String args[]) {
    final JFrame frame1 = new JFrame("Left");
    final JFrame frame2 = new JFrame("Right");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button1 = new JButton("Left");
    JButton button2 = new JButton("Right");
    frame1.add(button1);
    frame2.add(button2);
    ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JButton source = (JButton) e.getSource();
        JOptionPane pane = new JOptionPane("New label", JOptionPane.QUESTION_MESSAGE);
        pane.setWantsInput(true);
        JDialog dialog = pane.createDialog(frame2, "Enter Text");
        // dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
        dialog.setVisible(true);
        String text = (String) pane.getInputValue();
        if (!JOptionPane.UNINITIALIZED_VALUE.equals(text) && text.trim().length() > 0) {
          source.setText(text);
        }
      }
    };
    button1.addActionListener(listener);
    button2.addActionListener(listener);
    frame1.setBounds(100, 100, 200, 200);
    frame1.setVisible(true);
    frame2.setBounds(400, 100, 200, 200);
    frame2.setVisible(true);
  }
}





new JDialog(Window owner, String title, ModalityType modalityType)

  
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] args) {
    final JFrame parent1 = new JFrame("Parent Frame 1");
    parent1.setLayout(new FlowLayout());
    parent1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Application modal dialog");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JDialog dialog = new JDialog(parent1, "Application-Modal Dialog",
            Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setBounds(200, 150, 200, 150);
        dialog.setVisible(true);
      }
    });
    parent1.add(button);
    parent1.setBounds(100, 100, 200, 150);
    parent1.setVisible(true);
    JFrame parent2 = new JFrame("Parent Frame 2");
    parent2.setBounds(500, 100, 200, 150);
    parent2.setVisible(true);
  }
}