Bottom of JScrollPane is cut off

后端 未结 2 1893
无人及你
无人及你 2021-01-28 20:09

I am trying to create a simple email client and the bottom of the body is being cut-off. If I add a horizontal scroll bar, it does not appear, and the bottom of the Vertical scr

2条回答
  •  半阙折子戏
    2021-01-28 20:46

    The problem is because of FlowLayout Manager is being used. I have solved your problem with a different layout manager.
    Before posting the solution here are some tips that you should follow

    1. Change your class name. It should be in camel-case
    2. Try to call pack() instead of setSize() as it will handle it automatically. When I replaced your setSize() with pack(), it was showing a awkward looking GUI which proves your layout and adding elements were not proper.

          Font size30 = new Font(null, Font.PLAIN, 30);
      
          JPanel pnl = new JPanel();
          pnl.setLayout(new BoxLayout(pnl,BoxLayout.Y_AXIS));
      
          Container contentPane = getContentPane();
      
          //--- User Info ---//
      
          JPanel userInfo = new JPanel();
      
          JLabel userLabel = new JLabel("Username: ");
          JTextField userField = new JTextField(12);
          userInfo.add(userLabel);
          userInfo.add(userField);
      
          JLabel passLabel = new JLabel("Password: ");
          JTextField passField = new JTextField(10);
          userInfo.add(passLabel);
          userInfo.add(passField);
      
          JLabel serverLabel = new JLabel("Mail Server: ");
          JTextField serverField = new JTextField(10);
          userInfo.add(serverLabel);
          userInfo.add(serverField);
      
          JLabel portLabel = new JLabel("Server Port: ");
          JTextField portField = new JTextField(3);
          userInfo.add(portLabel);
          userInfo.add(portField);
      
          //--- To: CC: and Subject Fields ---//
      
          JPanel msgInfo = new JPanel();
      
          JLabel toLabel = new JLabel("To: ");
          JTextField toField = new JTextField(30);
          msgInfo.add(toLabel);
          msgInfo.add(toField);
      
          JLabel subLabel = new JLabel("Subject: ");
          JTextField subField = new JTextField(30);
          msgInfo.add(subLabel);
          msgInfo.add(subField);
      
          //--- Body ---//
      
          JLabel bodyLabel = new JLabel("Body");
          bodyLabel.setFont(size30);
      
          JTextArea bodyField = new JTextArea(30,70);
          bodyField.setLineWrap(true);
          bodyField.setWrapStyleWord(true);
      
          JScrollPane bodyScroll = new JScrollPane(bodyField);
      
          bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
          bodyScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      
      
          pnl.add(userInfo);
          pnl.add(msgInfo);
          pnl.add(bodyLabel);
          pnl.add(bodyScroll);
      
          contentPane.add(pnl);
      
          setVisible(true);
          pack();
      

提交回复
热议问题