Java 有些代码行无法运行
我正在制作一个聊天客户端,但是代码没有运行时出现了一些问题Java 有些代码行无法运行,java,swing,visible,Java,Swing,Visible,我正在制作一个聊天客户端,但是代码没有运行时出现了一些问题 public static void login(String userName, String password) throws XMPPException { ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222,"Work"); connection = new XMPPConnection(co
public static void login(String userName, String password) throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222,"Work");
connection = new XMPPConnection(config);
try{
connection.connect();
connection.login(userName, password);
System.out.println("Login Successful");
//gui.removeAll();
URL temp = start.class.getResource("slate.png");
gui.window.remove(gui.password);
gui.window.remove(gui.username);
gui.window.remove(gui.login);
gui.window.remove(gui.failed);
gui.window.setContentPane(new JLabel(new ImageIcon(temp)));
gui.window.setBackground(new Color(27,27,27));
System.out.println("Reached 1");
//displayBuddyList();
gui.list2.setVisible(true);
System.out.println("Reached 2");
gui.list2.setText("text test");
System.out.println("Reached 3");
}
catch(Exception e){
gui.failed.setVisible(true);
}
}
行“gui.list2.setVisible(true)”和“gui.list2.setText(“TestText”)”seam可能不起作用。但是我收到了所有的System.out.println信息
“list2”是一个JTextArea,其地址已添加到我使用的JFrame中:
下面是我的gui类中的代码:
window.add(list2);
list2.setBounds(0,0,window.getWidth(),window.getHeight());
list2.setVisible(false);
对于所有需要进一步研究的代码:在将gui容器(窗口)或JTextArea组件设置为可见后,尝试重新绘制和(重新)验证该组件。您的源代码足够长,很难准确指出您所引用的问题所在。我想可能正是这句话:
gui.window.setContentPane(new JLabel(new ImageIcon(temp))); // <- this line
gui.window.setBackground(new Color(27,27,27));
...
gui.list2.setText("text test");
...
gui.list2.setVisible(true);
基本上,看起来您这样做是为了有时动态地切换出帧上的可见组件。您的组件都是重叠的,您可以选择哪些组件可见以重新配置框架。我强烈建议不要这样做
交换容器视图的“正确”方法是使用单独的面板并设置内容窗格(或者更好地使用用于此目的的JComponents,例如JTabbedPane)。以下是一个简短的工作示例:
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
public class PanelSwap {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new PanelSwap().frame.setVisible(true);
}
});
}
JFrame frame;
JPanel panel1;
JPanel panel2;
PanelSwap() {
frame = new JFrame();
frame.setLocationRelativeTo(null);
panel1 = new JPanel(new BorderLayout());
JButton toPanel2 = new JButton("Goto Panel 2");
toPanel2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setContentPane(panel2);
frame.validate();
}
});
panel1.add(toPanel2, BorderLayout.CENTER);
panel2 = new JPanel(new BorderLayout());
JButton toPanel1 = new JButton("Goto Panel 1");
toPanel1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setContentPane(panel1);
frame.validate();
}
});
panel2.add(toPanel1, BorderLayout.CENTER);
frame.setContentPane(panel1);
frame.pack();
}
}
如果希望共享某些GUI元素,可以将内容窗格设置为包含共享元素的面板,并在该面板中添加/删除元素。使用类似于BorderLayout的方法可以获得最佳效果
另一点是,您需要在事件调度线程上与GUI交互。有关教程,请参见。特别是“初始线程”和“事件调度线程”
这就是调用器的作用。可以在事件(如actionPerformed
)内更新GUI,因为它们是在EDT上执行的。您应该在任何其他地方使用invokeLater
。这条规则也有一些例外,但在大多数情况下,你需要在EDT上做Swing练习
最后一个更像旁白的是,你应该真正遵循名称,特别是以大写字母开头的类。您的
start
和gui
类应该是start
和gui
。因为您的gui类有一个名为start
的方法,所以它更令人困惑。所有这些都是在EDT中发生的吗?如果是,是否有其他东西干扰EDT?Put gui.list2.setVisible(true);在setText调用之后。@Quilliom这不起作用。和@John3136什么是EDT?EDT是事件调度线程。如果你搞砸了,Java就没有机会为你画东西了。比如window.repaint()
我在JFrame和JTextArea上试过这个方法谢谢!!我简直不敢相信添加gui.window.add(gui.list2)
在解决了这个问题后,我会尽快集中精力重写代码。如果我遇到问题或什么,我可以给你留言吗?因为我认为你有足够的代表使用聊天,你可以试着打招呼我那里。用@(比如@darklama)键入用户名也会从聊天室发送提及通知。如果我不在,你可以问这个问题。有时房间里还有其他人,或者我上车时可能会看到。
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
public class PanelSwap {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new PanelSwap().frame.setVisible(true);
}
});
}
JFrame frame;
JPanel panel1;
JPanel panel2;
PanelSwap() {
frame = new JFrame();
frame.setLocationRelativeTo(null);
panel1 = new JPanel(new BorderLayout());
JButton toPanel2 = new JButton("Goto Panel 2");
toPanel2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setContentPane(panel2);
frame.validate();
}
});
panel1.add(toPanel2, BorderLayout.CENTER);
panel2 = new JPanel(new BorderLayout());
JButton toPanel1 = new JButton("Goto Panel 1");
toPanel1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setContentPane(panel1);
frame.validate();
}
});
panel2.add(toPanel1, BorderLayout.CENTER);
frame.setContentPane(panel1);
frame.pack();
}
}