我正在制作聊天客户端,但我遇到了一些代码未运行的问题。
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“)”接缝不起作用。但我得到了所有的System.out.println消息。
“list2”是一个JTextArea,它已经添加了我正在使用的JFrame: 继承我gui课程中的代码:
window.add(list2);
list2.setBounds(0,0,window.getWidth(),window.getHeight());
list2.setVisible(false);
有关进一步调查的所有代码:http://pastebin.com/PcSPzgBN
答案 0 :(得分:1)
您的源代码足够长,很难确切地指出您所引用的问题的确切位置。我想这可能就是这一行:
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);
在设置内容窗格后,list2
可能不再出现在框架中。它可能在之前的内容窗格(gui.firstscreen
?)上。
然而,还有一些更普遍的问题。
我看到一个看起来像这样的模式:
SomeJComponent aComp1 = new SomeJComponent();
somewhere.add(aComp1);
aComp1.setBounds(/* ... */);
aComp1.setVisible(false);
SomeJComponent aComp2 = new SomeJComponent();
somewhere.add(aComp2);
aComp2.setBounds(/* ... */);
aComp2.setVisible(false);
基本上看起来你这样做是为了动态地动态切换帧上的可见组件。您的组件都是重叠的,您可以选择哪些组件可见以重新配置框架。我强烈建议不要这样做。
&#34;正确&#34;交换容器视图的方法是使用单独的面板并设置内容窗格(甚至更好地使用用于此目的的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这样的东西来获得最佳效果。
另一点是你需要在Event Dispatch Thread上与GUI进行交互。有关该教程,请参阅Concurrency in Swing。特别是&#34;初始线程&#34; 和&#34;事件派遣线程&#34; 。
这就是invokeLater
的用途。可以更新事件内的GUI(actionPerformed
之类的方法),因为它们是在EDT上执行的。您应该使用其他任何地方invokeLater
。该规则有一些例外,但大多数情况下你需要在EDT上做Swing。
最后一个更像是旁白你应该真正关注Java code conventions的名字,特别是以一个大写字母开头的类。您的start
和gui
课程应为Start
和GUI
。它进一步令人困惑,因为你的gui类有一个名为start
的方法。
答案 1 :(得分:0)
在将组件设置为可见之后,尝试重新绘制并重新验证gui容器(窗口)或JTextArea组件。