java吧 关注:1,300,606贴子:12,843,961
  • 7回复贴,共1

为JPanel添加滚动条需要刷新窗口才能显示

只看楼主收藏回复

但是JPanel内组件就不显示了



IP属地:上海来自Android客户端1楼2017-04-13 10:25回复
    jPanel_message = new JPanel();
    jPanel_message.setBackground(Color.WHITE);
    JScrollPane jScrollPane = new JScrollPane(jPanel_message);
    jScrollPane.setBounds(10, 10, 580, 300);
    jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    contenPane.add(jScrollPane);


    IP属地:上海来自Android客户端2楼2017-04-13 10:25
    回复
      2026-03-30 22:12:31
      广告
      不感兴趣
      开通SVIP免广告
      JButton jButton = new JButton("发送");
      jButton.setBounds(10, 430 , 80 , 30);
      jButton.setBackground(Color.LIGHT_GRAY);
      jButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
      String msg = jTextPane_send.getText();
      if(msg != null && !msg.equals("")) {
      JBubble jBubble = new JBubble(msg, jPanel_message);
      jBubble.setFont(font);
      jBubble.setBounds(0, messageheight, jPanel_message.getWidth(), jBubble.height + 15 < 50 ? 50 : jBubble.height + 15);
      jPanel_message.add(jBubble);
      messageheight += ((jBubble.height + 15 < 50 ? 50 : jBubble.height + 15) + 10);
      jPanel_message.setPreferredSize(new Dimension(580, messageheight));
      jScrollPane.repaint();
      repaint();
      sendMsg(msg);
      }
      }
      });
      contenPane.add(jButton);


      IP属地:上海4楼2017-04-13 10:27
      收起回复
        @Override
        public void run() {
        while(true) {
        try {
        datagramSocket.receive(datagramPacket);
        msg = new String(bytes, "UTF-8").trim();
        JBubble jBubble = new JBubble(msg, ip, jPanel_message);
        jBubble.setFont(font);
        jBubble.setBounds(0, messageheight, jPanel_message.getWidth(), jBubble.height + 15 < 50 ? 50 : jBubble.height + 15);
        jPanel_message.add(jBubble);
        messageheight += ((jBubble.height + 15 < 50 ? 50 : jBubble.height + 15) + 10);
        jPanel_message.setPreferredSize(new Dimension(580, messageheight));
        jScrollPane.repaint();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }


        IP属地:上海5楼2017-04-13 10:27
        收起回复
          顺便想问下我想显示emoji表情应该怎么办


          IP属地:上海6楼2017-04-13 10:28
          回复
            JBubble的定义,是否和我没有重写repaint有关?那该怎么改?
            public class JBubble extends JComponent {
            private String msg;
            private BufferedImage image;
            private JComponent jComponent;
            private int width = 0;
            private boolean flag;
            private List<String> stringList;
            public int height = 0;
            public JBubble(String msg, JComponent jComponent) {
            try {
            this.image = ImageIO.read(new File("src"+File.separator+"image"+File.separator+"img_yourself.png"));
            } catch (IOException e) {
            e.printStackTrace();
            }
            this.jComponent = jComponent;
            this.msg = msg;
            this.flag = true;
            this.stringList = new ArrayList<>();
            cutString(msg);
            height = (int)(stringList.size()*getPoint(msg).getY());
            addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
            if(e.getClickCount() == 2) {
            }
            }
            });
            }
            public JBubble(String msg, String ip, JComponent jComponent) {
            this(msg, jComponent);
            this.flag = false;
            try {
            this.image = ImageIO.read(new File("src"+File.separator+"image"+File.separator+"img_"+ip+".png"));
            } catch (IOException e) {
            e.printStackTrace();
            }
            }
            @Override
            protected void paintComponent(Graphics g) {
            Graphics2D graphics2D = (Graphics2D) g;
            if(!flag) {
            graphics2D.drawImage(image, 0, 0, 50, 50, this);
            graphics2D.setColor(Color.LIGHT_GRAY);
            int[] xs = {60, 75, 75, 60};
            int[] ys = {25, 20, 30, 25};
            graphics2D.fillPolygon(xs, ys, 3);
            graphics2D.setColor(Color.lightGray);
            graphics2D.fillRoundRect(75, 10, width + 5, height+5, 15, 15 );
            graphics2D.setColor(Color.DARK_GRAY);
            height = 0;
            Iterator iterator = stringList.iterator();
            while(iterator.hasNext()) {
            height += getPoint(msg).getY();
            graphics2D.drawString((String) iterator.next(), 80, height + 10);
            }
            } else {
            graphics2D.drawImage(image, jComponent.getWidth() - 50, 0, 50, 50, this);
            graphics2D.setColor(Color.LIGHT_GRAY);
            int[] xs = {jComponent.getWidth() - 60, jComponent.getWidth() - 75, jComponent.getWidth() - 75, jComponent.getWidth() - 60};
            int[] ys = {25, 20, 30, 25};
            graphics2D.fillPolygon(xs, ys, 3);
            graphics2D.setColor(Color.lightGray);
            int height = (int)(stringList.size()*getPoint(msg).getY());
            graphics2D.fillRoundRect(jComponent.getWidth() - 75 - width, 10, width + 5, height+5, 15, 15 );
            graphics2D.setColor(Color.DARK_GRAY);
            height = 0;
            Iterator iterator = stringList.iterator();
            while(iterator.hasNext()) {
            height += getPoint(msg).getY();
            graphics2D.drawString((String) iterator.next(), jComponent.getWidth() - 70 - width, height + 10);
            }
            }
            super.paintComponent(g);
            }
            @Override
            public boolean contains(int x, int y) {
            if(!flag) {
            return (x - 25) * (x - 25) + (y - 25) * (y - 25) < 625;
            }
            return false;
            }
            public void cutString(String msg) {
            if(getPoint(msg).getX() + 160 < jComponent.getWidth()) {
            stringList.add(msg);
            if(width == 0) {
            width = (int) getPoint(msg).getX();
            }
            } else {
            width = jComponent.getWidth() - 160;
            String temp = "";
            for(int i = 0; i < msg.length(); i++) {
            if(getPoint(temp + msg.charAt(i)).getX() + 160 > jComponent.getWidth()) {
            stringList.add(temp);
            msg = msg.substring(i, msg.length());
            cutString(msg);
            break;
            }
            temp += msg.charAt(i);
            }
            }
            }
            }


            IP属地:上海7楼2017-04-13 11:02
            回复