簡體   English   中英

使用GridBagLayout的JPanel的Java限制寬度

[英]Java Restraining width for JPanel with GridBagLayout

我寫了一個程序來生成PDF表。 為了使操作更容易,我還創建了一個GUI來控制事物。 但是,確實讓我感到困擾的是,在JFrame上,此JComboBox占用了大量空間,從而使正確的subPanel不必要地變寬了。

原因可能是comboBox中有一個很長的字符串

我嘗試為面板使用“設置首選大小”,但是沒有用。 然后,我繼續使用GridBagLayout嘗試縮小該組合框。 有什么方法可以將所有按鈕和comboBox限制為一定的寬度嗎? 還是應該使用其他布局?

最好是,該右側面板中的所有組件都可以具有相同的寬度,就像現在一樣。

謝謝!

在此處輸入圖片說明

這是我的代碼:

JTable studentTable=new JTable(new StudentTableModel(bro));
            studentTable.setRowHeight(25);
            studentTable.setGridColor(java.awt.Color.BLACK);
            DefaultTableCellRenderer centerRenderer = new     
DefaultTableCellRenderer();
            centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

studentTable.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);

studentTable.getColumnModel().getColumn(3).setCellRenderer(centerRenderer);

studentTable.getColumnModel().getColumn(5).setCellRenderer(centerRenderer);
        JPanel panelA1= new JPanel();
            panelA1.setOpaque(false);
            panelA1.setAlignmentX(Component.LEFT_ALIGNMENT);
            panelA1.setLayout(new GridBagLayout());

        JScrollPane scrollPaneA= new JScrollPane(studentTable);

        JButton buttonA1= new JButton("Activate All");

        JButton buttonA2= new JButton("Create Tables");

        JButton buttonA3= new JButton("Save File");

        JLabel labelA1= new JLabel("-Job Crews-");

        JButton buttonA4= new JButton("Deact Crew");

        JComboBox<Object> comboBoxA1=new JComboBox<Object>();
        comboBoxA1.addItem(new String("-N/A-"));
        for(JobCrew jj: crews)
        {
            comboBoxA1.addItem(jj);
        }
        comboBoxA1.setMaximumSize(new Dimension(100,100));

        GridBagConstraints c = new GridBagConstraints();
        {
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            panelA1.add(buttonA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 1;
            panelA1.add(buttonA2,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 2;
            panelA1.add(buttonA3,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 3;
            panelA1.add(labelA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 4;
            panelA1.add(comboBoxA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 5;
            panelA1.add(buttonA4,c);

        }

        JPanel panelA= new JPanel();
            panelA.setLayout(new BoxLayout(panelA,BoxLayout.X_AXIS));
            panelA.setOpaque(false);
            panelA.add(scrollPaneA); panelA.add(panelA1);
        //////
        /**********/
        tabs.addTab("Student info",null,panelA,"Organizes & displays student information");

看看組合框彈出窗口

在此處輸入圖片說明

它將允許您控制組合框本身的寬度,並且彈出窗口可以是完整寬度。

或者,可以設置屬性,以便在需要時在彈出窗口中顯示滾動條。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM