编辑:我推迟了“Core.createBot();”的执行使用ScheduledWorker解决问题。
所以我使用Java创建了一个IRC Bot,现在我正在通过GUI重写它以便可控制。如果用户通过该命令发出命令,则bot会向通道发送一条消息,说明谁发出了命令。要知道它是谁,我要求用户在应用程序启动后立即设置用户名。单击按钮时,当前GUI关闭,机器人启动,实际的GUI弹出。然而,它只显示一个白色的屏幕,没有别的。没有JLabel,没有JTextFields,没有。我究竟做错了什么?
这是我的主要课程
package bl4ckscor3.bot.bl4ckb0tGUI.core;
import java.awt.Font;
import java.io.IOException;
import javax.swing.JFrame;
import org.pircbotx.Configuration;
import org.pircbotx.PircBotX;
import org.pircbotx.exception.IrcException;
import bl4ckscor3.bot.bl4ckb0tGUI.gui.Gui;
import bl4ckscor3.bot.bl4ckb0tGUI.gui.NameGui;
public class Core
{
public static PircBotX bot;
public static Gui gui;
public static String name;
public static NameGui nameGui;
public static void main(String args[]) throws IOException, IrcException
{
setupNameGui();
}
private static void setupNameGui()
{
nameGui = new NameGui();
nameGui.setTitle("Username selection");
nameGui.setFont(new Font("Arial", 0, 14));
nameGui.setSize(300, 150);
nameGui.setLocationRelativeTo(null);
nameGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nameGui.setResizable(false);
nameGui.setVisible(true);
}
public static void setupGui()
{
gui = new Gui();
gui.setTitle("bl4ckb0t");
gui.setFont(new Font("Arial", 0, 14));
gui.setSize(800, 800);
gui.setLocationRelativeTo(null);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setResizable(false);
gui.setVisible(true);
}
public static void createBot()
{
Configuration config = new Configuration.Builder()
.setName("bl4ckb0t")
.setVersion("1.0")
.setServerHostname("irc.esper.net")
.setServerPort(6667)
.setNickservPassword("xxx")
.setLogin("bl4ckb0t")
.setAutoNickChange(true)
.addListener(new BotListener())
.setMessageDelay(500)
.buildConfiguration();
bot = new PircBotX(config);
try
{
bot.startBot();
}
catch(IOException e){}
catch(IrcException e){}
}
}
这就是问题似乎出现的类:
package bl4ckscor3.bot.bl4ckb0tGUI.gui;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import bl4ckscor3.bot.bl4ckb0tGUI.core.Core;
public class NameGui extends JFrame
{
private JLabel label = new JLabel();
private JTextField text = new JTextField();
private JButton buttonStart = new JButton();
private JButton buttonStop = new JButton();
private String textText = "Please insert your username below.";
private Container cp = getContentPane();
public NameGui()
{
cp.setLayout(null);
label.setBounds(40, 10, textText.length() * 6, 20);
label.setText(textText);
text.setBounds(42, 40, 200, 20);
buttonStart.setText("Let's start!");
buttonStart.setBounds(10, 80, 100, 20);
buttonStart.addActionListener(new ButtonListener());
buttonStop.setText("I changed my mind.");
buttonStop.setBounds(130, 80, 145 , 20);
buttonStop.addActionListener(new ButtonListener());
cp.add(label);
cp.add(text);
cp.add(buttonStart);
cp.add(buttonStop);
}
private class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
switch(event.getActionCommand())
{
case "Let's start!":
if(Core.name != null)
Core.name = text.getText();
else
Core.name = "Anonymous";
Core.nameGui.dispose();
Core.setupGui();
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
Core.createBot();
}
});
break;
case "I changed my mind.":
System.exit(0);
break;
default:
System.out.println("Something went wrong: " + event.getActionCommand());
}
}
}
}
答案 0 :(得分:2)
Core.createBot();
上述代码在Event Dispatch Thread(EDT)上执行。我不确切知道Bot的作用,但是我猜它在那里等待输入,这意味着它阻止了EDT并阻止了GUI重新绘制。
我猜想" bot"需要在单独的线程上运行,因此它不会阻止EDT。有关详细信息,请阅读Concurrency上Swing教程中的部分。