我有一个问题,如何为数组中的每个元素将现有的数组字符串导入jlist
,我知道很多人以前都问过这个问题,但是大多数情况下,我看到了如何使用jButton
,我也听说过
DefaultListModel
,但没有用
private void JListAction(java.awt.event.ActionEvent evt) {
DefaultListModel listModel = new DefaultListModel();
for (int i = 0; i < dm.getSize(); i++) {
listModel.addElement(dic.newWord.get(i).getWordExplain());
}
jList2.setModel(listModel);
}
这只是我的GUI代码
[import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Quốc Khánh
*/
public class GUI extends javax.swing.JFrame {
DictionaryCommandline dcl = new DictionaryCommandline();
DictionaryManagement dm = new DictionaryManagement();
Dictionary dic = new Dictionary();
DictionaryApplication dicA = new DictionaryApplication();
private Object textArea;
Font font = new Font("consolas", Font.BOLD, 18);
/**
* Creates new form GUI
*/
public GUI() {
setTitle("từ điển Anh Việt v1.0");
dm.insertFromFile(dic);
initComponents();
jTextPane1.setFont(font);
jTextArea1.setFont(font);
jTextArea1.setLineWrap(true);
jTextArea1.setForeground(Color.BLACK);
jButton1.setFont(font);
JScrollPane pane=new JScrollPane(jList2);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jButton1 = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
jList2 = new javax.swing.JList<>();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(jTextPane1);
jButton1.setText("translate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane3.setViewportView(jTextArea1);
jScrollPane4.setViewportView(jList2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(54, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane3)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
.addContainerGap(67, Short.MAX_VALUE))
);
jButton1.getAccessibleContext().setAccessibleName("translate");
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String word = jTextPane1.getText();
String mean = dicA.getWord(dic, dm.getSize(), dm, word);
jTextArea1.setText(dicA.getWordEx(dic, dm.getSize(), dm, mean));
}
private void JListAction(java.awt.event.ActionEvent evt) {
DefaultListModel listModel = new DefaultListModel();
for (int i = 0; i < dm.getSize(); i++) {
listModel.addElement(dic.newWord.get(i).getWordExplain());
}
jList2.setModel(listModel);
}
/**
* @param args the command line arguments
*/
public static void main(String\[\] args) {
java.awt.EventQueue.invokeLater(() -> {
new GUI().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JList<String> jList2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration
/**
*
*/
}
答案 0 :(得分:1)
生成GUI时,您的字符串数组是否已经可用?如果是这样,您可以将其用作参数直接将其插入JList。
您可以将String []放在方括号中,而不是使用JList jList2 = new javax.swing.JList<>();
(在initComponent()方法的第5行中完成):
String[] data = {"a","b","c"};
JList jList2 = new javax.swing.JList<>(data);
JScrollPane pane=new JScrollPane(jList2);
在这种情况下,您的ScrollPane看起来类似于this