大家好,朋友们,我在java swing中创建了一个网吧管理软件,我想从服务器上退出客户端机器上的java软件( swing中的欢迎窗口)。我有这个代码,但它不是working.when客户端运行的程序,swing窗口是不可见的。我可以从服务器关闭它,但我想要的是,当客户端编译和运行代码时,swing窗口应该是可见的,当我从服务器启动close命令时,swing窗口应该是可见的吗
import java.net.*;
import java.io.*;
public class cl extends javax.swing.JFrame {
/** Creates new form cl */
public cl() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 204, 204));
jPanel1.setBackground(new java.awt.Color(255, 204, 204));
jLabel5.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\new-1.jpg")); // NOI18N
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("Welcome to our cafe");
jLabel2.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
jLabel2.setText("Contact Administrator to start your session");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5)
.addGroup(jPanel1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(92, 92, 92)
.addComponent(jLabel1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(42, 42, 42)
.addComponent(jLabel2)))
.addContainerGap(872, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jLabel2)))
.addContainerGap(597, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try
{
String s1,s2;
Socket s=new Socket("192.168.1.2",1024);
DataInputStream dis=new DataInputStream(s.getInputStream());
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
s1=dis.readUTF();
if (s1.equals("5"))
{
System.exit(0);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
// End of variables declaration}
发布于 2012-01-12 14:36:47
问题是您从来没有在main()方法中创建过框架。
将此代码添加到您的main方法:
public static void main(String args[]) {
cl frame = new cl();
cl.setSize(640,480);
cl.setVisible(true);
// rest of code follows...然后这个框架就会出现。
您可能还希望调查是否希望用户能够关闭框架和JFrame.setDefaultCloseOperation(int)
我还建议您考虑使用java RMI,而不是您自己的协议。RMI可能会给你更多的功能/特性,而你不需要手动编写网络代码。RMI还意味着您不需要在单独的线程中运行侦听: RMI将为您处理这一点。
发布于 2012-01-12 14:28:26
可能不是你的问题的答案,请
1)不要使用NetBeans生成的代码,使用Standard Swing JComponents
2)寻找正确的LayoutManager,因为GroupLayout生成的大量代码很难管理
3)将图形用户界面重定向到后台任务,使用SwingWorker,Runnable#Thread,否则Socket会冻结,直到长时间结束
https://stackoverflow.com/questions/8830876
复制相似问题