⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tutorial.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        String[] messages = { "This is an example of an Input Dialog",
                "that displays an array of Strings"};
        String result = JOptionPane.showInputDialog(this, messages,
                "Input a value", JOptionPane.QUESTION_MESSAGE);
        String msg = null;
        if (result == null)
            msg = "User selected Cancel option";
        else
            msg = "User entered \"" + result + "\"";
        JOptionPane.showMessageDialog(this, msg, "Result of showInputDialog",
                JOptionPane.PLAIN_MESSAGE);
    }

    /**
     * Demonstrate how to customize the JOptionPane.
     */
    private void showCustomInputDialog(Component parent_) {

        String[] results = { "", ""};
        String selected_option = null;

        String[] message = { "This shows how to create and use",
                "a JOptionPane directly, without using",
                "the convenience methods"};
        JOptionPane pane = new JOptionPane(message);

        // Make the dialog display a JTextField for user input.
        pane.setWantsInput(true);

        // Set the initial input value displayed to the user.
        pane.setInitialSelectionValue("default input value");

        // Provide customized button labels.
        String[] options = { "Option 1", "Option 2", "Option 3", "Option 4"};
        pane.setOptions(options);

        // Make "Option 2" the default button.
        pane.setInitialValue("Option 2");

        JDialog dialog = pane.createDialog(parent_, "Custom JOptionPane");
        dialog.show();
        Object selectedValue = pane.getValue();
        System.err.println("Selected value is " + selectedValue);
        results[ 0] = "The input value is \"" + (String) pane.getInputValue()
                + "\"";

        // If there is NOT an array of option buttons:
        // (In this case, there is).
        if (pane.getOptions() == null) {
            int option = ((Integer) selectedValue).intValue();
            switch (option) {
            case JOptionPane.YES_OPTION:
                selected_option = "YES";
                break;
            case JOptionPane.OK_OPTION:
                selected_option = "OK";
                break;
            case JOptionPane.NO_OPTION:
                selected_option = "NO";
                break;
            case JOptionPane.CANCEL_OPTION:
                selected_option = "CANCEL";
                break;
            }
        } else {
            // If there IS an array of option buttons:
            for (int i = 0; i < options.length; i++) {
                if (options[ i].equals(selectedValue)) {
                    selected_option = options[ i];
                    break;
                }
            } // end for
        }
        results[ 1] = "The selected option is \"" + selected_option + "\"";

        /*
         * Change the (static) labels and accelerators in the JOptionPane. Note
         * that the buttons stay customized for future invocations of the
         * JOptionPane methods, until they are customized again.
         * JOptionPane.OK_ACCELERATOR = KeyEvent.VK_F5;
         * JOptionPane.YES_ACCELERATOR = KeyEvent.VK_F6;
         * JOptionPane.NO_ACCELERATOR = KeyEvent.VK_F7;
         * JOptionPane.CANCEL_ACCELERATOR = KeyEvent.VK_F8;
         */

        JOptionPane.showMessageDialog(this, results,
                "Result of Customized JOptionPane", JOptionPane.PLAIN_MESSAGE);
    }

}

/**
 * This class demonstrates how to lay out components manually, by setting the
 * LayoutManager to "null". In Charva, you can set a component's position
 * within its container with "setLocation()"; but in Swing, you have to use
 * "setBounds()". Also, of course, in Charva the units are rows and columns
 * whereas in Swing they are pixels.
 */

class NullLayoutTest extends JDialog implements ActionListener {

    public NullLayoutTest(Frame owner_) {
        super(owner_, "Null Layout Test");
        setLocation(3, 3);
        setSize(60, 20);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        JLabel label0 = new JLabel(
                "Demonstrates how to lay components out manually");
        contentPane.add(label0);
        label0.setLocation(2, 2);

        JPanel panel1 = new JPanel();
        panel1.setLayout(null);
        contentPane.add(panel1);
        panel1.setLocation(2, 3);
        panel1.setSize(40, 6);
        panel1.setBorder(new TitledBorder("Panel1"));

        JLabel label1 = new JLabel("Label 1:");
        panel1.add(label1);
        label1.setLocation(1, 2);

        JTextField textfield1 = new JTextField("Text Field 1");
        panel1.add(textfield1);
        textfield1.setLocation(11, 2);

        JPanel panel2 = new JPanel();
        panel2.setLayout(null);
        contentPane.add(panel2);
        panel2.setLocation(2, 10);
        panel2.setSize(40, 6);
        panel2.setBorder(new TitledBorder("Panel2"));

        JLabel label2 = new JLabel("Label 2:");
        panel2.add(label2);
        label2.setLocation(1, 2);

        JTextField textfield2 = new JTextField("Text Field 2");
        panel2.add(textfield2);
        textfield2.setLocation(11, 2);

        JButton okButton = new JButton("OK");
        okButton.addActionListener(this);
        contentPane.add(okButton);
        okButton.setLocation(25, 17);
        okButton.setMnemonic(0x18); // CTRL-X
    }

    public void actionPerformed(ActionEvent ae_) {
        if (ae_.getActionCommand().equals("OK")) {
            hide();
        }
    }
}

/**
 * This class demonstrates how to use the BorderLayout (which is the default
 * layout for JFrame and JDialog), the BoxLayout and the FlowLayout (which is
 * the default layout for JPanel).
 */

class LayoutTest extends JDialog implements ActionListener {

    public LayoutTest(Frame owner_) {
        super(owner_, "Miscellaneous Layout Test");
        setLocation(3, 3);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout()); // default layout for
                                                   // JDialog

        JPanel toppan = new JPanel();
        toppan.setBorder(new TitledBorder("North Panel"));
        toppan.add(new JLabel("north panel uses FlowLayout"));
        contentPane.add(toppan, BorderLayout.NORTH);

        JPanel westpan = new JPanel();
        westpan.setBorder(new TitledBorder("West Panel"));
        westpan.setLayout(new BoxLayout(westpan, BoxLayout.Y_AXIS));
        westpan.add(new JLabel("west panel uses BoxLayout"));
        westpan.add(new JTextField("JTextField #1."));
        westpan.add(new JTextField("JTextField #2."));
        westpan.add(new JTextField("JTextField #3."));
        westpan.add(new JTextField("JTextField #4."));
        westpan.add(new JTextField("JTextField #5."));
        contentPane.add(westpan, BorderLayout.WEST);

        JPanel eastpan = new JPanel();
        eastpan.setBorder(new TitledBorder("East Panel"));
        eastpan.add(new JTextField("A JTextField"));
        contentPane.add(eastpan, BorderLayout.EAST);

        JPanel centerpan = new JPanel();
        centerpan.setLayout(new BorderLayout());
        centerpan.setBorder(new TitledBorder("Center Panel"));
        centerpan.add(new JLabel("A label in the center"), BorderLayout.CENTER);
        contentPane.add(centerpan, BorderLayout.CENTER);

        JPanel southpan = new JPanel();
        southpan.setBorder(new TitledBorder("South Panel"));
        southpan.add(new JLabel("A label in the south: "));
        JButton okButton = new JButton("OK");
        okButton.addActionListener(this);
        southpan.add(okButton);
        contentPane.add(southpan, BorderLayout.SOUTH);
        okButton.setMnemonic(KeyEvent.VK_F10);
        pack();
    }

    public void actionPerformed(ActionEvent ae_) {
        if (ae_.getActionCommand().equals("OK")) {
            hide();
        }
    }
}

/**
 * This class is based on the MiscellaneousLayoutTest but it demonstrates how
 * to set the foreground and background colors of dialogs and components.
 */

class ColorLayoutTest extends JDialog implements ActionListener {

    public ColorLayoutTest(Frame owner_) {
        super(owner_,
                "Layout Test in Color (yellow foreground, green background)");
        setLocation(3, 3);
        setForeground(Color.yellow);
        setBackground(Color.green);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout()); // default layout for
                                                   // JDialog

        JPanel toppan = new JPanel();
        toppan.setBorder(new TitledBorder(
                "North Panel (inherits green background)"));
        toppan.setForeground(Color.blue);
        contentPane.add(toppan, BorderLayout.NORTH);

        JRadioButton button1 = new JRadioButton("A JRadioButton...  ");
        JRadioButton button2 = new JRadioButton("And another JRadioButton");
        ButtonGroup buttons = new ButtonGroup();
        buttons.add(button1);
        buttons.add(button2);
        button1.setSelected(true);
        toppan.add(button1);
        toppan.add(button2);

        JPanel westpan = new JPanel();
        westpan.setBorder(new TitledBorder("West Panel"));
        westpan.setLayout(new BoxLayout(westpan, BoxLayout.Y_AXIS));
        JLabel label1 = new JLabel("Magenta label in west panel");
        westpan.add(label1);
        label1.setForeground(Color.magenta);
        JTextField textfield1 = new JTextField(
                "Cyan JTextField, red background");
        textfield1.setForeground(Color.cyan);
        textfield1.setBackground(Color.red);
        westpan.add(textfield1);

        JTextField whiteTextField = new JTextField("White JTextField");
        whiteTextField.setForeground(Color.white);
        westpan.add(whiteTextField);
        JTextField blueTextField = new JTextField("Blue JTextField");
        blueTextField.setForeground(Color.blue);
        westpan.add(blueTextField);
        JTextField yellowTextField = new JTextField("Yellow JTextField");
        yellowTextField.setForeground(Color.yellow);
        westpan.add(yellowTextField);
        JTextField blackTextField = new JTextField("Black JTextField");
        blackTextField.setForeground(Color.black);
        westpan.add(blackTextField);
        contentPane.add(westpan, BorderLayout.WEST);

        JPanel eastpan = new JPanel();
        eastpan.setForeground(Color.black);
        eastpan.setBorder(new TitledBorder("East Panel"));
        eastpan.add(new JTextField("A JTextField"));
        contentPane.add(eastpan, BorderLayout.EAST);

        JPanel centerpan = new JPanel();
        centerpan.setForeground(Color.white);
        centerpan.setBackground(Color.black);
        centerpan.setLayout(new BorderLayout());
        LineBorder centerpan_lineborder = new LineBorder(Color.green);
        TitledBorder centerpan_titledborder = new TitledBorder(
                centerpan_lineborder, "Green border, yellow title", 0, 0, null,
                Color.yellow);
        centerpan.setBorder(centerpan_titledborder);
        centerpan.add(new JLabel("A white label in the center"),
                BorderLayout.CENTER);
        contentPane.add(centerpan, BorderLayout.CENTER);

        JPanel southpan = new JPanel();
        southpan.setBorder(new TitledBorder("South Panel (white foreground)"));
        southpan.setBackground(Color.blue);
        southpan.setForeground(Color.white);
        JLabel labelsouth = new JLabel("A green label in the south panel ");
        labelsouth.setForeground(Color.green);
        southpan.add(labelsouth);
        JButton okButton = new JButton("OK");
        okButton.addActionListener(this);
        southpan.add(okButton);
        contentPane.add(southpan, BorderLayout.SOUTH);
        okButton.setMnemonic(KeyEvent.VK_F10);
        pack();
    }

    public void actionPerformed(ActionEvent ae_) {
        if (ae_.getActionCommand().equals("OK")) {
            hide();
        }
    }
}

/**
 * This class demonstrates how to use the GridBagLayout.
 */

class GridBagLayoutTest extends JDialog implements ActionListener,
        ListSelectionListener {

    private JTextField lastnameField = new JTextField(25);

    private JTextField initialsField = new JTextField(5);

    private JTextField address1Field = new JTextField(20);

    private JTextField address2Field = new JTextField(20);

    private JTextField cityField = new JTextField(20);

    private JTextField postcodeField = new JTextField(8);

    private JTextField stateField = new JTextField(15);

    public GridBagLayoutTest(Frame owner_) {
        super(owner_, "GridBagLayout Test");
        Container contentPane = getContentPane();

        JPanel centerpan = new JPanel();
        centerpan.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        centerpan.add(new JLabel("Last name: "), gbc);

        gbc.gridy = 1;
        centerpan.add(new JLabel("Address line 1: "), gbc);

        gbc.gridy = 2;
        centerpan.add(new JLabel("Address line 2: "), gbc);

        gbc.gridy = 3;
        centerpan.add(new JLabel("City: "), gbc);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -