0

我正在尝试使 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。那么出了什么问题??什么是解决方案?

4

1 回答 1

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:

在此处输入图像描述

请注意,我还将 JTextFields 的 GridBagConstraints 从 BOTH 更改为 HORIZONTAL。


编辑
您在评论中声明:

如果您不介意回答,还有一个问题。尽管我已将 TimeIntervalInput 定义为最多容纳 20 个字符,但 TimeIntervalInput 看起来如此之宽。对此有什么解决方案吗?

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

  GridBagConstraints gbc = makeGbc(3, 1);
  gbc.fill = GridBagConstraints.NONE;
  gbc.anchor = GridBagConstraints.WEST;
  f1.add(timeIntervalInput, gbc);
于 2013-11-11T23:13:05.483 回答