- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 JFilechooser 打开一个文本文件并将字符串放入 JList 中。我认为所有字符串都会进入列表,但我不知道为什么这些字符串没有出现在 JScrollPane 上。分组布局有问题吗?我不知道要改变什么..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.List;
public class WordFinder extends JFrame implements ActionListener {
private WordList words = new WordList();
private JScrollPane scroll;
private JLabel label;
private JLabel word;
private JTextField textArea;
private JButton button;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem, menuItem2;
private JFileChooser fc;
private JList list;
static private final String newline = "\n";
private int lineCount;
public WordFinder() {
super("Word Finder");
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
menuBar = new JMenuBar();
menu = new JMenu("File");
menuBar.add(menu);
menuItem = new JMenuItem("Open...");
menuItem.addActionListener(this);
menuItem2 = new JMenuItem("Exit");
menuItem2.addActionListener(this);
menu.add(menuItem);
menu.add(menuItem2);
setJMenuBar(menuBar);
label = new JLabel("Find: ");
word = new JLabel(lineCount + " words total");
textArea = new JTextField();
textArea.setEditable(true);
textArea.setPreferredSize(new Dimension(200, 20));
button = new JButton("Clear");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
scroll = makeListView();
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setPreferredSize(new Dimension(200, 230));
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(label)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(textArea)
.addComponent(word)
.addComponent(scroll))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(button)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label)
.addComponent(textArea)
.addComponent(button))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(word))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
.addComponent(scroll));
setVisible(true);
pack();
// call System.exit() when user closes the window
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JScrollPane makeListView() {
// String[] labels = {"1", "2", "3"};
// list = new JList(labels);
JScrollPane listScroller = new JScrollPane(list);
return listScroller;
}
private void updateListView(DefaultListModel listModel) {
list = new JList(listModel);
scroll = new JScrollPane(list);
}
public void actionPerformed(ActionEvent e) {
DefaultListModel listModel = new DefaultListModel();
if (e.getSource() == menuItem) {
int returnVal = fc.showOpenDialog(WordFinder.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String fileName = file.getAbsolutePath();
try {
FileReader files = new FileReader(fileName);
BufferedReader br = new BufferedReader(files);
String str;
while((str = br.readLine()) != null) {
listModel.addElement(str);
//System.out.println(str);
lineCount++;
}
System.out.println(lineCount);
updateListView(listModel);
br.close();
} catch (Exception ex) {
ex.printStackTrace(System.out);
System.out.println("can't read file");
}
System.out.println("Opening: " + file.getName() + newline);
}
} else if (e.getSource() == menuItem2) {
setVisible(false);
dispose();
}
}
/**
* Main method. Makes and displays a WordFinder window.
* @param args Command-line arguments. Ignored.
*/
public static void main(String[] args) {
// In general, Swing objects should only be accessed from
// the event-handling thread -- not from the main thread
// or other threads you create yourself. SwingUtilities.invokeLater()
// is a standard idiom for switching to the event-handling thread.
SwingUtilities.invokeLater(new Runnable() {
public void run () {
// Make and display the WordFinder window.
new WordFinder();
}
});
}
}
最佳答案
当您调用makeListView
时,JList
为null
,因为它尚未初始化......因此,您基本上是在说scroll = new JScrollPane(null);
...这不是特别有用...
接下来,当您调用 updateListView
时,您将创建 JList
和 JScrollPane
的新实例,并且不对它们执行任何操作...
private void updateListView(DefaultListModel listModel) {
list = new JList(listModel);
scroll = new JScrollPane(list);
}
所以它们永远不会显示在屏幕上...
要纠正此问题,您需要进行一些修改...
JScrollPane
之前,创建 JList
的实例并将其分配给实例字段 list
list
和 scroll
的新实例,只需使用 JList#setModel
您可能还想看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
使用 JList
,您可以通过使用 JList#setVisibleRowCount
影响 JScrollPane
的大小。和 JList#setPrototypeCellValue
关于java - JList 不显示在 JScrollPane 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26421548/
我有一个 JList 的自定义单元格渲染器,它在其中显示一个嵌套的 JList 和另一个显示按钮的自定义单元格渲染器。我遇到的问题是任何用户输入(例如鼠标点击按钮、滚动操作)都没有得到处理,嵌套的 J
所以,我有两个选项卡,两个选项卡都有 JList,并且都有一个存储数据的数组列表,现在我需要找到一种方法,可以将数据从一个选项卡中的一个 JList 复制到另一个选项卡中的另一个 JList。 谢谢
您好,我需要有关填充第二个 Jlist 的紧急帮助,我已成功从数据库填充第一个 Jlist,但在找到填充第二个 Jlist 的方法时遇到了巨大困难。 我愿意, 从 Jlist1 中选择,单击按钮,将所
我有两个 Jlist( block 列表和层列表),当我单击层列表中的不同项目时,我尝试更新 block 列表的值。由于某种原因它不起作用。这是我的代码: JButton openButton,
如何从 JList 中获取 3 个单独的字符串?如果我使用 model.elementAt ,我会得到字符串,但它们不会彼此分离。 数据如何存储在 JList 中? 我可以使用数组将数据彼此分开吗?
我正在尝试创建一个循环,将项目添加到 jlist,但是当我放入 actionlistener 时,它似乎没有这样做...... textFieldSearch.addActionListener(ne
美好的一天,我是窗口构建器的新手(64 位 mac eclipse,如果重要的话)。鉴于教程代码中的错误数量,我猜它最近已经更新了很多。 基本上,我有一个位于面板中的 jlist 中的项目列表,以及位
我一直在这里进行研究并一直在寻找问题的解决方案。我是java新手,所以我不知道所有的语法。我试图让我的代码使用 jlist 之间的按钮将项目从一个 jlist 传输到另一个 jlist。左侧列表包含开
我创建了一个菜单,其中有 4 个类(class)。我为每道菜创建了一个 Jlist,其中包含不同的元素。我如何知道让这些选定的项目显示在另一个包含 Jlist 的类中的另一个 Jlist 中。 最佳答
我有一个 MySQL 数据库,其中包含我想填充到我的 Java 程序中的 JList 中的数据。我有两个 JList,一个填充事件标题,第二个填充客人姓名。 我想要的是当用户单击任何事件标题时,第二个
这是我的代码。为什么这些值没有进入第二个 JList?此外,第二个 Jlist 不可见。所有导入均已到位,并且在以下代码中不可见: public class Gui extends JFrame {
我有以下代码在工作 made_list.setListData(original_list.getSelectedValues()); 这里made_list是一个JList,original_lis
这是任何人都可以轻松快速运行的代码,它显示了我的问题: import java.awt.Color; import java.awt.Component; import java.awt.EventQ
这个问题已经有答案了: How to clear a JList in Java? (4 个回答) 已关闭 9 年前。 我正在使用 jlist.removeall 删除 jlist 中的所有项目,但它
我有一个 JList,它将根据用户指向选定的 JList。它的作用就像一个监视器,将监视选定的 JList。当我实现自定义单元格渲染器并以编程方式执行诸如 this.list = getSelecte
我有一个工作得非常完美的自定义渲染器 - 我如何将它用作“绘图引擎”来生成与 JList 中生成的相同对象? 最佳答案 how can I use it as a "drawing engine" t
我的程序是基于API的。我有一个 JList 和一个有一些名称的模型。还有一个用于获取所选项目的 selectListener 和一个按钮,用于将该项目发送到另一个窗口,该窗口还有另一个这是我的第一个
请耐心等待。我有一个包含 3 个 JList 的 Denist 程序。添加患者的患者 JList、添加患者历史的历史 jlist 以及应与历史 jlist 和患者 jlist 链接的发票历史。我让他们
我在这里遇到了一个问题,我创建了一个actionListener,它的目的是创建一个随机的人类并将其添加到JList以显示在JScrollPane上。到目前为止,一切都很顺利,除了每当我单击 JBut
我正在尝试创建一个带有 JList 和按钮的表单我想要做的是从 JList 中选择一个项目,然后按下按钮以根据选择执行操作。但是,一旦点击按钮,JList 就会失去焦点,选择消失,导致按钮无法确定选择
我是一名优秀的程序员,十分优秀!