【问题标题】:How do I move a table to the left if it is in a JPanel?如果表格在 JPanel 中,如何将表格向左移动?
【发布时间】:2021-11-03 23:09:00
【问题描述】:

我正在尝试将表格移动到 JPanel 的左侧,它也在窗口内

我得到的是:

我想要的是这个:

我尝试使用.setBounds 方法,但它不起作用。有谁知道该怎么做?

这是我的代码:

package Modulos;
import paneles.*;
import javax.swing.*;

public class ModuloAdmin extends JFrame {

    public ModuloAdmin(){
        //Crear tabla
        String[][] datosprueba = {
                {"001","Carlitos", "Casa","51202011"},
                {"001","Carlitos", "Casa","51202011"}
        };

        String[] TituloColumna = {"Código", "Nombre", "Dirección", "Teléfono"};
        JTable TablaSucursales = new JTable(datosprueba,TituloColumna);

        //Contenido de las pestañas
        JPanel PanelSucursales = new JPanel();

        PanelSucursales.add(TablaSucursales);
        PanelSucursales.add(new JScrollPane(TablaSucursales));
        JPanel PanelProductos = new JPanel();
        PanelProductos.add(new JLabel("Panel productos"));
        JPanel PanelClientes = new JPanel();
        PanelClientes.add(new JLabel("Panel Clientes"));
        JPanel PanelVendedores = new JPanel();
        PanelVendedores.add(new JLabel("Panel Vendedores"));

        //CREACION DE PESTAÑAS
        JTabbedPane Pestanias = new JTabbedPane();
        Pestanias.setBounds(20,80,700,700);
        Pestanias.add("Sucursales",PanelSucursales);
        Pestanias.add("Productos",PanelProductos);
        Pestanias.add("Clientes", PanelClientes);
        Pestanias.add("Vendedores",PanelVendedores);

        //vENTANA MODULO DE ADMIN
        JFrame VentanaModuloAdmin = new JFrame();
        VentanaModuloAdmin.add(Pestanias);
        VentanaModuloAdmin.setLayout(null);
        VentanaModuloAdmin.setSize(800,850);
        VentanaModuloAdmin.setLocationRelativeTo(null);
        VentanaModuloAdmin.setTitle("Módulo de administrador");
        VentanaModuloAdmin.setVisible(true);
        VentanaModuloAdmin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

【问题讨论】:

    标签: java swing jpanel layout-manager


    【解决方案1】:

    使用appropriate layout mangers 和容器管理

    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.border.EmptyBorder;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JTable table;
    
            public TestPane() {
                setBorder(new EmptyBorder(16, 16, 16, 16));
                setLayout(new GridLayout(0, 2));
                add(new JScrollPane(table));
                add(createButtonPane());
            }
    
            protected JPanel createButtonPane() {
                JPanel panel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.ipadx = 16;
                gbc.ipady = 16;
                gbc.fill = gbc.HORIZONTAL;
                gbc.gridx = 0;
                gbc.gridy = 0;
    
                panel.add(new JButton("Clear"), gbc);
                gbc.gridx = 1;
                panel.add(new JButton("Carga Masiva"), gbc);
    
                gbc.gridy++;
                gbc.gridx = 0;
                panel.add(new JButton("Actualizar"), gbc);
                gbc.gridx = 1;
                panel.add(new JButton("Eliminar"), gbc);
    
                gbc.gridy++;
                gbc.gridx = 0;
                gbc.gridwidth = 2;
                gbc.anchor = GridBagConstraints.NORTH;
                gbc.weighty = 1;
                panel.add(new JButton("Exportar Listado a PDF"), gbc);
    
                return panel;
            }
    
    
        }
    
    }
    

    说真的,布局管理器解决了 UI 开发人员面临的最困难的问题之一 - 输出的可变性质(屏幕分辨率、字体指标、可访问性修饰符,一大堆不同的东西会改变需要计算布局的方式)。

    它们可能看起来很难开始,但是一旦你习惯使用它们并学习如何使用上面演示的“复合布局”,它将帮助你更快地制作更好的 UI

    【讨论】:

      猜你喜欢
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2020-05-13
      • 1970-01-01
      • 2013-12-24
      • 2014-06-10
      相关资源
      最近更新 更多