我写了一个程序,但是当我启动他时,我的JList没有显示。任何人都可以看到什么是坏的? https://pastebin.com/CWFnSjen,它的工作原理如下:在JTextField中我需要输入一个Word,然后点击"输入"必须将此世界添加到JList
public static void main(String[] args)
{
new DrawSwing();
}
public DrawSwing()
{
SwingUtilities.invokeLater(() -> createGUI());
}
protected void createGUI()
{
JPanel main = new JPanel();
main.setLayout(new BorderLayout());
WlasnyMode<String> model = new WlasnyMode<>();
MyTextField text = new MyTextField(model);
MyFrame jf = new MyFrame();
jf.setTitle("Rysowanie");
jf.setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);
jf.setLocation(50,50); jf.setResizable(true);
JPanel uper =new JPanel();
uper.setLayout(new GridLayout(1,1));
JPanel textPanel = new JPanel();
jf.setLayout(new BorderLayout());
textPanel.setLayout(new BorderLayout());
uper.add(text);
textPanel.setPreferredSize(new Dimension(500,500));
JList<String> lista = new JList<>(model);
JScrollPane scroll = new JScrollPane(lista);
textPanel.add(scroll);
lista.setCellRenderer(new MyCellRenderer());
lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lista.setLayoutOrientation(JList.VERTICAL);
lista.setVisibleRowCount(30);
lista.setBounds(100,100 , 300, 300);
textPanel.add(lista);
main.add(uper,BorderLayout.NORTH);
main.add(textPanel,BorderLayout.CENTER);
jf.setContentPane(main);
jf.pack( );
jf.setVisible(true);
}
}
答案 0 :(得分:0)
你JList
看起来很好。如果我在您的代码中添加lista.setBackground(Color.BLUE);
,则可以清楚地看到JList
。
问题的一部分是你把它包装在另一个JPanel
(textPanel
)中,这通常搞砸了很多东西。您不需要它,它将有助于降低代码中的一些复杂性
“核心”问题在于您ListModel
的实施。更改模型时,您需要生成适当的事件。由于列表的整个结构已更改,因此您需要使用fireContentsChanged
,例如......
public class WlasnyMode<T> extends AbstractListModel<String> {
//...
void addElement(String text) {
this.text.add(text);
Collections.sort(this.text);
fireContentsChanged(this, 0, this.text.size() - 1);
}
}
此外,KeyListener
是处理输入的不良选择,尤其是当ActionListener
以独立于平台的方式提供相同的工作时......
public class MyTextField extends JTextField implements ActionListener {
WlasnyMode model;
MyTextField(WlasnyMode model) {
super();
this.model = model;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
model.addElement(this.getText());
System.out.println(this.getText());
}
}
但这也引发了一系列关于从JTextField
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.*;
import javax.swing.event.ListDataListener;
public class DrawSwing {
public static void main(String[] args) {
new DrawSwing();
}
public DrawSwing() {
SwingUtilities.invokeLater(() -> createGUI());
}
protected void createGUI() {
// utworzenie okna
JPanel main = new JPanel();
main.setLayout(new BorderLayout());
WlasnyMode<String> model = new WlasnyMode<>();
MyTextField text = new MyTextField(model);
JFrame jf = new JFrame();
// określenie tytułu okna
jf.setTitle("Rysowanie");
// obsługa zamknięcia okna JFrame
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// określenie położenia okna
jf.setLocation(50, 50);
// uniemoĹĽliwienie zmiany rozmiarĂłw okna
jf.setResizable(true);
JPanel uper = new JPanel();
uper.setLayout(new GridLayout(1, 1));
JPanel textPanel = new JPanel();
jf.setLayout(new BorderLayout());
textPanel.setLayout(new BorderLayout());
uper.add(text);
JList<String> lista = new JList<>(model);
JScrollPane scroll = new JScrollPane(lista);
lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lista.setLayoutOrientation(JList.VERTICAL);
lista.setVisibleRowCount(30);
lista.setBackground(Color.BLUE);
main.add(uper, BorderLayout.NORTH);
main.add(scroll, BorderLayout.CENTER);
//jf.addKeyListener('f');
// utworzenie obszaru rysowania - pulpitu
// wymiana domyślnego pulpitu na własny
jf.setContentPane(main);
// upakowanie okna
jf.pack();
// wyświetlenie okna
jf.setVisible(true);
}
public class WlasnyMode<T> extends AbstractListModel<String> {
ArrayList<String> text = new ArrayList<>();
//WlasnyMode()
//{
// super();
// }
@Override
public String getElementAt(int liczba) {
// TODO Auto-generated method stub
return text.get(liczba);
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return text.size();
}
void addElement(String text) {
this.text.add(text);
Collections.sort(this.text);
fireContentsChanged(this, 0, this.text.size() - 1);
}
void remove(int index) {
text.remove(index);
}
void removeRange(int fromIndex, int toIndex) {
for (int i = fromIndex; i < toIndex; i++) {
text.remove(i);
}
}
public void addListDataListener(ListDataListener l) {
super.addListDataListener(l);
}
}
public class MyTextField extends JTextField implements ActionListener {
WlasnyMode model;
MyTextField(WlasnyMode model) {
super();
this.model = model;
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
model.addElement(this.getText());
System.out.println(this.getText());
}
}
}