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

时间:2012-05-20 02:47:53

标签: java swing jscrollpane jtextarea miglayout

我无法弄清楚为什么我的代码会导致底层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;
    }
}

1 个答案:

答案 0 :(得分:0)

我相信您希望底部JTabbedPane适应您的应用程序窗口高度。如果是 - 行:

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

是你的问题。您将第3行的最小高度设置为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.