Creating Java dialogs

2019-01-25 22:59发布

What would be the easiest way for creating a dialog:

  • in one window I'm giving data for envelope addressing, also set font type from list of sizes
  • when clicked OK, in the same window or in next window I get preview of how the envelope would look like with the given names, and used selected font size

It should look similarly to:

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

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

5条回答
男人必须洒脱
2楼-- · 2019-01-25 23:30

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();
    }
}
查看更多
戒情不戒烟
3楼-- · 2019-01-25 23:30

If it is allowed to use a GUI builder I would recommend you IntelliJ IDEA's

You can create something like that in about 5 - 10 mins.

If that's not possible ( maybe you want to practice-learn-or-something-else ) I would use a JFrame instead ) with CardLayout

Shouldn't be that hard to do.

查看更多
Juvenile、少年°
4楼-- · 2019-01-25 23:46

You will need to use JDialog. No point messing about with JOptoinPane - it's not meant for gathering more than a simple string. Additionally, use either MigLayout, TableLayout, or JGoodies forms - it will help you get a nice layout that's easy to code.

查看更多
等我变得足够好
5楼-- · 2019-01-25 23:52

This should get you going.

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();
    }

    // ...
}

Result:

enter image description here

查看更多
疯言疯语
6楼-- · 2019-01-25 23:53

You can use JOptionPane. You can add any Swing component to it.

Create a panel with all the components you need except for the buttons and then add the panel to the option pane. The only problem with this approach is that focus will be on the buttons by default. To solve this problem see the solution presented by Dialog Focus.

查看更多
登录 后发表回答