gui section seven

GUI轮廓的小示例:

//:com/gui/start/Border.java
package com.gui.start;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class Borders extends JFrame{
    static JPanel showBorder(Border b) {
        JPanel jp = new JPanel();
        jp.setLayout(new BorderLayout());
        String nm = b.getClass().toString();
        nm = nm.substring(nm.lastIndexOf('.') + 1);
        jp.add(new JLabel(nm, JLabel.CENTER), BorderLayout.CENTER);
        jp.setBorder(b);
        return jp;
    }

    public Borders() {
        setLayout(new GridLayout(2,4));
        add(showBorder(new TitledBorder("Title")));
        add(showBorder(new EtchedBorder()));
        add(showBorder(new MatteBorder(5,5,30,30,Color.GREEN)));
        add(showBorder(new BevelBorder(BevelBorder.RAISED)));
        add(showBorder(new SoftBevelBorder(BevelBorder.LOWERED)));
        add(showBorder(new CompoundBorder(new EtchedBorder(), new LineBorder(Color.RED))));

    }

    public static void main(String[] args) {
        SwingConsole.run(new Borders(), 500, 300);
    }

}

unavailable

接下来演示的是下拉列表(感觉这个更加实用):

//:com/gui/start/ComboBoxes.java
package com.gui.start;

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

public class ComboBoxes extends JFrame{

    private String [] description = {
            "Ebullient", "Obtuse", "Recalcitrant", "Brilliant",
            "Somnescent", "Timorous", "Florid", "Putrescent"
    };
    private JTextField t = new JTextField(15);
    private JComboBox c = new JComboBox();
    private JButton b = new JButton("Add items");
    private int count = 0;

    public ComboBoxes() {
        for(int i = 0; i < 4; i++) {
            c.addItem(description[count++]);
        }
        t.setEditable(false);
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if(count < description.length) {
                    c.addItem(description[count++]);
                }
            }

        });

        c.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t.setText("index: " + c.getSelectedIndex() + " " + ((JComboBox)e.getSource()).getSelectedItem());
            }

        });
        setLayout(new FlowLayout());
        add(t);
        add(c);
        add(b);
    }

    public static void main(String[] args) {
        SwingConsole.run(new ComboBoxes(), 200, 175);
    }

}

文本框用于显示下拉列表中的选中项,其中getSelectedIndex()和getSelectedItem()是两个获取下标以及文本的方法
按钮使用addItem()方法向下拉列表中添加文本选项。

unavailable