【问题标题】:JScrollPane adding items Horizontally, but I want them added verticallyJScrollPane 水平添加项目,但我希望它们垂直添加
【发布时间】:2014-11-08 11:32:26
【问题描述】:

我正在创建一个想要上下滚动的定制表格,所以我使用 JScrollPane。

我的问题是,每次我在表格中添加另一行时,它都会将其添加到最后一行的一侧,而不是最后一行的下方,这是我想要的位置。例如,它向表中添加列而不是行。

需要添加的行数不确定,取决于表中有多少条目。

我知道我可以使用表格,但出于本练习的目的,我想这样做。

下面是我的代码的 MCVE 版本,它演示了这个问题。我保留了颜色变化,因此字段更明显:

public class Customers {


     public static JFrame frame = new JFrame();

     public static void frameGui(JPanel panel, String name){

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setContentPane(panel);

            frame.setSize(1200,500);

            frame.setVisible(true);
     }



     public static void ScrollCustomersGui(){           

            JPanel Table = new JPanel();

            Table.add(customersTableHeadings(Table));
            Table.add(customersTableHeadings(Table));  
            Table.add(customersTableHeadings(Table));  

            JScrollPane scroll = new JScrollPane(Table);

            JPanel All = new JPanel(new BorderLayout());
            All.add(scroll);

            frameGui(All, "Customers");
        }

    public static JPanel customersTableHeadings(JPanel panel){

         FlowLayout FlowCustTable = (FlowLayout)panel.getLayout();
         FlowCustTable.setHgap(0);

         JPanel customersTable = new JPanel(FlowCustTable);

         JTextField custid = new JTextField("ONE");
         custid.setPreferredSize(new Dimension(500, 50));
         custid.setBackground(Color.CYAN);

         customersTable.add(custid);

         return customersTable;

    }





}

【问题讨论】:

    标签: java user-interface jpanel jscrollpane


    【解决方案1】:

    这里使用垂直BoxLayout是修改后的代码:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    
    public class Customers
    {
    
        public static JFrame frame = new JFrame();
    
        public static void frameGui(JPanel panel, String name)
        {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(panel);
            frame.setSize(1200, 500);
            frame.setVisible(true);
        }
    
        public static void ScrollCustomersGui()
        {
            Box box = Box.createVerticalBox();
            //JPanel Table = new JPanel();
            //Table.setLayout(new BoxLayout(Table, BoxLayout.Y_AXIS));
    
            box.add(customersTableHeadings());
            box.add(customersTableHeadings());
            box.add(customersTableHeadings());
    
            JScrollPane scroll = new JScrollPane(box);
    
            JPanel All = new JPanel(new BorderLayout());
            All.add(scroll);
    
            frameGui(All, "Customers");
        }
    
        public static JPanel customersTableHeadings()
        {
            // FlowLayout FlowCustTable = (FlowLayout) panel.getLayout();
            // FlowCustTable.setHgap(0);
    
            JPanel customersTable = new JPanel();
            customersTable.setMaximumSize(new Dimension(550, 60));
    
            JTextField custid = new JTextField("ONE");
            custid.setPreferredSize(new Dimension(500, 50));
            custid.setBackground(Color.CYAN);
    
            customersTable.add(custid);
    
            return customersTable;
    
        }
    
        public static void main(String[] args)
        {
            ScrollCustomersGui();
        }
    }
    

    【讨论】:

    • 如何去除行间的空格。我以前使用过 setHgap(0)
    • @jim BoxLayout 使用它所包含的组件的最大尺寸,因此要最小化添加组件之间的间隙,您应该使用新添加面板的 setMaximumSize(Dimension)。 (注意:setMaximumSize 已在给定的修改代码中使用,请参阅方法customersTableHeadings)
    【解决方案2】:

    您使用的 FlowLayout 不适用于您的情况,您可以使用 BoxLayout 检查它here,您也可以使用 GridBagLayout,但它很复杂 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-24
      • 2016-10-11
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多