使用JAVA Eclipse收到消息后,如何在此JPanel中添加动态聊天气泡

时间:2019-06-28 18:43:58

标签: java swing

public class ChatWindow extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField txtSearch;
    static String c;
    static String path;
    static ReceiveMail rcv = new ReceiveMail();
    static String b;
    private JList list;

    /**
     * Launch the application.
     */
    public static void main(String[] args) 
    {
        /*rcv.run();
        b = rcv.a;*/
        System.out.println(b);
        EventQueue.invokeLater(new Runnable() {
            public void run() {

                try {
                    ChatWindow frame = new ChatWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });     
    }
    /*public void LoadList()
    {
        Connection con;
         Statement st;
         ResultSet rs;
         DefaultListModel DLM = new DefaultListModel();
         try{
             con = DriverManager.getConnection("jdbc:mysql:///FP\",\"root\", \"()(Madrid)()");
             st = con.createStatement();
             rs = st.executeQuery("select * from User");
             while(rs.next())
             {
                DLM.addElement(rs.getString(1)); 
             }

             st.close();
             rs.close();

         }catch(Exception ex){
             ex.printStackTrace();
         }
    }

    /**
     * Create the frame.
     */
    public ChatWindow() {

        setTitle("SnapChat");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 776, 569);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(255, 239, 213));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBackground(new Color(128, 0, 0));
        panel.setBounds(0, 0, 252, 547);
        contentPane.add(panel);
        panel.setLayout(null);

        txtSearch = new JTextField();
        txtSearch.setText("          Search Contacts");
        txtSearch.setBackground(new Color(255, 250, 240));
        txtSearch.setBounds(6, 6, 182, 26);
        txtSearch.setColumns(10);
        panel.add(txtSearch);

        JButton btnLogOut = new JButton("Log Out");
        btnLogOut.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                LogIn frame = new LogIn();
                frame.setVisible(true);         
            }
        });
        btnLogOut.setBounds(55, 512, 117, 29);
        panel.add(btnLogOut);

        list = new JList();
        list.setBounds(6, 512, 240, -467);
        panel.add(list);

        JButton btnNewButton_1 = new JButton("");
        btnNewButton_1.setIcon(new ImageIcon("/Users/mohammadbilal/Documents/Uni/Final Project/src/Images/3 2.png"));
        btnNewButton_1.setBounds(185, 6, 42, 26);
        panel.add(btnNewButton_1);    

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(128, 0, 0));
        panel_1.setBounds(251, 0, 525, 72);
        contentPane.add(panel_1);
        panel_1.setLayout(null);

        textField = new JTextField();
        textField.setBackground(new Color(255, 250, 240));
        textField.setBounds(256, 489, 391, 58);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("");
        btnNewButton.setIcon(new ImageIcon("/Users/mohammadbilal/Documents/Uni/Final Project/src/Images/2 2.png"));
        btnNewButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION) 
                {
                    System.out.println("You chose to open this file: " +chooser.getSelectedFile().getName());
                    File selectedFile = chooser.getSelectedFile();
                    path = selectedFile.getAbsolutePath();
                    c =chooser.getSelectedFile().getName();            
                }
            }
        });

        btnNewButton.setBounds(659, 489, 117, 29);
        contentPane.add(btnNewButton);

        JButton btnSend = new JButton("");
        btnSend.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ImageToChunks chunks = new ImageToChunks();
                try {
                    chunks.send();
                    System.out.println("Sent");
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        btnSend.setIcon(new ImageIcon("/Users/mohammadbilal/Documents/Uni/Final Project/src/Images/1 2.png"));
        btnSend.setBounds(659, 518, 117, 29);
        contentPane.add(btnSend);

        JPanel panel_2 = new JPanel();
        panel_2.setBackground(new Color(255, 239, 213));
        panel_2.setBounds(251, 71, 525, 418);
        contentPane.add(panel_2);
        panel_2.setLayout(null);

        JLabel lblNewLabel_1 = new JLabel();
        lblNewLabel_1.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
        lblNewLabel_1.setBounds(6, 6, 182, 22);
        panel_2.add(lblNewLabel_1);
        lblNewLabel_1.setText(b);

        //LoadList();

            }   
}

这是应该显示聊天气泡的JFrame窗口。每当有味精到来时,我都希望将其动态放置在气泡中并进行调整

enter image description here

0 个答案:

没有答案