如何在JPanel中的溢出时启用滚动条?

时间:2019-05-28 08:33:28

标签: java swing scrollbar

我不想以表格格式显示数据库中的数据,因此我尝试了类似您在代码中看到的方法。每次从数据库中检索记录时,我已经添加了新的JScrollPane容器,其中包含按钮和标签。但是由于大量的数据,面板正在溢出,但是没有启用滚动选项。

我尝试使用public class Test2 extends javax.swing.JFrame { private Connection con; private Statement st; private ResultSet rt; public int PageId = 8236; /** * Creates new form Test2 */ public Test2() { initComponents(); this.setLocationRelativeTo(null); try{ Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521","practice","perfect"); st = con.createStatement(); int height = 100; int width = sc.getWidth(); int x = 0; int y = 0; ResultSet rs=st.executeQuery("select id, firstname || ' ' || lastname, salary from employee"); while(rs.next()){ JPanel g = new JPanel(); g.setBounds(x, y, width, height); g.setBackground(Color.yellow); JLabel l = new JLabel(rs.getString(1)); JLabel l1 = new JLabel(rs.getString(2)); JLabel sal = new JLabel(rs.getString(3)); JButton b = new JButton("View profile"); b.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { JOptionPane.showMessageDialog(sc, "Clicked!!"); } }); g.add(l); g.add(l1); g.add(b); sc.add(g); y += (height + 10); } } catch(Exception e){ JOptionPane.showMessageDialog(this, "Can't connect", "connection error", JOptionPane.ERROR_MESSAGE); System.out.println(e.getMessage()); } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { sc = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); sc.setAutoscrolls(true); javax.swing.GroupLayout scLayout = new javax.swing.GroupLayout(sc); sc.setLayout(scLayout); scLayout.setHorizontalGroup( scLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 252, Short.MAX_VALUE) ); scLayout.setVerticalGroup( scLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 483, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(sc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(sc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { 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(Test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Test2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Test2.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 Test2().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JPanel sc; // End of variables declaration } 了很多次,但是没有一种方法可以帮助我。

string Query = "delete from Customers where CustomerName = '"+ dgv_CustomerDetails.CurrentCell(rowIndex).value +"'";

1 个答案:

答案 0 :(得分:0)

  

我不想以表格格式显示数据库中的数据

为什么?根据您提供的代码,输出看起来很糟糕。您基于文本创建多个标签。这意味着每个标签的大小可以不同,这意味着每个标签在多行上的布局将是随机且不对齐的。

如果要在表格列中单击按钮,请签出:Table Button Column

  

面板正在溢出,但是没有启用滚动选项。

sc.setAutoscrolls(true);

这不是向组件添加滚动的方式。该组件需要添加到JScrollPane中:

JPanel scrollingPanel = new JPanel();
scrollingPanel.setLayout(...);
scrollingPanel.add(...);
scrollingPanel.add(...);

getContentPane.add(new JScrollPane(scrollingPanel), BorderLayout.CENTER);
pack();

注意,我从未使用过SpringLayout。我建议您学习如何手动创建GUI的方法,这样就不必依赖IDE来生成代码。将会更容易理解和维护代码。