2013-12-19 44 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,并设置它的值。因此,增加计数器
相关问题