相关疑难解决方法(0)

如果线程启动执行程序,则无法从Future <?>和SwingWorker获取ArrayIndexOutOfBoundsException

我通过使用Executor玩SwingWorker的多线程,我在那里错误地从Vector中识别出错误的元素,看起来像这个代码相当忽略了Vector中的元素不存在

我的问题 - >如何/有可能以某种方式捕获此异常

简单的输出

run:
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
Thread Status with Name :StartShedule, SwingWorker Status is STARTED
Thread Status with Name :StartShedule, SwingWorker Status is DONE
BUILD SUCCESSFUL (total time: 11 seconds)
Run Code Online (Sandbox Code Playgroud)

通过取消注释

//changeTableValues1(); // un-comment for get ArrayIndexOutOfBoundsException
Run Code Online (Sandbox Code Playgroud)

一切都正确,我得到ArrayIndexOutOfBoundsException并输出

run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: …
Run Code Online (Sandbox Code Playgroud)

java swing exception-handling vector swingworker

20
推荐指数
2
解决办法
8356
查看次数

Swing - 更新标签

我有一个消息标签和一个提交按钮.将多次按下提交按钮,每次按下的操作最多可能需要一分钟.

按下按钮时,我想将消息设置为空,在任务完成后,我想将消息设置为"完成".

private void submitActionPerformed(java.awt.event.ActionEvent evt) {
   message = "";
   updateMessageLabel();

   doTheTask();

   /* this update is apply to the label after completion */
   message = "Complete";
}
Run Code Online (Sandbox Code Playgroud)

是否可以在submitActionPerformed()方法运行之前(或在方法中)更新该消息标签,但是在单击按钮之后?

java concurrency swing event-dispatch-thread

8
推荐指数
2
解决办法
1万
查看次数

简单的Java GUI作为弹出窗口和下拉菜单

我从来没有在java中编写GUI.这次我也可以跳过它并args用作UI(用户界面).但我想知道是否有一种简单的方法可以创建一个小的GUI来让用户选择其中一个选项.换句话说,要实现askUser()用户可以从下拉菜单中选择并按"确定"的功能.我花了一些时间学习这个主题,但甚至不确定我知道这个任务需要哪种类型的GUI.JFrame的?JPanel的?JMenu的?谢谢.

这是所需功能的一个例子.

package trygui;

public class Main {

    public static void main(String[] args) {
        String[] choices = new String[]{"cats", "dogs"};
        int choice = askUser(choices);
        System.out.println("selected: " + choices[choice]);
    }

    static int askUser(String[] choices) {
        // create pop-up dialog
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:我使用Netbeans,如果这可以有所作为.

java swing popup joptionpane jcombobox

5
推荐指数
1
解决办法
3万
查看次数

JPanel 添加但未“及时”显示

我有一个 JFrame,它根据您单击的 MenuItem 显示 JPanel。它可以工作,但是现在我需要在将 JPanel 添加到框架并显示它后调用一个方法(因为我在该面板内使用 JFreeChart 并且我必须chartPanel.repaint()在 JPanel 可见时调用):

this.getContentPane().add( myjpanel, BorderLayout.CENTER ); //this = JFrame
this.validate();
myjpanel.methodCalledOnceDisplayed();
Run Code Online (Sandbox Code Playgroud)

看起来没问题吗?真的myjpanel被展示了吗?好像不是:

public void methodCalledOnceDisplayed() {
    chartPanel.repaint()
}
Run Code Online (Sandbox Code Playgroud)

这不起作用(chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0)正在抛出 IndexOutOfBoundsException)。这意味着调用重绘时 JPanel 不可见,我测试了以下内容:

public void methodCalledOnceDisplayed() {
    JOptionPane.showMessageDialog(null,"You should see myjpanel now");
    chartPanel.repaint()
}
Run Code Online (Sandbox Code Playgroud)

现在它起作用了,我myjpanel在警报后面看到了,正如预期的那样,重新绘制了 chartPanel 并且没有发生异常。

编辑:SSCCE(需要 jfreechart 和 jcommon:http ://www.jfree.org/jfreechart/download.html )

导入 java.awt.BorderLayout;
导入 java.awt.EventQueue;
导入 java.awt.Font;
导入 javax.swing.JButton;
导入 javax.swing.JFrame;
导入 javax.swing.JLabel;
导入 javax.swing.JOptionPane;
导入 javax.swing.JPanel;
导入 javax.swing.border.EmptyBorder;
导入 …

java concurrency swing jfreechart event-dispatch-thread

4
推荐指数
1
解决办法
3136
查看次数

在循环中处理其他GUI事件

我正在编写一个Java 7 Swing应用程序,该应用程序调用非GUI类来在循环中执行某些操作.不幸的是,在循环运行时我无法与GUI交互.是否可以在该循环中强制处理GUI事件?

我正在寻找一些东西,告诉JVM在继续循环之前处理其他GUI事件,如按钮点击.

Java 7 Swing中是否存在我想做或做的事情,我自己必须弄乱多线程(我还没那么远......)?

java swing

1
推荐指数
2
解决办法
916
查看次数

JOptionPane.showMessageDialog没有显示文本

使用JOptionPane.showMessageDialog()方法时,我似乎遇到了一些问题.当我使用该方法时,唯一正确设置的是对话框标题.它不想显示我提供的文本.

这是我用来尝试创建警报的代码:

 JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
Run Code Online (Sandbox Code Playgroud)

上面的代码提供了以下图片:

输出我得到

如果有人能告诉我我做错了什么,或者我应该使用不同的方法,我会非常感激.

编辑:

我的主要类:这创建了一个GUI,用户输入信息"Host"和"DisplayName".当他们单击"连接"时,会创建一个新线程(ClientConnectSocket).

public class Main extends JFrame {

public static JPanel contentPane;
private JTextField hostTxt;
public static JTextField displayNameTxt;

JLabel lblDisplayName = new JLabel("Display Name:");
JButton btnConnect = new JButton("Connect");
JLabel lblHost = new JLabel("Host:");


public static String username = "None :(";
public static String host = "localhost";

public static boolean connected = false;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() …
Run Code Online (Sandbox Code Playgroud)

java swing dialog jpanel

1
推荐指数
1
解决办法
2961
查看次数