如何防止JScrollPane / JTextArea超出包含的JTabbedPane? - java

我不知道为什么我的代码会导致底部JTextArea超出其所在窗口的边界。

我使用WindowBuilder创建了GUI,并尝试了多种不同的方法来限制大小,但是我似乎无法弄清为什么它会超出我在WindowBuilder的“设计”视图中设置的大小。

这是我的代码:(这是我第一次使用此论坛,请原谅发布错误)

public class Reporter extends JFrame {
    private static final long serialVersionUID = 1L;
    class MyTableModel extends AbstractTableModel {
        private static final long serialVersionUID = 1L;
        private String[] columnNames;
        private Object[][] data;

        public MyTableModel(String[] columnNames) {
            this.columnNames=columnNames;
            this.data=new Object[0][columnNames.length];
        }
        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public Class<? extends Object> getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
        public void addRow(Object[] objs) {
            Object[][] newData = new Object[this.getRowCount()+1][this.getColumnCount()];
            for (int row=0; row<this.getRowCount(); row++) {
                for (int col=0; col<this.getColumnCount(); col++) {
                    newData[row][col]=this.data[row][col];
                }
            }
            for (int col=0; col<this.getColumnCount(); col++) {
                newData[newData.length-1][col]=objs[col];
            }
            this.data=newData;
            fireTableDataChanged();
        }
        public void setRow(int rowIndex, Object[] objs) {
            for (int col=0; col<this.getColumnCount(); col++) {
                this.data[rowIndex][col]=objs[col];
            }
            fireTableRowsUpdated(rowIndex, rowIndex);
        }
    }
    private JTabbedPane bugTabPane;
    private JScrollPane tableScrollPane;
    private ArrayList<JTextArea> bugOuts;
    private JScrollPane graphScrollPane;
    private JTable table;
    private JScrollPane announcerScrollPane;
    private JTextArea announcerTextArea;
    private GraphBox graphPane;

    public Reporter() {
        setBounds(100, 100, 903, 751);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        getContentPane().setLayout(new MigLayout("", "[grow]", "[200px:200px][200px:200px:200px][200px:200px:200px]"));

        this.graphScrollPane = new JScrollPane();
        this.graphScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        this.graphScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        getContentPane().add(this.graphScrollPane, "cell 0 0,grow");
        this.graphPane = new GraphBox();
        this.graphScrollPane.add(graphPane);

        Attribute[] attributeNames=Attribute.values();
        String[] otherColumnNames={"Name","Creator","Last Act"};
        String[] columnNames = new String[attributeNames.length+otherColumnNames.length];
        for (int i=0; i<otherColumnNames.length; i++) {
            columnNames[i]=otherColumnNames[i];
        }
        for (int i=0; i<attributeNames.length; i++) {
            columnNames[otherColumnNames.length+i]=attributeNames[i].toString();
        }

        this.tableScrollPane = new JScrollPane();
        getContentPane().add(this.tableScrollPane, "cell 0 1,grow");
        this.table = new JTable(new MyTableModel(columnNames));
        this.tableScrollPane.setViewportView(this.table);

        this.bugTabPane = new JTabbedPane(JTabbedPane.TOP);
        getContentPane().add(this.bugTabPane, "cell 0 2,grow");
        this.bugTabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        this.announcerScrollPane = new JScrollPane();
        this.announcerScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        this.announcerScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        this.bugTabPane.addTab("Announcer", null, this.announcerScrollPane, null);

        this.announcerTextArea = new JTextArea(1, 40);
        this.announcerTextArea.setMaximumSize(new Dimension(2147483647, 200));
        this.announcerTextArea.setEnabled(false);
        this.announcerScrollPane.setViewportView(this.announcerTextArea);
        this.bugOuts = new ArrayList<JTextArea>();
        pack();

    }
    public TableModel getTableModel() {
        return this.table.getModel();
    }
    public void addTableRow(BugStepRecord bugStepRecord) {
        Object[] rowObjs = new Object[this.table.getColumnCount()];
        rowObjs[0] = bugStepRecord.getName();
        rowObjs[1] = bugStepRecord.getCreator();
        rowObjs[2] = bugStepRecord.getAct();
        for (int i=0; i<bugStepRecord.getAttributeVals().length; i++) {
            rowObjs[3+i] = bugStepRecord.getAttributeVals()[i];
        }
        MyTableModel myTableModel = (MyTableModel)this.table.getModel();
        myTableModel.addRow(rowObjs);
    }
    public void updateTableRow(int rowIndex, BugStepRecord bugStepRecord) {
        Object[] rowObjs = new Object[this.table.getColumnCount()];
        rowObjs[0] = bugStepRecord.getName();
        rowObjs[1] = bugStepRecord.getCreator();
        rowObjs[2] = bugStepRecord.getAct();
        for (int i=0; i<bugStepRecord.getAttributeVals().length; i++) {
            rowObjs[3+i] = bugStepRecord.getAttributeVals()[i];
        }
        MyTableModel myTableModel = (MyTableModel)this.table.getModel();
        myTableModel.setRow(rowIndex, rowObjs);
    }
    public JTextArea addBugTab(String name, Icon icon) {
        final JTextArea textArea = new JTextArea(1, 40);
        textArea.setEditable(false);
        JScrollPane bugScrollPane = new JScrollPane();
        bugScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        bugScrollPane.setViewportView(textArea);
        this.bugOuts.add(textArea);
        this.bugTabPane.addTab(name, icon, bugScrollPane, null);
        return textArea;
    }
}

参考方案

我相信您希望底部的JTabbedPane适应您的应用程序窗口的高度。在这种情况下-行:

new MigLayout("", "[grow]", "[200px:200px][200px:200px:200px][200px:200px:200px]"));

是你的问题。您将第三行的最小高度设置为200px(如果您希望该行增长-也不发生-最大高度200px)

从MigLayout cheat sheet:

The format is "min:preferred:max", however there are shorter versions since for instance it is seldom needed to specify the maximum size.

Java-搜索字符串数组中的字符串 - java

在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…

如何使用正则表达式匹配以相反顺序排列的后两个字符为前两个字符的任何字符串 - java

Closed. This question needs to be more focused。它当前不接受答案。                                                                                                                            …

Java RegEx中的单词边界\ b - java

我在使用\b作为Java Regex中的单词定界符时遇到困难。对于text = "/* sql statement */ INSERT INTO someTable"; Pattern.compile("(?i)\binsert\b");找不到匹配项Pattern insPtrn = Pattern.compile(&…

Java Double与BigDecimal - java

我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…