gpt4 book ai didi

java - 滚动 Pane 垂直滚动条不显示滚动旋钮

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

我创建了一个 JScrollPane 并向其中添加了一个 JTable。我希望能够使用垂直滚动条滚动到表格底部。窗口 Pane 与表初始化一起显示,但是单击顶部或底部箭头不会分别将 View 推进到顶部或底部。垂直滚动轨道上也没有滚动条显示。

我尝试将视口(viewport)设置为不同的大小,还使用不同的构造函数来创建 jscrollpane,即 jscrollpane(table_3, JScrollPane.VERTICAL_SCROLLBAR_​​AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_​​AS_NEEDED);但都没有奏效。

public class test {
JFrame f;
int myMonth, myDay, myYear;
private JTable tblAppts;

private String[] colTimes = { "Time", "Test 1", "Test 2", "Test 3" };
private Object[][] myTimes = { { "Time", "Test 1", "Test 2", "Test 3" }, {
"00:30", "", "", "" },
{ "01:00", "", "", "" }, { "01:30", "", "", "" }, { "02:00", "",
"", "" }, { "02:30", "", "", "" },
{ "03:00", "", "", "" }, { "03:30", "", "", "" }, { "04:00", "",
"", "" }, { "04:30", "", "", "" },
{ "05:00", "", "", "" }, { "05:30", "", "", "" }, { "06:00", "",
"", "" }, { "06:30", "", "", "" },
{ "07:00", "", "", "" }, { "07:30", "", "", "" }, { "08:00", "",
"", "" }, { "08:30", "", "", "" },
{ "09:00", "", "", "" }, { "09:30", "", "", "" }, { "10:00", "",
"", "" }, { "10:30", "", "", "" },
{ "11:00", "", "", "" }, { "11:30", "", "", "" }, { "12:00", "",
"", "" }, { "12:30", "", "", "" },
{ "13:00", "", "", "" }, { "13:30", "", "", "" }, { "14:00", "",
"", "" }, { "14:30", "", "", "" },
{ "15:00", "", "", "" }, { "15:30", "", "", "" }, { "16:00", "",
"", "" }, { "16:30", "", "", "" },
{ "17:00", "", "", "" }, { "17:30", "", "", "" }, { "18:00", "",
"", "" }, { "18:30", "", "", "" },
{ "19:00", "", "", "" }, { "19:30", "", "", "" }, { "20:00", "",
"", "" }, { "20:30", "", "", "" },
{ "21:00", "", "", "" }, { "21:30", "", "", "" }, { "22:00", "",
"", "" }, { "22:30", "", "", "" },
{ "23:00", "", "", "" }, { "23:30", "", "", "" } };
private JTable tblTime;
private JTable table_1;
private JTable table_2;
private JTable table_3;

// set the table widths method
public static void setJTableColumnsWidth(JTable table, int
tablePreferredWidth, double... percentages) {
double total = 0;
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
total += percentages[i];
}

for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
TableColumn column = table.getColumnModel().getColumn(i);
column.setPreferredWidth((int) (tablePreferredWidth
(percentages[i] / total)));
}
}

test() {

f = new JFrame("WSDP");// creating instance of JFrame#
// open the frame in a maximized state ie max vert and horizontal
f.setSize(1000, 1000);
f.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);

// menus
JPanel panel = new JPanel();
panel.setBounds(0, 0, 982, 27);

// create border
Border blackline = BorderFactory.createLineBorder(Color.black);
panel.setBorder(blackline);
f.getContentPane().add(panel);

// icon buttons panel
JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 26, 109, 593);
panel_1.setBorder(blackline);
f.getContentPane().add(panel_1);

JScrollPane scrollPane = new JScrollPane();
// scrollPane.setBounds(107, 78, 475, 875);
scrollPane.setBounds(107, 78, 475, 500);
// scrollPane.setPreferredSize(new Dimension(450, 110));
scrollPane.setBorder(blackline);
scrollPane.setViewportBorder(blackline);



scrollPane.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

table_3 = new JTable(myTimes, colTimes);
scrollPane.setRowHeaderView(table_3);
table_3.setFillsViewportHeight(true);
scrollPane.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
table_3.setRowHeight(20);
table_3.setFont(new Font("Courier", Font.BOLD, 14));
setJTableColumnsWidth(table_3, 480, 10, 30, 30, 30);
f.getContentPane().add(scrollPane);
f.setVisible(true);

}

public static void main(String[] args) {

test t = new test();
}

}

最佳答案

您真的需要通读How to Use TablesHow to Use Scroll Panes因为您对 API 的工作原理存在一些根本性的误解。

您从未设置过 JScrollPane 的视口(viewport) View ...

我已将您的“示例”精简为一个完整且可验证的示例,该示例演示了您的问题的立即解决方案...

Example

import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

private String[] colTimes = {"Time", "Test 1", "Test 2", "Test 3"};
private Object[][] myTimes = {{"Time", "Test 1", "Test 2", "Test 3"}, {
"00:30", "", "", ""},
{"01:00", "", "", ""}, {"01:30", "", "", ""}, {"02:00", "",
"", ""}, {"02:30", "", "", ""},
{"03:00", "", "", ""}, {"03:30", "", "", ""}, {"04:00", "",
"", ""}, {"04:30", "", "", ""},
{"05:00", "", "", ""}, {"05:30", "", "", ""}, {"06:00", "",
"", ""}, {"06:30", "", "", ""},
{"07:00", "", "", ""}, {"07:30", "", "", ""}, {"08:00", "",
"", ""}, {"08:30", "", "", ""},
{"09:00", "", "", ""}, {"09:30", "", "", ""}, {"10:00", "",
"", ""}, {"10:30", "", "", ""},
{"11:00", "", "", ""}, {"11:30", "", "", ""}, {"12:00", "",
"", ""}, {"12:30", "", "", ""},
{"13:00", "", "", ""}, {"13:30", "", "", ""}, {"14:00", "",
"", ""}, {"14:30", "", "", ""},
{"15:00", "", "", ""}, {"15:30", "", "", ""}, {"16:00", "",
"", ""}, {"16:30", "", "", ""},
{"17:00", "", "", ""}, {"17:30", "", "", ""}, {"18:00", "",
"", ""}, {"18:30", "", "", ""},
{"19:00", "", "", ""}, {"19:30", "", "", ""}, {"20:00", "",
"", ""}, {"20:30", "", "", ""},
{"21:00", "", "", ""}, {"21:30", "", "", ""}, {"22:00", "",
"", ""}, {"22:30", "", "", ""},
{"23:00", "", "", ""}, {"23:30", "", "", ""}};
private JTable table_3;

public Test() {

JFrame f = new JFrame("WSDP"); // creating instance of JFrame#
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JScrollPane scrollPane = new JScrollPane();

table_3 = new JTable(myTimes, colTimes);
table_3.setFillsViewportHeight(true);
// This is dangerous and is likely to come back and haunt you
table_3.setRowHeight(20);
table_3.setFont(new Font("Courier", Font.BOLD, 14));

// THIS is what's missing
scrollPane.setViewportView(table_3);

f.getContentPane().add(scrollPane);

f.pack();
f.setVisible(true);

}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
new Test();
}
});
}

}

我还建议阅读Laying Out Components Within a Container因为它将解决您将面临的许多其他问题

关于java - 滚动 Pane 垂直滚动条不显示滚动旋钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54475242/

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