Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

user interface - Creating Java dialogs

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...