gpt4 book ai didi

java - 我添加了滚动条,但它不会在框架中上下滚动

转载 作者:行者123 更新时间:2023-11-30 08:16:51 25 4
gpt4 key购买 nike

我添加了滚动条,但它不会在框架中上下滚动,而且我没有看到我创建的上部标签。请给我任何建议,以便我可以在框架中上下滚动。我还有一个问题,当我的框架大小固定并且我想创建许多标签和文本字段我必须向下滚动,因此在这种情况下我必须增加其框架大小或不增加其框架大小

 public static void main(String[] args) 
{
JFrame frame = new JFrame("jframe");
JLabel[] labels=new JLabel[50];

for (int i=0;i<labels.length;i++)
{
labels[i]=new JLabel("Column" + i);
}


JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cst = new GridBagConstraints();
JScrollBar vbar=new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 500);

for(int i =0 ; i<50 ;i++)
{

cst.fill = GridBagConstraints.HORIZONTAL;
cst.gridx = 0;
cst.gridy = i;//
cst.gridwidth = 2;
panel.add(labels[i],cst);

}




frame.getContentPane().add(vbar, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1300,700);
frame.getContentPane().add(panel);
frame.setVisible(true);
}

最佳答案

为什么不使用 JScrollPane 并将面板放在上面?看看:https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

public static void main(String[] args)
{
JFrame frame = new JFrame("jframe");
JLabel[] labels = new JLabel[50];

for(int i = 0; i < labels.length; i++)
{
labels[i] = new JLabel("Column" + i);
}

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cst = new GridBagConstraints();
// JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL,30,40,0,500);

for(int i = 0; i < 50; i++)
{

cst.fill = GridBagConstraints.HORIZONTAL;
cst.gridx = 0;
cst.gridy = i;//
cst.gridwidth = 2;
panel.add(labels[i],cst);

}

JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1300,700);
frame.setVisible(true);
}

关于java - 我添加了滚动条,但它不会在框架中上下滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29535135/

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