- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个在单击按钮时执行的函数。假设有一个循环将 1 加到 10 到 JList
。我将该数据添加到 DefaultListModel
。它完美地工作并且数字被添加。然后我在循环中添加了一个 Thread.sleep(1000)
。但是输出是不同的。我想每秒添加 1 个元素。但现在它等待 10 秒,并在第 10 秒结束时将所有 1 加到 10。我哪里错了吗?
List processList = listNumbers.getSelectedValuesList();
DefaultListModel resultList = new DefaultListModel();
listResult.setModel(resultList);
for (int i = 0; i < processList.size(); i++) {
resultList.addElement(String.valueOf(i));
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
}
}
最佳答案
不要使用,真的不要在EventDispashThread
期间使用Thread.sleep(int)
,因为 sleep 锁定了当前的Thread
并且在本例 EventDispashThread
,向 GUI 输出非预期的结果,更多信息在 Concurency in Swing 中,
如果你需要一些东西来延迟,然后将 Items wrappend 添加到 Runneble#Thread
中,将输出到 GUI 包装到 invokeLater
中,或者最好的方法是使用 javax.swing.Timer
编辑第一。示例如何使用 Thread.sleep(int) 以编程方式阻止 EDT,因为 othewise 不适用于期望输出到 GUI(代码非常冗长,因为我想编写代码)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShakingButtonDemo implements Runnable {
private JButton button;
private JRadioButton radioWholeButton;
private JRadioButton radioTextOnly;
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new ShakingButtonDemo());
}
@Override
public void run() {
radioWholeButton = new JRadioButton("The whole button");
radioTextOnly = new JRadioButton("Button text only");
radioWholeButton.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(radioWholeButton);
bg.add(radioTextOnly);
button = new JButton(" Shake with this Button ");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
shakeButton(radioWholeButton.isSelected());
}
});
JPanel p1 = new JPanel();
p1.setBorder(BorderFactory.createTitledBorder("Shake Options"));
p1.setLayout(new GridLayout(0, 1));
p1.add(radioWholeButton);
p1.add(radioTextOnly);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(0, 1));
p2.add(button);
JFrame frame = new JFrame();
frame.setTitle("Shaking Button Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(p1, BorderLayout.NORTH);
frame.add(p2, BorderLayout.SOUTH);
frame.setSize(240, 160);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void shakeButton(final boolean shakeWholeButton) {
final Point point = button.getLocation();
final Insets margin = button.getMargin();
final int delay = 75;
Runnable r = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
try {
if (shakeWholeButton) {
moveButton(new Point(point.x + 5, point.y));
Thread.sleep(delay);
moveButton(point);
Thread.sleep(delay);
moveButton(new Point(point.x - 5, point.y));
Thread.sleep(delay);
moveButton(point);
Thread.sleep(delay);
} else {// text only
setButtonMargin(new Insets(margin.top, margin.left + 3, margin.bottom, margin.right - 2));
Thread.sleep(delay);
setButtonMargin(margin);
Thread.sleep(delay);
setButtonMargin(new Insets(margin.top, margin.left - 2, margin.bottom, margin.right + 3));
Thread.sleep(delay);
setButtonMargin(margin);
Thread.sleep(delay);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
};
Thread t = new Thread(r);
t.start();
}
private void moveButton(final Point p) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
button.setLocation(p);
}
});
}
private void setButtonMargin(final Insets margin) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
button.setMargin(margin);
}
});
}
}
编辑第二。与@camickr 的 kind hepl(看起来像 similair)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ShakeComponents1 {
private JFrame frame = new JFrame();
private final String items[] = {"One", "Two", "Three"};
private Timer timer;
private JPanel panel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JButton button = new JButton(" Exit ");
private boolean repeats = true;
private boolean runs = false;
private Color clr[] = {Color.red, Color.blue, Color.magenta};
private Insets initMargin;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ShakeComponents1().makeUI();
}
});
}
public void makeUI() {
buttonPanel = new JPanel();
buttonPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
buttonPanel.setLayout(new BorderLayout());
button.setPreferredSize(new Dimension(100, 45));
button.setForeground(Color.darkGray);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
Runnable doRun = new Runnable() {
@Override
public void run() {
System.exit(0);
}
};
SwingUtilities.invokeLater(doRun);
}
});
button.addMouseListener(new java.awt.event.MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
if (runs) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
runs = false;
timer.stop();
changePnlBorder(new EmptyBorder(5, 5, 5, 5));
changeBtnForegroung(Color.darkGray);
}
});
}
}
@Override
public void mouseExited(MouseEvent e) {
if (!runs) {
timer.start();
runs = true;
}
}
});
buttonPanel.add(button);
final Insets margin = button.getMargin();
panel.add(buttonPanel);
for (int i = 0; i < 2; i++) {
JComboBox combo = new JComboBox(items);
combo.setMinimumSize(new Dimension(50, 25));
combo.setMaximumSize(new Dimension(150, 25));
combo.setPreferredSize(new Dimension(100, 25));
combo.addActionListener(new ShakeAction());
panel.add(combo);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocation(50, 50);
frame.setVisible(true);
timer = new Timer(500, new ShakeAction());
timer.setRepeats(repeats);
initMargin = button.getMargin();
}
private class ShakeAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private int noColor = 0;
private Border border;
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
if (count > 5) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(750);
changeBtnForegroung(Color.darkGray);
Thread.sleep(750);
count = 0;
Thread.sleep(750);
} catch (Exception e) {
System.out.println(e);
}
}
}).start();
} else {
new Thread(new Runnable() {
@Override
public void run() {
try {
runs = true;
if (noColor < 2) {
noColor++;
changeBtnForegroung(clr[noColor]);
} else {
noColor = 0;
changeBtnForegroung(clr[noColor]);
}
changeBtnMargin(new Insets(initMargin.top, initMargin.left + 10, initMargin.bottom, initMargin.right - 10));
border = new EmptyBorder(0, 5, 10, 5);
changePnlBorder(border);
Thread.sleep(100);
changeBtnMargin(new Insets(initMargin.top, initMargin.left - 10, initMargin.bottom, initMargin.right + 10));
border = new EmptyBorder(0, 0, 10, 10);
changePnlBorder(border);
Thread.sleep(100);
changeBtnMargin(new Insets(initMargin.top, initMargin.left + 10, initMargin.bottom, initMargin.right - 10));
border = new EmptyBorder(5, 10, 5, 0);
changePnlBorder(border);
Thread.sleep(100);
changeBtnMargin(new Insets(initMargin.top, initMargin.left - 10, initMargin.bottom, initMargin.right + 10));
border = new EmptyBorder(10, 10, 0, 0);
changePnlBorder(border);
Thread.sleep(100);
changeBtnMargin(new Insets(initMargin.top, initMargin.left, initMargin.bottom, initMargin.right));
border = new EmptyBorder(5, 5, 5, 5);
changePnlBorder(border);
Thread.sleep(100);
count++;
} catch (Exception e) {
System.out.println(e);
}
}
}).start();
}
}
}
private void changePnlBorder(final Border b) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
buttonPanel.setBorder(b);
buttonPanel.revalidate();
buttonPanel.repaint();
}
});
}
private void changeBtnForegroung(final Color c) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
button.setForeground(c);
}
});
}
private void changeBtnMargin(final Insets margin) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
button.setMargin(margin);
}
});
}
}
编辑第三。
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class DelayedComboBoxTest extends JFrame {
private static final long serialVersionUID = 1L;
private JCheckBox chkA = new JCheckBox("A");
private JCheckBox chkB = new JCheckBox("B");
private JCheckBox chkC = new JCheckBox("C");
private JComboBox cboItems = new JComboBox();
public DelayedComboBoxTest() {
super("Delayed ComboBox Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(chkA);
p.add(chkB);
p.add(chkC);
p.add(cboItems);
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(p);
pack();
cboItems.addPopupMenuListener(new MyPopupMenuListener());
}
private void rebuildList() {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Vector<String> items = new Vector<String>();
if (chkA.isSelected()) {
items.add("A");
}
if (chkB.isSelected()) {
items.add("B");
}
if (chkC.isSelected()) {
items.add("C");
}
cboItems.setModel(new DefaultComboBoxModel(items));
try {
new Thread().sleep(2500); // simulate a long transaction
} catch (InterruptedException ex) {
}
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public static void main(String[] args) {
JFrame f = new DelayedComboBoxTest();
f.setVisible(true);
}
private class MyPopupMenuListener implements PopupMenuListener {
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
int items = cboItems.getItemCount();
rebuildList();
if (items != cboItems.getItemCount()) {
cboItems.hidePopup();
cboItems.showPopup();
}
}
}
}
但对于所有示例,必须存在 EDT 并且 EventQueue
中有 Events
,否则简单不起作用
关于java - 在 Swing Java 中向 JList 添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7442310/
我有一个 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 就会失去焦点,选择消失,导致按钮无法确定选择
我是一名优秀的程序员,十分优秀!