有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing使jsf页面使用java类

我在大学里做一个项目,在一个社交媒体网站上,我遇到了一个问题。我有我的jsf页面,我还创建了一个java类(更确切地说是在web上获得的),用于在它们之间聊天。用户应该正在查看朋友的个人资料,其中有一个他/她可以按下的按钮,并显示一个新的JFrame用于聊天

然而,聊天室。java已经有了一个main。我稍微修改了代码,更改了主功能。看起来是这样的:

@ManagedBean(name="chatbox")
@SessionScoped

public class ChatBox extends JFrame implements ActionListener { 
    String name; 
    InetAddress iadr; 
    int port; 
    MulticastSocket so; 
    JTextArea txt = new JTextArea(); 
    JScrollPane sp = new JScrollPane(txt); 
    JTextField write = new JTextField(); 
    JButton quit = new JButton("Go Offline"); 

    public ChatBox(String username, String groupAdr, int portNr) throws IOException { 
        name = username; 
        iadr = InetAddress.getByName(groupAdr); 
        port = portNr; so = new MulticastSocket(port); 
        so.joinGroup(iadr); 
        new Receiver(so,txt); 

        setTitle("Chatting with "+ name); 
        txt.setEditable(true); 
        add(quit,BorderLayout.NORTH); 
        add(sp,BorderLayout.CENTER); 
        add(write,BorderLayout.SOUTH); 
        quit.addActionListener(this); 
        write.addActionListener(this); 
        setSize(400,250); 
        setVisible(true); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
    }

    public void sendMess(String s) { 
        byte[] data = (name + ": " + s).getBytes(); 
        DatagramPacket packet = new DatagramPacket(data,data.length,iadr,port); 
        try { 
            so.send(packet); 
        } catch(IOException ie) { 
            Toolkit.getDefaultToolkit().beep(); 
            JOptionPane.showMessageDialog(null, "Data overflow !"); 
        } 
    }

    public void actionPerformed(ActionEvent e) { 
        if (e.getSource() == write) { 
            sendMess(write.getText()); 
            write.setText(""); 
        } 
        else if (e.getSource() == quit) { 
            sendMess("Offline"); 
            try { 
                so.leaveGroup(iadr); 
            } catch(IOException ie) { 
                Toolkit.getDefaultToolkit().beep(); 
                JOptionPane.showMessageDialog(null, "Data overflow, connection error !");
            } 
            so.close(); dispose(); System.exit(0); 
        } 
    }

    public void mmm()throws IOException { 
        String in = JOptionPane.showInputDialog(null,"What's your name?"); 
       //  if(arg.length>0) in = arg[0];
        new ChatBox(in, "224.0.0.7",3); 
    }

    class Receiver implements Runnable { 
        Thread activity = new Thread(this);
        MulticastSocket so; 
        JTextArea txt; 

        Receiver(MulticastSocket sock, JTextArea txtAr) { 
            so = sock; 
            txt = txtAr;
            activity.start(); 
        }

        public void run() { 
            byte[] data = new byte[1024]; 
            while(true) try { 
                Calendar calendar = Calendar.getInstance();
                java.util.Date now = calendar.getTime();
                java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());

                DatagramPacket packet = new DatagramPacket(data,data.length); 
                so.receive(packet); 
                String mess = new String(data,0,packet.getLength()); 
                txt.append(currentTimestamp+ "\n" +mess+ "\n"); 
                System.out.println(mess);
            } catch(IOException e) { 
                break; 
            }    
        } 
    }
}

这里是jsf页面,我试图调用该聊天室。通过按下按钮执行以下操作:

<h:body>
    <div id="top">
        <ui:insert name="top">

            <h:form>
                <div style="height: 70px; margin: 10px auto 0px; background: #00A2E8; text-align: left;">
                    <a href="../User/userhome.xhtml?faces-redirect=true"><img src="../Images/logo.png" style="width: 100px; height: 70px; float: none;"/></a>
                    <div style="float: right; text-align: right;">
                        <h:panelGrid columns="2">
                            Hello,<h:outputLabel value="#{usersController.username}" />


                            <h:commandButton value="Logout" action="/Guest/guesthome.xhtml?faces-redirect=true"/>
                        </h:panelGrid>
                    </div>
                </div>

            </h:form>

        </ui:insert>
    </div>

    <div id="content" class="center_content" style="background-image: url('./Images/backgroundblue.jpg'); background-repeat: no-repeat;
background-size:cover;">
        <ui:insert name="content">
            <div id="content2" style="background-color: #CCC; width: 1000px; margin: 0 auto; opacity: 1;
filter: alpha(opacity=40);">

                <h:form style="margin-left: 300px;">
                    <ui:include src="/User/usertemplate/sprybar.xhtml" />
                </h:form>
                <br></br>
                <br></br>
                <br></br>
                <h:form style="border-bottom: 1px solid #AFAFAF;"></h:form>
                <br></br>

                <h:form style="text-align: center;">
                    <div style="text-align: center;">
                        <h1><h:outputText value="#{usersController.selected.firstname}" title="#{bundle.ViewUsersTitle_firstname}"/>
                        <h:outputText value="&#160;" />
                        <h:outputText value="#{usersController.selected.lastname}" title="#{bundle.ViewUsersTitle_lastname}"/></h1>
                    </div>
                    <br></br><br></br>

                </h:form>

                <h:form>

                    <h:commandButton action="#{chatbox.mmm()}" value="Send Message"/>
                    </h:form>
                    <br></br><br></br>

                <p:panelGrid columns="2" style="float: left; margin-left: 150px;">
                    <h:outputText value="#{bundle.ViewUsersLabel_userid}"/>
                    <h:outputText value="#{usersController.selected.userid}" title="#{bundle.ViewUsersTitle_userid}"/>
                    <h:outputText value="#{bundle.ViewUsersLabel_username}"/>
                    <h:outputText value="#{usersController.selected.username}" title="#{bundle.ViewUsersTitle_username}"/>
                    <h:outputText value="#{bundle.ViewUsersLabel_password}"/>
                    <h:outputText value="#{usersController.selected.password}" title="#{bundle.ViewUsersTitle_password}"/>
                    <h:outputText value="#{bundle.ViewUsersLabel_email}"/>
                    <h:outputText value="#{usersController.selected.email}" title="#{bundle.ViewUsersTitle_email}"/>
                    <h:outputText value="#{bundle.ViewUsersLabel_lastname}"/>
                    <h:outputText value="#{usersController.selected.lastname}" title="#{bundle.ViewUsersTitle_lastname}"/>
                    <h:outputText value="#{bundle.ViewUsersLabel_firstname}"/>
                    <h:outputText value="#{usersController.selected.firstname}" title="#{bundle.ViewUsersTitle_firstname}"/>
                    <h:outputText value="#{bundle.ViewUsersLabel_dob}"/>
                    <h:outputText value="#{usersController.selected.dob}" title="#{bundle.ViewUsersTitle_dob}">
                        <f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
                    </h:outputText>
                    <h:outputText value="#{bundle.ViewUsersLabel_detailid}"/>
                    <h:outputText value="#{usersController.selected.detailid}" title="#{bundle.ViewUsersTitle_detailid}"/>
                    <h:outputText value="#{bundle.ViewUsersLabel_roleid}"/>
                    <h:outputText value="#{usersController.selected.roleid}" title="#{bundle.ViewUsersTitle_roleid}"/>
                    <h:outputText value="#{bundle.ViewUsersLabel_countryid}"/>
                    <h:outputText value="#{usersController.selected.countryid}" title="#{bundle.ViewUsersTitle_countryid}"/>


                </p:panelGrid>


                <p:panelGrid columns="2" style="float: right; margin-right: 150px;">
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_detailid}"/>
                    <h:outputText value="#{usersController.ud.detailid}" title="#{bundle.ViewUserdetailsTitle_detailid}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_orientation}"/>
                    <h:outputText value="#{usersController.ud.orientation}" title="#{bundle.ViewUserdetailsTitle_orientation}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_ethnicity}"/>
                    <h:outputText value="#{usersController.ud.ethnicity}" title="#{bundle.ViewUserdetailsTitle_ethnicity}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_height}"/>
                    <h:outputText value="#{usersController.ud.height}" title="#{bundle.ViewUserdetailsTitle_height}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_weight}"/>
                    <h:outputText value="#{usersController.ud.weight}" title="#{bundle.ViewUserdetailsTitle_weight}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_smoking}"/>
                    <h:outputText value="#{usersController.ud.smoking}" title="#{bundle.ViewUserdetailsTitle_smoking}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_drinking}"/>
                    <h:outputText value="#{usersController.ud.drinking}" title="#{bundle.ViewUserdetailsTitle_drinking}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_drugs}"/>
                    <h:outputText value="#{usersController.ud.drugs}" title="#{bundle.ViewUserdetailsTitle_drugs}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_horoscope}"/>
                    <h:outputText value="#{usersController.ud.horoscope}" title="#{bundle.ViewUserdetailsTitle_horoscope}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_education}"/>
                    <h:outputText value="#{usersController.ud.education}" title="#{bundle.ViewUserdetailsTitle_education}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_languagespoken}"/>
                    <h:outputText value="#{usersController.ud.languagespoken}" title="#{bundle.ViewUserdetailsTitle_languagespoken}"/>
                    <h:outputText value="#{bundle.ViewUserdetailsLabel_gender}"/>
                    <h:outputText value="#{usersController.ud.gender}" title="#{bundle.ViewUserdetailsTitle_gender}"/>
                </p:panelGrid>




            </div>
        </ui:insert>
    </div>

    <div id="bottom">
        <ui:insert name="bottom">Bottom</ui:insert>
    </div>
</h:body>

使用jsf中的java类的想法可能吗?如果是,如何进行? 提前谢谢


共 (0) 个答案