无法添加make JTextArea在JScrollPane中显示多行

时间:2013-11-10 03:44:30

标签: java swing

我正在尝试使JTextArea的多行可见。我正在使用GridBagLayout来添加组件。这是一段代码:

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;


public class SSCE {
    SSCE(){
        JFrame f1=new JFrame();
        GridBagLayout gbl=new GridBagLayout();
        JButton btnAddAcc=new JButton("Add Acount");
        JButton insertId=new JButton("Insert");
        JButton insertTweet=new JButton("Insert2");
        JButton tweetButton=new JButton("TweetButton");

        JLabel accountStatusHeader=new JLabel("account  status Header");
        JLabel accountDisplayNameHeader=new JLabel("account  displayname Header");
        JLabel enterInterval=new JLabel("enter Interval!!");

        final JTextArea accountDispName = new JTextArea(50, 50);
        final JTextArea statusDisplay = new JTextArea(50, 50);

        final JTextArea jTextAreaId = new JTextArea(20, 50);
        final JTextArea jTextAreaTweets = new JTextArea(20, 50);
        jTextAreaId.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,
                Color.PINK, Color.GREEN));
        final JTextArea tweetLog = new JTextArea(100, 100);
        tweetLog.setPreferredSize(new Dimension(1000, 5000));
        JScrollPane tweetLogPaneScroll = new JScrollPane(tweetLog);
        JScrollPane idScrollPane = new JScrollPane(jTextAreaId);
        JScrollPane tweetScrollPane = new JScrollPane(jTextAreaTweets);


        final JTextField timeIntervalInput = new JTextField(20);
        final JTextField tagIdInsertTextBox = new JTextField(50);
        final JTextField tweetInsertTextBox = new JTextField(50);



        f1.setLayout(gbl);
        f1.add(btnAddAcc,makeGbc(0,0,1,2));
        f1.add(accountDisplayNameHeader,makeGbc(1,0));
        f1.add(accountStatusHeader,makeGbc(1,1));
        f1.add(accountDispName,makeGbc(2,0));
        f1.add(statusDisplay,makeGbc(2,1));
        f1.add(enterInterval,makeGbc(3,0));
        f1.add(timeIntervalInput,makeGbc(3,1));
        f1.add(new JLabel("Twitter Ids"),makeGbc(4,0));
        f1.add(new JLabel("Tweets"),makeGbc(4,1));
        f1.add(idScrollPane,makeGbc(5,0,5));
        f1.add(tweetScrollPane,makeGbc(5,1,5));
        f1.add(tagIdInsertTextBox,makeGbc(10,0));
        f1.add(tweetInsertTextBox,makeGbc(10,1));
        f1.add(insertId,makeGbc(11,0));
        f1.add(insertTweet,makeGbc(11,1));
        f1.add(tweetButton,makeGbc(12,0,1,2));
        f1.add(tweetLogPaneScroll,makeGbc(13,0,6,2));


        f1.setSize(800,400);
        f1.setVisible(true);
        f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        accountDispName.setVisible(false);
        statusDisplay.setVisible(false);

    }
    private GridBagConstraints makeGbc(int y, int x) {
        GridBagConstraints gbc = new GridBagConstraints();
//        gbc.gridwidth = 1;
//        gbc.gridheight = 1;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.insets = new Insets(2, 2, 2, 2);
        gbc.anchor = (y == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
        gbc.fill = GridBagConstraints.BOTH;
        return gbc;
    }
    private GridBagConstraints makeGbc(int y, int x,int gridheight) {
        GridBagConstraints gbc = new GridBagConstraints();
//        gbc.gridwidth = 1;
        gbc.gridheight = gridheight;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.insets = new Insets(2, 2, 2, 2);
        gbc.anchor = (y == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
        gbc.fill = GridBagConstraints.BOTH;
        return gbc;
    }
    private GridBagConstraints makeGbc(int y, int x,int gridheight,int gridwidth) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.insets = new Insets(2, 2, 2, 2);
        gbc.anchor = (y == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
        gbc.fill = GridBagConstraints.BOTH;
        return gbc;
    }
    public static void main(String args[]){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SSCE a1;
                a1 = new SSCE();
            }
        });
    }

}

请注意以下几行:

f1.add(idScrollPane,makeGbc(5,0,5));
f1.add(tweetScrollPane,makeGbc(5,1,5));

在上面,我将第三个参数(网格高度)传递为5但仍然只看到一行。我想将行间距设置为5。 还有以下内容:

f1.add(tweetLogPaneScroll,makeGbc(13,0,6,2));

再次,我将第三个参数(gridheight)传递给6.但是我只看到一行textarea。出了什么问题?最新解决方案是什么?

1 个答案:

答案 0 :(得分:2)

你的SSCCE帮助我看到一切 - 谢谢!我已经投票决定打开你的问题并进行投票。您使用不切实际的JTextArea行号自杀,然后设置GUI的大小。摆脱所有setSize(...)setPreferredSize(...)方法调用。将您的JTextArea行计为5或10,而不是50,而不是100.在pack()之前调用setVisible(true)

例如,请参阅我在下面所做的更改以及其中!!的评论。请注意,我试图摆脱大多数你的幻数,但你仍然需要对列计数做同样的事情。为了调试,我还在文本组件中添加了文本,以便我可以一目了然地看到哪个文本组件与哪个变量一起使用。您当然不希望在演示文稿代码中显示此文本,但同样,它是一个有用的调试工具:

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;

public class SSCE {
   private static final int SMALL_ROWS = 5; // !! was 20!
   private static final int BIG_ROWS = 10; // !! was 50!

   SSCE() {
      JFrame f1 = new JFrame();
      GridBagLayout gbl = new GridBagLayout();
      JButton btnAddAcc = new JButton("Add Acount");
      JButton insertId = new JButton("Insert");
      JButton insertTweet = new JButton("Insert2");
      JButton tweetButton = new JButton("TweetButton");
      JLabel accountStatusHeader = new JLabel("account  status Header");
      JLabel accountDisplayNameHeader = new JLabel(
            "account  displayname Header");
      JLabel enterInterval = new JLabel("enter Interval!!");
      final JTextArea accountDispName = new JTextArea("accountDispName JTA",
            BIG_ROWS, 50);
      final JTextArea statusDisplay = new JTextArea("statusDisplay JTA",
            BIG_ROWS, 50);
      final JTextArea jTextAreaId = new JTextArea("jTextAreaId JTA",
            SMALL_ROWS, 50);
      final JTextArea jTextAreaTweets = new JTextArea("jTextAreaTweets JTA",
            SMALL_ROWS, 50);
      jTextAreaId.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,
            Color.PINK, Color.GREEN));
      final JTextArea tweetLog = new JTextArea("tweetLog JTA", BIG_ROWS, 100); // was
                                                                               // 100!
      // !! tweetLog.setPreferredSize(new Dimension(1000, 5000));
      JScrollPane tweetLogPaneScroll = new JScrollPane(tweetLog);
      JScrollPane idScrollPane = new JScrollPane(jTextAreaId);
      JScrollPane tweetScrollPane = new JScrollPane(jTextAreaTweets);
      final JTextField timeIntervalInput = new JTextField(
            "timeIntervalInput JTF", 20);
      final JTextField tagIdInsertTextBox = new JTextField(
            "tagIdInsertTextBox JTF", 50);
      final JTextField tweetInsertTextBox = new JTextField(
            "tweetInsertTextBox JTF", 50);
      f1.setLayout(gbl);
      f1.add(btnAddAcc, makeGbc(0, 0, 1, 2));
      f1.add(accountDisplayNameHeader, makeGbc(1, 0));
      f1.add(accountStatusHeader, makeGbc(1, 1));
      f1.add(accountDispName, makeGbc(2, 0));
      f1.add(statusDisplay, makeGbc(2, 1));
      f1.add(enterInterval, makeGbc(3, 0));
      f1.add(timeIntervalInput, makeGbc(3, 1));
      f1.add(new JLabel("Twitter Ids"), makeGbc(4, 0));
      f1.add(new JLabel("Tweets"), makeGbc(4, 1));
      f1.add(idScrollPane, makeGbc(5, 0, 5));
      f1.add(tweetScrollPane, makeGbc(5, 1, 5));
      f1.add(tagIdInsertTextBox, makeGbc(10, 0));
      f1.add(tweetInsertTextBox, makeGbc(10, 1));
      f1.add(insertId, makeGbc(11, 0));
      f1.add(insertTweet, makeGbc(11, 1));
      f1.add(tweetButton, makeGbc(12, 0, 1, 2));
      f1.add(tweetLogPaneScroll, makeGbc(13, 0, 6, 2));
      // !! f1.setSize(800, 400);
      f1.pack(); // !!
      f1.setVisible(true);
      f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      accountDispName.setVisible(false);
      statusDisplay.setVisible(false);
   }

   private GridBagConstraints makeGbc(int y, int x) {
      GridBagConstraints gbc = new GridBagConstraints();
      // gbc.gridwidth = 1;
      // gbc.gridheight = 1;
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.insets = new Insets(2, 2, 2, 2);
      gbc.anchor = (y == 0) ? GridBagConstraints.LINE_START
            : GridBagConstraints.LINE_END;
      gbc.fill = GridBagConstraints.BOTH;
      System.out.printf("gridwidth, gridheight: [%d, %d]%n", gbc.gridwidth,
            gbc.gridheight);
      return gbc;
   }

   private GridBagConstraints makeGbc(int y, int x, int gridheight) {
      GridBagConstraints gbc = new GridBagConstraints();
      // gbc.gridwidth = 1;
      gbc.gridheight = gridheight;
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.insets = new Insets(2, 2, 2, 2);
      gbc.anchor = (y == 0) ? GridBagConstraints.LINE_START
            : GridBagConstraints.LINE_END;
      gbc.fill = GridBagConstraints.BOTH;
      return gbc;
   }

   private GridBagConstraints makeGbc(int y, int x, int gridheight,
         int gridwidth) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = gridwidth;
      gbc.gridheight = gridheight;
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.insets = new Insets(2, 2, 2, 2);
      gbc.anchor = (y == 0) ? GridBagConstraints.LINE_START
            : GridBagConstraints.LINE_END;
      gbc.fill = GridBagConstraints.BOTH;
      return gbc;
   }

   public static void main(String args[]) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            SSCE a1;
            a1 = new SSCE();
         }
      });
   }
}

这导致GUI看起来像这样:

enter image description here

注意,我也会将JTextFields的GridBagConstraints从BOTH更改为HORIZONTAL。


修改
您在评论中说明:

  

如果你不介意回答,还有一个问题。虽然我已经将它定义为最多20个字符,但是TimeIntervalInput显得如此之宽。可以解决这个问题吗?

您需要继续使用网格包约束,因为您使用的限制非常严格。例如,请注意当我在GBC上对该JTextField使用更严格的约束时会发生什么:

  GridBagConstraints gbc = makeGbc(3, 1);
  gbc.fill = GridBagConstraints.NONE;
  gbc.anchor = GridBagConstraints.WEST;
  f1.add(timeIntervalInput, gbc);