Java 将JScrollPane添加到JFrame
正在完成我的最后一个课程项目,我希望在需要的时候可以在我的JFrame中添加一个滚动条 我希望它只在JFrame上的JButtons开始消失时才出现,但我首先需要一个任何类型的工作滚动条。感谢您的帮助 以下是迄今为止我为GUI构造函数编写的代码:Java 将JScrollPane添加到JFrame,java,jframe,scrollbar,Java,Jframe,Scrollbar,正在完成我的最后一个课程项目,我希望在需要的时候可以在我的JFrame中添加一个滚动条 我希望它只在JFrame上的JButtons开始消失时才出现,但我首先需要一个任何类型的工作滚动条。感谢您的帮助 以下是迄今为止我为GUI构造函数编写的代码: public GUI() { JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBA
public GUI()
{
JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
initComponents();
setLocationRelativeTo(null);
setResizable(true);
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
posX=evt.getX();
posY=evt.getY();
}
});
this.addMouseMotionListener(new MouseAdapter()
{
public void mouseDragged(MouseEvent evt)
{
setLocation (evt.getXOnScreen()-posX,evt.getYOnScreen()-posY);
}
});
}
和主菜单:
public static void main(String args[])
{
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new GUI().setVisible(true);
}
});
}
publicstaticvoidmain(字符串参数[])
{
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable())
{
公开募捐
{
新建GUI().setVisible(true);
}
});
}
我不知所措。我阅读了Oracle的滚动窗格教程,但收效甚微
非常感谢您的帮助
JPanel contain = new JPanel();
container.add(panel1);
JScrollPane obj = new JScrollPane(contain);
frame.add(obj);
希望这对JScrollPane有所帮助内容在哪里添加到JScrollPane,JScrollPane在哪里添加到UI?我想我需要以某种方式将initComponents()中的所有组件添加到滚动窗格中。我怎么做
我怎么做
我不知道是什么initComponents
,因此无法具体评论,但我假设其中有一个主JPanel,您可以添加到JScrollPane。另请参见@FarrukhinitComponent()的答案是NetBeans自动创建的用于添加按钮、标签等的无效方法。NetBeans自动创建以添加按钮、标签等。
如果您是Swing新手,我建议您学习如何手工构建UI。您应该能够将@Farrukh提供的内容调整到initComponents方法中。由于没有看到更多的代码,很难给出具体的答案