gpt4 book ai didi

JAVA GUI按钮问题

转载 作者:行者123 更新时间:2023-12-02 00:39:08 26 4
gpt4 key购买 nike

所以我有一些如下所示的代码:

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)

所以我认为错误来 self 的主要部分,我在其中调用了 EventQueue 。当我拿出来时...

 EventQueue.invokeLater(new Runnable() 
{
public void run()
{

...除了 try catch 语句之外什么都不留下,一切似乎都工作正常,并且按钮被禁用。但是,我只是想知道是否有必要将 EventQueue 语句保留在那里,它的重要性是什么?

也是一个相关问题。我试图对大量文件夹(~100)运行此命令,按钮 1-3 工作正常,但在按钮 4 中,它到达第 18 个文件夹,GUI 就卡住了。我想知道是否有办法解决这个问题来防止卡住?

预先感谢您的帮助!

最佳答案

您的 IPGUI 构造函数中有一个本地 btnConvertDocuments,它屏蔽了类 btnConvertDocuments 对象。

刚刚

    btnConvertDocuments = new JButton("1. Convert Documents");
btnConvertDocuments.setAlignmentX(Component.CENTER_ALIGNMENT);
btnConvertDocuments.setMaximumSize(new Dimension(160, 0));
btnConvertDocuments.setPreferredSize(new Dimension(0, 50));

等等。对于其他按钮

关于JAVA GUI按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6860587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com