2013-12-19 61 views
0

我目前正在創建一個類似於應用程序的音板,其中人員要點擊我的JList中的其中一個項目並將其設置爲下一個可用的文本字段,就像剛纔點擊的那樣。例如,JList包含hello, testing1, testing2。如果首先點擊testing2,我想把它放到第一個文本框中,如果hello被點擊下一步,我想把它放到第二個文本框中,依此類推。JList單擊一個,然後將其放入第一個JTextfield,然後單擊另一個到下一個JTextfield

該應用程序完成時,程序將在JList中包含約100個項目。我目前無法得到這個工作,並嘗試過無數次。

此外還有一個問題,當點擊頂部和不同的JList項目時,頂部的項目將首先顯示。不一定是問題,如果我可以使問題完全發揮作用,但它使它感覺有點不習慣。

到目前爲止我的代碼:

package com.leagueoflegends.soundboard; 

import javax.swing.*; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

import java.awt.*; 

public class Soundboard implements ListSelectionListener { 

static JList<Object> list; 
String[] text = { "hello", "testing1", "testing2" }; 
Icon icon; 
JLabel pictureLabel; 
JPanel insidePanel; 
JTextField inlineText; 

JTextField field[] = new JTextField[6]; 

public Soundboard() { 
    JFrame f = new JFrame("soundboard!"); 

    JPanel masterPanel = new JPanel(new BorderLayout()); 

    //icon = new ImageIcon(getClass().getResource("Tray.png")); 
    //pictureLabel = new JLabel(icon); 

    list = new JList<Object>(text); // data has type Object[] 
    list.setSelectionModel(new DefaultListSelectionModel(){ 
     public void setSelectionInterval(int index0, int index1){ 
      if(super.isSelectedIndex(index0)){ 
       super.removeSelectionInterval(index0,index1); 
      }else{ 
       super.addSelectionInterval(index0,index1); 
      } 
     } 
    }); 
    list.setLayoutOrientation(JList.VERTICAL_WRAP); 
    list.setVisibleRowCount(-1); 
    list.addListSelectionListener(this); 

    JScrollPane listScroller = new JScrollPane(list); 
    listScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
    listScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    listScroller.setPreferredSize(new Dimension(100, 60)); 
    // listScroller.setSize(new Dimension(250, 60)); 

    JPanel smallPanel = new JPanel(new GridLayout(2, 3)); 
    // smallPanel.setBorder(BorderFactory.createLineBorder(Color.red)); 
    for (int i = 0; i <= 5; i++) { 
     insidePanel = new JPanel(new BorderLayout()); 
     insidePanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
     field[i] = new JTextField(); 
     field[i].setEditable(false); 
     field[i].setHorizontalAlignment(JTextField.CENTER); 
     insidePanel.add(field[i], BorderLayout.PAGE_START); 
     smallPanel.add(insidePanel); 

    } 

    masterPanel.add(smallPanel); 
// masterPanel.add(pictureLabel, BorderLayout.PAGE_START); 
    masterPanel.add(listScroller, BorderLayout.WEST); 
    f.add(masterPanel); 

    f.pack(); 
    f.setSize(1000, 800); 
    f.setMinimumSize(new Dimension(400, 350)); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
} 

@Override 
public void valueChanged(ListSelectionEvent e) { 
    if (e.getValueIsAdjusting() == false) { 
     for (int i = 0; i < text.length; i++) { 
      if (list.getSelectedIndex() == i) { 
       field[0].setText(text[i]); 
      } 
     } 
    } 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException 
        | UnsupportedLookAndFeelException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      new Soundboard(); 
     } 
    }); 
} 

}

回答

0

一般的想法是;

  1. 創建一個數組或List你的JTextField s。添加這些你的UI。
  2. 創建一個計數器,指示當前「空」字段
  3. 使用時,用戶已經選擇的東西無論是MouseListenerListSelectionListener識別,提取從列表中選擇值。使用「當前」櫃檯,提取從ListJTextField,並設置它的值。因此,增加計數器
相關問題