gpt4 book ai didi

java - JScrollPane 不起作用(MigLayout)

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

我正在尝试使用 MiglayoutJScrollPane 放入 JTable 但如果我编译它,JScrollpane > 不起作用(所以没有箭头等等...)。

这是我的代码:

public class MyTableModel extends AbstractTableModel {
final String[] cols = {"", "", ""};
final Object[][] data = {
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
};

public int setRowHeight() {
return 50;
}

public int getColumnCount() {
return cols.length;
}

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

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

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

public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

public boolean isCellEditable(int row, int col) {
return false;
}

public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
System.out.println("setVal" + value); // da sind die Daten!
}

static void create() {
MigLayout layout = new MigLayout("debug,fill");
JFrame f = new JFrame();
JPanel p = new JPanel();

//Button
JButton addRow = new JButton("Add Row");
addRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("test OK");
}
});

p.setLayout(layout);
p.setBackground(Color.RED);

MyTableModel mod = new MyTableModel();

JTable table = new JTable(mod);

JScrollPane pane = new JScrollPane(table);

JTableHeader tableHeader = table.getTableHeader();

tableHeader.setReorderingAllowed(false);
tableHeader.setResizingAllowed(true);

table.setValueAt(new ImageIcon("C:\\redIcon.png"), 0, 2);
table.setRowHeight(40);

p.add(tableHeader, "dock north");
p.add(table, "dock center");
p.add(pane, "dock east");

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setSize(600, 300);
f.setContentPane(p);
f.setVisible(true);
}

public static void main(String[] Args) {
create();
}
}

最佳答案

在滚动 Pane 下方插入这两行:

pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

这会添加上/下和左/右滚动条。

但是你的代码有点复杂,很难看出你真正想要完成什么。尤其是添加大量 JPanel 的部分看起来很奇怪,而且看起来有问题。

关于java - JScrollPane 不起作用(MigLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34746170/

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