Java JFrame中的每件事都不是';看不见
我制作了一个消息传递程序,客户端有一个GUI。在启动客户端之前,客户端程序需要有关用户和服务器的信息。我使用args数组通过命令行输入信息 但现在我的程序运行起来了,我制作了一个GUI来输入信息。我在另一个类中创建了它,该类调用客户机类并将信息传递给客户机类。InfoGUI工作正常,但当我输入信息并调用客户机类时,会显示框架,但框架中没有可见的组件 当我恢复到旧方法时,客户端GUI工作正常。我不知道我还能做什么 调用客户端类的代码:Java JFrame中的每件事都不是';看不见,java,swing,user-interface,event-dispatch-thread,Java,Swing,User Interface,Event Dispatch Thread,我制作了一个消息传递程序,客户端有一个GUI。在启动客户端之前,客户端程序需要有关用户和服务器的信息。我使用args数组通过命令行输入信息 但现在我的程序运行起来了,我制作了一个GUI来输入信息。我在另一个类中创建了它,该类调用客户机类并将信息传递给客户机类。InfoGUI工作正常,但当我输入信息并调用客户机类时,会显示框架,但框架中没有可见的组件 当我恢复到旧方法时,客户端GUI工作正常。我不知道我还能做什么 调用客户端类的代码: btn.addActionListener(new Actio
btn.addActionListener(new ActionListener() {
...
username = Username.getText();
hostName = sa.getText();
portNr = Integer.parseInt(spnr.getText());
f.dispose();
System.out.println("frame disposed of");
try {
Client client = new Client();
f.dispose();
Client.llc(username, hostName, portNr);
} catch (IOException e) {
e.printStackTrace();
}
}
});
客户端代码:
public static void llc(String iun, String ihn, int ipnr) throws IOException {
username = iun;
hostName = ihn;
portNr = ipnr;
launchClient();
}
public static void launchClient() throws IOException {
try (
//socket setup
Socket socket = new Socket(hostName, portNr);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
System.out.println("opened streams");
//frame setup
JFrame frame = new JFrame("frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//text field
JTextField MSGText = new JTextField(5);
//text area
JTextArea MSGD = new JTextArea(20, 30);
MSGD.setEditable(false);
JScrollPane scrollPane = new JScrollPane(MSGD);
System.out.println("opened streams");
//button
JButton b = new JButton("Send message");
b.setPreferredSize(new Dimension(60, 30));
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendMSG = MSGText.getText();
MSGText.setText("");
out.println("<" + username + ">: " + sendMSG);
}
});
//panel
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.add(Box.createVerticalStrut(5));
p.add(scrollPane);
p.add(Box.createVerticalStrut(5));
p.add(MSGText);
p.add(Box.createVerticalStrut(5));
p.add(b);
p.add(Box.createVerticalStrut(5));
JPanel pMain = new JPanel();
pMain.setLayout(new BoxLayout(pMain, BoxLayout.LINE_AXIS));
pMain.add(Box.createHorizontalStrut(5));
pMain.add(p);
pMain.add(Box.createHorizontalStrut(5));
frame.getContentPane().add(pMain);
//frame visiblity
frame.pack();
frame.setVisible(true);
System.out.println("opened streams");
while((getMSG = in.readLine()) != null) {
MSGD.append(getMSG + "\n");
System.out.println("opened streams");
}
}
}
publicstaticvoidllc(stringiun、stringihn、intipnr)抛出IOException{
用户名=iun;
主机名=ihn;
portNr=ipnr;
启动客户端();
}
public static void launchClient()引发IOException{
试一试(
//套接字设置
套接字=新套接字(主机名、端口号);
PrintWriter out=新的PrintWriter(socket.getOutputStream(),true);
BufferedReader in=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
) {
System.out.println(“开放流”);
//帧设置
JFrame框架=新的JFrame(“框架”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//文本字段
JTextField MSGText=新的JTextField(5);
//文本区
JTextArea MSGD=新的JTextArea(20,30);
MSGD.setEditable(false);
JScrollPane scrollPane=新的JScrollPane(MSGD);
System.out.println(“开放流”);
//钮扣
JButton b=新JButton(“发送消息”);
b、 setPreferredSize(新尺寸(60,30));
b、 addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件){
sendMSG=MSGText.getText();
MSGText.setText(“”);
out.println(“:”+sendMSG);
}
});
//面板
JPanel p=新的JPanel();
p、 setLayout(新的BoxLayout(p,BoxLayout.PAGE_轴));
p、 增加(垂直支柱箱(5));
p、 添加(滚动窗格);
p、 增加(垂直支柱箱(5));
p、 添加(MSGText);
p、 增加(垂直支柱箱(5));
p、 添加(b);
p、 增加(垂直支柱箱(5));
JPanel pMain=新的JPanel();
pMain.setLayout(新的BoxLayout(pMain,BoxLayout.LINE_轴));
pMain.add(Box.createHorizontalstrop(5));
pMain.add(p);
pMain.add(Box.createHorizontalstrop(5));
frame.getContentPane().add(pMain);
//框架可视性
frame.pack();
frame.setVisible(true);
System.out.println(“开放流”);
而((getMSG=in.readLine())!=null){
MSGD.append(getMSG+“\n”);
System.out.println(“开放流”);
}
}
}
如果您的客户机代码是扩展JFrame的客户机类,那么可能是框架在组件之前初始化。要解决这个问题,您只需在客户端类中将JFrame成员设置为public全局变量,然后在调用llc后调用client.frame.repaint()
public class Client extends JFrame {
public JFrame frame;
//everything else the same
}
-----------------------------------
btn.addActionListener(new ActionListener() {
...
username = Username.getText();
hostName = sa.getText();
portNr = Integer.parseInt(spnr.getText());
f.dispose();
System.out.println("frame disposed of");
try {
Client client = new Client();
f.dispose();
Client.llc(username, hostName, portNr);
client.frame.repaint();
} catch (IOException e) {
e.printStackTrace();
}
}
});
“我不知道我还应该做什么。”1)问一个问题。甚至将其重新定义为“我还应该做些什么来纠正这个问题?”就足够了(如果这是你的问题的话)。2) 为了更快地获得更好的帮助,请添加或。。。3) 请学习常见的Java命名法(命名约定-例如EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
,除非它是大写常量
),并一致使用它。(例如,scrollPane
是正确的术语,但MSGText
应以小写字母开头。)。。4) b.setPreferredSize(新尺寸(60,30))代码>首选大小只不过是猜测而已。要使按钮变大,请为其提供更大的字体或插图。