- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 SwingWorker
类来执行后台任务。在后台线程完成工作后,GUI
的许多不同组件都会更新(这些组件在 done()
方法中)。 doInBackground()
方法发布一个 HeatMap
类对象,process()
方法将其添加到 JPanel
组件。我已将 MouseListener
和 MouseMotionListener
添加到此 Heatmap
类对象中。 mouseMoved()
方法存在于主 GUI 类中。当鼠标移动时,鼠标在 HeatMap
上的坐标位置应显示在 JLabel
中。
当我运行代码时,HeatMap
对象在 JPanel
中可见,但我认为 EDT
无法访问它。这是因为,经过快速检查,我发现 rawIntensityMap
HeatMap
对象在 process()
中不是 null
SwingWorker
的方法,但在 mouseMoved()
方法中它仍然是 null
,因此我得到一个 NullPointerException
.
GUIMain
类和 SwingWorker
类中的 HeatMap
对象已声明为:
private HeatMap rawIntensityMap = null;
我不会将 rawIntensityMap
对象发送到 SwingWorker
类构造函数。我之前曾尝试过,但没有成功。
这是 SwingWorker
类中的 process()
方法:
@Override
protected void process(List<HeatMap> chunks) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
imagePanel.add(rawIntensityMap, BorderLayout.CENTER);
coordinates.setBounds(31, 31, rawIntensityMap.getWidth() - 31, rawIntensityMap.getHeight() - 31);
}
});
}
这是mouseMoved()
方法:
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("I am in the mouseevent" + coordinates.toString());
System.out.println("Width of raw Intensity map: " + rawIntensityMap.getWidth());
if (e.getPoint().x >= 31 && e.getPoint().y >= 31 && e.getPoint().x <= rawIntensityMap.getWidth() - 31 && e.getPoint().y <= rawIntensityMap.getHeight() - 31) {
rawIntensityMap.removeAll();
rawIntensityMap.add(coordinates);
coordinates.setText("(x,y) = " + "(" + (e.getPoint().x - 31) + "," + (e.getPoint().y - 31) + ")");
if (peakPickedImage.isSelected()) {
preprocessedIntensityMap.add(coordinates);
coordinates.setText("(x,y) = " + "(" + (e.getPoint().x - 31) + "," + (e.getPoint().y - 31) + ")");
coordinates.revalidate();
coordinates.repaint();
}
coordinates.revalidate();
coordinates.repaint();
}
}
这是我的 SwingWorker
类的基本结构:
public class FileReadWorker extends SwingWorker<REXP, HeatMap> {
public FileReadWorker(GUIMain guiClassObject, File fileName, JTree rawSpectraTree, DefaultTreeModel model, DefaultMutableTreeNode root, String currentPath, JTextField minMz, JTextField maxMz, JFreeChart spectrumPlot, ChartPanel chartPanel, JPanel chartContent, float minMzValue, float maxMzValue, Float globalMinMz, Float globalMaxMz, JLabel statusLabel, JPanel imagePanel, JLabel coordinates, JTabbedPane tabbedSpectralFiles, JScrollPane spectralFilesScrollPane, JPanel rawFilesPanel, JRadioButton rawImage, JRadioButton peakPickedImage, JMenuItem loadPeakListMenuItem, JButton loadPeaklistsButton, JMenuItem propertiesMenuItem, JButton propertiesButton) {
this.guiClassObject = guiClassObject;
this.fileName = fileName;
this.rawSpectraTree = rawSpectraTree;
this.currentPath = currentPath;
this.minMz = minMz;
this.maxMz = maxMz;
this.spectrumPlot = spectrumPlot;
this.chartPanel = chartPanel;
this.chartContent = chartContent;
this.minMzValue = minMzValue;
this.maxMzValue = maxMzValue;
this.GlobalMinMz = globalMinMz;
this.GlobalMaxMz = globalMaxMz;
this.statusLabel = statusLabel;
this.imagePanel = imagePanel;
this.coordinates = coordinates;
this.tabbedSpectralFiles = tabbedSpectralFiles;
this.spectralFilesScrollPane = spectralFilesScrollPane;
this.rawFilesPanel = rawFilesPanel;
this.rawImage = rawImage;
this.peakPickedImage = peakPickedImage;
this.loadPeakListMenuItem = loadPeakListMenuItem;
this.loadPeaklistsButton = loadPeaklistsButton;
this.propertiesMenuItem = propertiesMenuItem;
this.propertiesButton = propertiesButton;
this.model = model;
this.root = root;
}
@Override
protected REXP doInBackground() throws Exception {
// does some background tasks
// Works on the generating the HeatMap
try {
rawIntensityMap = gim.generateIntensityMap(rawSpectrumObjects, currentPath, minMzValue, maxMzValue, Gradient.GRADIENT_Rainbow, "RAW");
publish(rawIntensityMap);
} catch (RserveException e) {
e.printStackTrace();
} catch (REXPMismatchException e) {
e.printStackTrace();
}
// returns a REXP object
return rawSpectrumObjects;
}
@Override
public void done() {
// Updates different components of the GUI
rawIntensityMap.addMouseListener(guiClassObject);
rawIntensityMap.addMouseMotionListener(guiClassObject);
}
}
}
有人可以指出这里的错误吗?
更多代码:
这是我的 GUIMain
类,其中声明了 HeatMap rawIntensityMap
。另外,我还粘贴了实际使用此 HeatMap
对象的部分代码(未粘贴所有方法)。
public class GUIMain extends JFrame implements ActionListener, ItemListener, MouseListener, MouseMotionListener, ChangeListener {
volatile HeatMap rawIntensityMap;
private JPanel imagePanel; // container for the HeatMap
/**
* Constructor to setup the GUI
*/
public GUIMain(String title) {
super(title);
setLayout(new BorderLayout());
//getSize();
setSize(getSize());
imagePanel = new JPanel(new BorderLayout());
g.gridx = 0;
g.gridy = 1;
g.gridwidth = 2;
g.weightx = 1.0; // fill the rest of the space
g.weighty = 1.0;
g.fill = GridBagConstraints.BOTH;
imagePanel.setBorder(BorderFactory.createEtchedBorder());
//imagePanel.addMouseListener(this);
imageDisplay.add(imagePanel, g);
// ImageDisplay.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
imageDisplay.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("View 2-D ion intensity map"),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
}
public void actionPerformed(ActionEvent e) {
//Handle open *.img imaging file button and menu item action
if ((e.getSource() == OpenImagingFileButton) || (e.getSource() == loadRawSpectraMenuItem)) {
int returnVal = fcImg.showOpenDialog(GUIMain.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fcImg.getSelectedFile();
root = new DefaultMutableTreeNode(file);
rawSpectraTree = new JTree(root);
model = (DefaultTreeModel) rawSpectraTree.getModel();
//Passing this HeatMap to the SwingWorker class
FileReadWorker frw = new FileReadWorker(this, file, rawSpectraTree, rawIntensityMap, model, root, currentPath, minMz, maxMz, spectrumPlot, chartPanel, chartContent, minMzValue, maxMzValue, GlobalMinMz, GlobalMaxMz, statusLabel, imagePanel, coordinates, tabbedSpectralFiles, spectralFilesScrollPane, rawFilesPanel, rawImage, peakPickedImage, loadPeakListMenuItem, loadPeaklistsButton, propertiesMenuItem, propertiesButton);
frw.execute();
}
// Method when a different HeatMap color gradient is selected
@Override
public void itemStateChanged(ItemEvent e) {
colorNumber = (Integer) e.getItem();
if (e.getStateChange() == ItemEvent.SELECTED) {
rawIntensityMap.updateGradient(gradients[colorNumber]);
if (peakPickedImage.isEnabled()) {
preprocessedIntensityMap.updateGradient(gradients[colorNumber]);
}
}
}
// Mouse moved event
@Override
public void mouseMoved(MouseEvent e) {
if(rawIntensityMap == null)
System.out.println("TRUE**");
else
System.out.println("FALSE**");
System.out.println("I am in the mouseevent" + coordinates.toString());
if (e.getPoint().x >= 31 && e.getPoint().y >= 31 && e.getPoint().x <= rawIntensityMap.getWidth() - 31 && e.getPoint().y <= rawIntensityMap.getHeight() - 31) {
rawIntensityMap.removeAll();
rawIntensityMap.add(coordinates);
coordinates.setText("(x,y) = " + "(" + (e.getPoint().x - 31) + "," + (e.getPoint().y - 31) + ")");
if (peakPickedImage.isSelected()) {
preprocessedIntensityMap.add(coordinates);
coordinates.setText("(x,y) = " + "(" + (e.getPoint().x - 31) + "," + (e.getPoint().y - 31) + ")");
coordinates.revalidate();
coordinates.repaint();
}
coordinates.revalidate();
coordinates.repaint();
}
}
}
SwingWorker
类中的代码已粘贴在上面。
最佳答案
您有两个完全独立的字段,名为 rawIntensityMap
。
您有GUIMain.rawIntensityMap
和FileReadWorker.rawIntensityMap
。您在 FileReadWorker::doInBackground
中分配 FileReadWorker.rawIntensityMap
,但从未将 GUIMain.rawIntensityMap
分配给任何值。
尝试在 GUIMain
中为 rawIntensityMap
创建 setter,并在 FileReadWorker::done< 中调用
.guiClassObject.setRawIntensityMap(rawIntensityMap);
/
关于java - 为什么 SwingWorker 不向 EDT 返回对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37054780/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!