将数组的内容添加到JFrame,添加JPanel,添加一个JScroll

时间:2012-10-03 03:13:32

标签: java swing jpanel

String[] option = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas",
                   "Brebante", "Cabaylo", "C. Cainglet", "O. Cainglet",
                   "Calunsag", "Cameros", "Casanaan", "Catulong", "Chicote",
                   "Dela Paz", "Ea", "Echavez"};

int choose = JOptionPane.showOptionDialog(null, "WHICH EMPLOYEE'S DATA WOULD YOU
             LIKE TO RESET?", "Click to Choose Type of Leave Credits",
             JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
             null, option, option[16]);

它完美运行,我唯一的问题是JOptionPane(带有要选择的名称)不适合屏幕,如何将名称添加到框架或面板中并放置{{ 1}}进入它所以它适合屏幕。

1 个答案:

答案 0 :(得分:5)

您可以使用列表来显示选项,例如:

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class TestOptions {
    public static void main(String[] args) {
        String[] options = { "Adlawan", "Angeles", "Arreza", "Benenoso",
                "Bermas", "Brebante", "Cabaylo", "C. Cainglet", "O. Cainglet",
                "Calunsag", "Cameros", "Casanaan", "Catulong", "Chicote",
                "Dela Paz", "Ea", "Echavez" };

        JList list = new JList(options);
        JScrollPane scrollPane = new JScrollPane(list);

        int result = JOptionPane.showConfirmDialog(null, scrollPane,
                "Select employee", JOptionPane.OK_CANCEL_OPTION);

        if (result == JOptionPane.OK_OPTION) {
            JOptionPane.showMessageDialog(null, list.getSelectedValue());
        }
    }
}