亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

桂互動。。

桂互動。。

aluckdog 2022-06-23 17:24:55
這是我第一次創建 Gui,我對如何創建交互感到困惑。我正在嘗試在組合框為單個時實現單一選擇模式,而在組合框被放置在多個時實現多個。我把它們放在多行注釋上。有任何想法嗎?//交互//選擇“單個”時,JLIST會更改,因此只能選擇一個項目。//選擇“多個”時,JLIST會更改,因此可以選擇多個項目//當一個國家或多個國家被選中時,JLabel 會改變以反映新的選擇public class GuiTest {public static String[] Countries = {"Africa", "Haiti", "USA", "Poland", "Russia", "Canada", "Mexico", "Cuba"};public static String[] Selection = {"Single", "Multiple"};JPanel p = new JPanel();JButton b = new JButton("Testing");JComboBox jc = new JComboBox(Selection);JList jl = new JList(Countries);private static void constructGUI() {    JFrame.setDefaultLookAndFeelDecorated(true);    JFrame frame = new JFrame();    frame.setTitle("Countries Selection");    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    // add a JLabel that says Welcome    JLabel label = new JLabel("Selected Items:");    frame.add(label);    frame.pack();    JComboBox jc = new JComboBox(Selection);    frame.add(jc);    frame.pack();    frame.setVisible(true);    JList jl = new JList(Countries);    frame.add(jl);    frame.pack();    JComponent panel = new JPanel();    panel.setLayout(new FlowLayout());    panel.add(new JLabel("Choose Selection Mode:"));    panel.add(jc);    frame.add(panel, BorderLayout.NORTH);    frame.add(jl, BorderLayout.WEST);    frame.add(label, BorderLayout.SOUTH);}public static void main(String[] args) {    SwingUtilities.invokeLater(new Runnable() {        public void run() {            constructGUI();        }    });}}
查看完整描述

2 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

它會是這樣的:


    jc.addActionListener((evt) -> {

        if ("Single".equals(jc.getSelectedItem())) {

            jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            int[] sel = jl.getSelectedIndices();

            if (sel != null && sel.length > 1) {

                jl.setSelectedIndex(sel[0]);

            }

        } else {

            jl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        }

    });

    jl.addListSelectionListener((evt) -> {

        StringBuilder buf = new StringBuilder();

        for (Object o: jl.getSelectedValuesList()) {

            if (buf.length() > 0) {

                buf.append(", ");

            }

            buf.append(o);

        }

        label.setText(buf.toString());

    });

    jc.setSelectedItem("Single");


查看完整回答
反對 回復 2022-06-23
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

您應該開始將模式添加到 ComboBox:


comboBoxCategoria.addItem("Single",0);

comboBoxCategoria.addItem("Multiple",1);

然后在你的 ComboBox 中添加一個 ActionListener 來修改列表選擇模式


jc.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent arg0) {

    if(jc.getSelectedItem().equals("Single")){

      jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    }else{//must equals

      jl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    }

  }

});

最后在列表上添加一個 MouseListener 以檢測列表選擇的變化并更改 JLabel 以反映新的選擇


jl.addMouseListener(new MouseAdapter() {

  @Override

  public void mouseReleased(MouseEvent e) { 

    label.setText(list.getSelectedValuesList().toString());

  }

});

編輯:您還應該添加一個 KeyListener 來更新標簽,因為可以通過箭頭鍵更改選擇


jl.addKeyListener(new KeyAdapter() {

  @Override

  public void keyReleased(KeyEvent e) {

    label.setText(list.getSelectedValuesList().toString());

  }

});


查看完整回答
反對 回復 2022-06-23
  • 2 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號