したがって、次のようなコードがあります。
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class IPGUI extends JFrame implements ActionListener
{
private static JPanel contentPane;
private JButton btnConvertDocuments;
private JButton btnExtractImages;
private JButton btnParseRIDValues;
private JButton btnParseImageInfo;
//Create the frame
public IPGUI()
{
//Sets frame properties
setTitle("IP Extractor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//Creates new JPanel with boxlayout
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
//////////////////New Button//////////////////
JButton btnConvertDocuments = new JButton("1. Convert Documents");
btnConvertDocuments.setAlignmentX(Component.CENTER_ALIGNMENT);
btnConvertDocuments.setMaximumSize(new Dimension(160, 0));
btnConvertDocuments.setPreferredSize(new Dimension(0, 50));
panel.add(btnConvertDocuments);
btnConvertDocuments.setActionCommand("w");
btnConvertDocuments.addActionListener((ActionListener) this);
JSeparator separator_3 = new JSeparator();
panel.add(separator_3);
//////////////////New Button//////////////////
btnExtractImages = new JButton("2. Extract Images");
btnExtractImages.setAlignmentX(Component.CENTER_ALIGNMENT);
btnExtractImages.setMaximumSize(new Dimension(160, 0));
btnExtractImages.setPreferredSize(new Dimension(0, 50));
panel.add(btnExtractImages);
btnExtractImages.setActionCommand("x");
btnExtractImages.addActionListener((ActionListener) this);
JSeparator separator_2 = new JSeparator();
panel.add(separator_2);
//////////////////New Button//////////////////
JButton btnParseRIDValues = new JButton("3. Parse rId Values");
btnParseRIDValues.setAlignmentX(Component.CENTER_ALIGNMENT);
btnParseRIDValues.setMaximumSize(new Dimension(160, 0));
btnParseRIDValues.setPreferredSize(new Dimension(0, 50));
panel.add(btnParseRIDValues);
btnParseRIDValues.setActionCommand("y");
btnParseRIDValues.addActionListener((ActionListener) this);
JSeparator separator_1 = new JSeparator();
panel.add(separator_1);
//////////////////New Button//////////////////
JButton btnParseImageInfo = new JButton("4. Parse Image Info.");
btnParseImageInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
btnParseImageInfo.setMaximumSize(new Dimension(160, 0));
btnParseImageInfo.setPreferredSize(new Dimension(0, 50));
panel.add(btnParseImageInfo);
btnParseImageInfo.setActionCommand("z");
btnParseImageInfo.addActionListener((ActionListener) this);
}
//Actions performed when an event occurs.
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
//If btnConvertDocuments is clicked, the FileConverter method is called and the button is then disabled [so as to prevent duplicates].
if (command.equals("w"))
{
FileConverter fc = new FileConverter();
btnConvertDocuments.setEnabled(false);
}
//If btnExtractImages is clicked, the ImageExtractor method is called and the button is then disabled [so as to prevent duplicates].
else if (command.equals("x"))
{
ImageExtractor ie = new ImageExtractor();
btnExtractImages.setEnabled(false);
}
//If btnParseRIDValues is clicked, the XMLIDParser method is called and the button is then disabled [so as to prevent duplicates].
else if (command.equals("y"))
{
XMLIDParser xip = new XMLIDParser();
btnParseRIDValues.setEnabled(false);
}
//If btnParseImageInfo is clicked, the XMLTagParser method is called and the button is then disabled [so as to prevent duplicates].
else if (command.equals("z"))
{
XMLTagParser xtp = new XMLTagParser();
btnParseImageInfo.setEnabled(false);
}
}
}
主要
import java.awt.EventQueue;
public class IPDriver
{
public static void main(String[] args)
{
//Calls GUI and sets its visibility to true
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
IPGUI frame = new IPGUI();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
エラーが発生しました
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at IPGUI.actionPerformed(IPGUI.java:119)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
SO私は、EventQueueが呼び出されているメインからエラーが発生していると考えています。そしてテイクアウトすると・・・
EventQueue.invokeLater(new Runnable()
{
public void run()
{
...そして、try catch ステートメントだけを残すと、すべてが正常に機能しているように見え、ボタンが無効になります。しかし、そこに EventQueue ステートメントを残す必要があるかどうか疑問に思っていました。その重要性は何ですか?
また、関連する質問です。これを大量のフォルダー (~100) に対して実行しようとしていますが、ボタン 1 ~ 3 は正常に動作しますが、ボタン 4 では 18 番目のフォルダーに到達し、GUI がフリーズします。凍結を防ぐためにこれを回避する方法があるかどうか疑問に思っていましたか?
助けてくれてありがとう!