horizontal desplazamiento codigo barra java swing jframe jscrollpane

java - codigo - Crear barra de desplazamiento para JFrame



jtable scroll horizontal netbeans (1)

Hola, estoy intentando crear barra de desplazamiento para mi JFrame. Creé el objeto JPanel y establecí los componentes en JPanel. Luego creó un objeto JScrollPane para el panel. A continuación, agregue el objeto ScrollPane a JFrame. No estoy viendo ninguna barra de desplazamiento. También me pregunto si existe una opción en JPanel que redimensione el objeto dentro de Jpanel automáticamente de acuerdo con el nivel de zoom de JPanel. Cualquier ayuda sería muy apreciada.

clase pública xmlottgui {

private JPanel Container; private JFrame frmCreateXml; private JTextField titlename; private JLabel lbltitlename; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { xmlottgui window = new xmlottgui(); window.frmCreateXml.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public xmlottgui() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { Container = new JPanel(); Container.setLayout(null); //JScrollPane pane=new JScrollPane(Container,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); frmCreateXml = new JFrame(); frmCreateXml.setTitle("Create XML"); frmCreateXml.setBounds(100, 100, 1000, 1200); frmCreateXml.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmCreateXml.getContentPane().setLayout(null); //Create MenuBar JMenuBar menuBar = new JMenuBar(); Container.add(menuBar); JMenu mnFile = new JMenu("File"); menuBar.add(mnFile); JMenuItem mntmImportFromCsv = new JMenuItem("Import From Excel File"); //Add menu item Exit JMenuItem mntmexit = new JMenuItem("Exit"); mntmexit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); mnFile.add(mntmexit); showform(); JScrollPane pane=new JScrollPane(Container,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); pane.setLayout(null); frmCreateXml.setContentPane(pane); frmCreateXml.getContentPane().add(pane); } private void showform(){ titlename = new JTextField(); titlename.setBounds(164, 27, 749, 26); Container.add(titlename); titlename.setColumns(10); lbltitlename = new JLabel("Title Name"); lbltitlename.setBackground(Color.GRAY); lbltitlename.setBounds(22, 1000, 90, 16); Container.add(lbltitlename); }


Esta:

pane.setLayout(null);

Deshabilitará por completo su JScrollPane y evitará que funcione, ya que evitará que JScrollPane muestre y manipule correctamente su puerto de visualización. JScrollPanes tiene su propio administrador de diseño muy especial, uno con el que nunca querrás pasar a menos que seas muy inteligente y sepas lo que estás haciendo. Como regla general, casi nunca deberías usar diseños nulos.

Además, esto no es correcto:

frmCreateXml.setContentPane(pane); frmCreateXml.getContentPane().add(pane);

Usted hace que el panel sea el panel de contenido y luego agrega el panel a sí mismo.

Y ESTO te está ensuciando:

frmCreateXml.getContentPane().setLayout(null);

Querrá conocer y utilizar los administradores de diseño, ya que hará que su vida sea mucho más fácil.