我正在创建一个类似于应用程序的音板,其中该人将点击我JList
中的一个项目,并将其设置为刚刚点击的下一个可用文本字段的名称。
例如,我的JList
包含hello, testing1, testing2
。如果首先点击testing2
,我想将其放入第一个文本字段,如果点击了hello
,我想将其放入第二个文本字段,依此类推。
在应用程序完成时,程序将在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 :(得分:0)
一般的想法是;
List
的数组或JTextField
。添加这些UI。MouseListener
或ListSelectionListener
来确定用户何时选择了某些内容,从列表中提取值。使用“当前”计数器,从JTextField
中提取List
并相应地设置其值,递增计数器