gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:53:40 25 4
gpt4 key购买 nike

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

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

这是我的代码:(第一次使用这个论坛,发帖错误请见谅)

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]"));

是你的问题。您将第 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.

关于java - 如何防止我的 JScrollPane/JTextArea 超出包含的 JTabbedPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10670408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com