Creating Java Dialogs

What would be the easiest way to create a dialogue:

  • In one window I give data for addressing envelopes, I also set the font type from the list of sizes
  • when I click OK, in the same window or in the next window, I get a preview of how the envelope with the specified names will look and used the selected font size

It should look like this:

alt text http://img15.imageshack.us/img15/7355/lab10aa.gif

Should I use Jdialog? Or will JOptionPane be enough? The next step is to choose the font color and background, so I have to keep that in mind.

+3
source share
5 answers

If you need to use JOptionPane:

import java.awt.*;
import javax.swing.*;

public class Main extends JFrame {

    private static JTextField nameField = new JTextField(20);
    private static JTextField surnameField = new JTextField();
    private static JTextField addr1Field = new JTextField();
    private static JTextField addr2Field = new JTextField();
    private static JComboBox sizes = new JComboBox(new String[] { "small", "medium", "large", "extra-large" });

    public Main(){
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        getContentPane().add(mainPanel);

        JPanel addrPanel = new JPanel(new GridLayout(0, 1));
        addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver"));
        addrPanel.add(new JLabel("Name"));
        addrPanel.add(nameField);
        addrPanel.add(new JLabel("Surname"));
        addrPanel.add(surnameField);
        addrPanel.add(new JLabel("Address 1"));
        addrPanel.add(addr1Field);
        addrPanel.add(new JLabel("Address 2"));
        addrPanel.add(addr2Field);
        mainPanel.add(addrPanel);
        mainPanel.add(new JLabel(" "));
        mainPanel.add(sizes);

        String[] buttons = { "OK", "Cancel"};

        int c = JOptionPane.showOptionDialog(
                null,
                mainPanel,
                "My Panel",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                buttons,
                buttons[0]
         );

        if(c ==0){
            new Envelope(nameField.getText(), surnameField.getText(), addr1Field.getText()
                    , addr2Field.getText(), sizes.getSelectedIndex());
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }
}
0

.

class TestDialog extends JDialog {

    private JButton okButton = new JButton(new AbstractAction("ok") {
        public void actionPerformed(ActionEvent e) {
            System.err.println("User clicked ok");
            // SHOW THE PREVIEW...
            setVisible(false);
        }
    });
    private JButton cancelButton = new JButton(new AbstractAction("cancel") {
        public void actionPerformed(ActionEvent e) {
            System.err.println("User clicked cancel");
            setVisible(false);
        }
    });

    private JTextField nameField = new JTextField(20);
    private JTextField surnameField = new JTextField();
    private JTextField addr1Field = new JTextField();
    private JTextField addr2Field = new JTextField();
    private JComboBox sizes = new JComboBox(new String[] { "small", "large" });

    public TestDialog(JFrame frame, boolean modal, String myMessage) {
        super(frame, "Envelope addressing", modal);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        getContentPane().add(mainPanel);

        JPanel addrPanel = new JPanel(new GridLayout(0, 1));
        addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver"));
        addrPanel.add(new JLabel("Name"));
        addrPanel.add(nameField);
        addrPanel.add(new JLabel("Surname"));
        addrPanel.add(surnameField);
        addrPanel.add(new JLabel("Address 1"));
        addrPanel.add(addr1Field);
        addrPanel.add(new JLabel("Address 2"));
        addrPanel.add(addr2Field);
        mainPanel.add(addrPanel);

        mainPanel.add(new JLabel(" "));

        mainPanel.add(sizes);
        JPanel buttons = new JPanel(new FlowLayout());
        buttons.add(okButton);
        buttons.add(cancelButton);

        mainPanel.add(buttons);

        pack();
        setLocationRelativeTo(frame);
        setVisible(true);
    }


    public String getAddr1() {
        return addr1Field.getText();
    }

    // ...
}

:

enter image description here

+2

JDialog. JOptoinPane - . , MigLayout, TableLayout, JGoodies forms - , .

0

GUI, IntelliJ IDEA

- 5-10 .

( , - - ), JFrame) CardLayout

.

0

JOptionPane. Swing.

Create a panel with all the necessary components, except for the buttons, and then add the panel to the options panel. The only problem with this approach is that the focus will be on the buttons by default. To resolve this issue, see the Solution presented by the dialog box .

0
source

Source: https://habr.com/ru/post/1747414/


All Articles