【问题标题】:Java resizing individual components of JPanelJava 调整 JPanel 的各个组件的大小
【发布时间】:2016-05-19 22:20:56
【问题描述】:

我有一个带有Box.createVerticalBox() 布局的 JPanel,其中包含五个 JPanel。 (1) 标签,(2) 表格 (3) JTextField (4) JTextArea (5) 按钮。调整大小时:

标签应贴在左上角并保持相同大小,
JTextField 应保持在 (2) 和 (4) 之间的左侧大小,并扩展到框架的全宽
按钮应固定在右下角并保持相同的大小,
JTable 和 JTextArea 应该扩展到整个框架的宽度并平均分配剩余空间

我尝试了几种布局,但无法调整大小。 要运行这个程序,需要两个类 EditPanel.java :

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class EditPanel extends JPanel {
    private JPanel p1Labels;
    private JPanel p2Table;
    private JPanel p3ecnTitle;
    private JPanel p5Buttons;    
    private JTextField fieldK;
    private JTextField fieldS;
    private JScrollPane myScrollBar;
    private Box theBox;

    public EditPanel() {
        init();
    }

    public void init() {  // Creating a vertical Box layout with five sections, placing jpanels there

        //First panel with buttons

        p1Labels = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
        fieldK = new JTextField("Animal");
        fieldS = new JTextField("Fox");        
        p1Labels.add(new JLabel("Kindom: "));
        p1Labels.add(fieldK);
        p1Labels.add(new JLabel("Species: "));
        p1Labels.add(fieldS);

        //Second panel with a table

        p2Table = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
        String[] columnNames = {"First", "Second", "Third", "Fourth"};
        Object[][] data = {{"11", "12", "13", "Forteen"},{"21", "22", "23", "Twenty four"}}; 
        JTable table = new JTable(data, columnNames);
        JScrollPane scrollPane1 = new JScrollPane(new JTable(data, columnNames));
        table.setFillsViewportHeight(true);
        p2Table.add(scrollPane1, BorderLayout.CENTER);

        //Third panel with a JTextField

        p3ecnTitle = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2)); 
        p3ecnTitle.add(new JLabel("Title: "));
        p3ecnTitle.add(new JTextField("", 14));

        //Forth panel with JTextArea
        //p4TextArea = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));//tried this too
        JTextArea ecnArea = new JTextArea(10, 20);        
        ecnArea.setText("");
        ecnArea.setName("Note");
        ecnArea.setLineWrap(true);
        ecnArea.setWrapStyleWord(true);
        myScrollBar = new JScrollPane(ecnArea,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);  

        //Fifth container with buttons

        p5Buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 2));
        p5Buttons.add(new JButton("SAVE"));
        p5Buttons.add(new JButton("DELETE"));
        p5Buttons.add(new JButton("CANCEL")); 

        //Placing everything in a container
        theBox = Box.createVerticalBox();
        theBox.add(p1Labels);
        theBox.add(p2Table);
        theBox.add(p3ecnTitle);
        //theBox.add(p4TextArea);
        theBox.add(myScrollBar);
        theBox.add(Box.createVerticalGlue());
        theBox.add(p5Buttons);
        this.add(theBox);
    }
}

还有main.java

import javax.swing.JFrame;
public class Main {
    public static void main(String[] args) {
        JFrame myFrame;
        myPanel EditECNDialog;
        myFrame = new JFrame();     
        EditECNDialog = new myPanel();
        myFrame.setTitle("Notes");
        myFrame.add(EditECNDialog);
        myFrame.pack();
        myFrame.setLocationRelativeTo(null);
        myFrame.setVisible(true);
    }
}

哪种布局最适合调整大小? boxlayout 可以处理大小调整吗?

【问题讨论】:

    标签: java swing jpanel window-resize


    【解决方案1】:

    GridBagLayout 是您应用的最佳布局管理器。见https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

    【讨论】:

    • 我知道提醒,但是如何设置布局以便在两个组件(JTextArea 和 JTable)之间分割空间?
    • 为两个组件的 weightx 赋予相同的值 0.5。
    • (1+) 由于这是一个垂直布局,您可以将两个组件的 weighty 值设为 0.5。另请注意,weighty 仅用于调整框架大小时的“额外”空间。也就是说,如果您希望表格和文本区域的大小相同,那么它们也必须具有相同的首选高度,因为 GridBagLayout 首先根据首选大小分配空间,然后将额外空间分配给 weightx 值。
    【解决方案2】:

    您可以按照 Wabbi 的建议使用 GridBagLayout。但是确实有我在 cmets 中提到的限制。

    但是,如果您真的希望 textarea 和 table 始终保持相同大小,则可以使用 Relative Layout。此布局将首先为具有固定大小的组件分配空间。然后将剩余的任何空间分配给具有相对约束的组件。

    所以基本代码是:

    RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
    rl.setFill( true );
    JPanel panel = new JPanel( rl );
    panel.add(labelPanel);
    panel.add(tableScrollPane, new Float(1));
    panel.add(textField);
    panel.add(textAreaScrollPane, new Float(1)); 
    panel.add(buttonsPanel);
    

    现在表格和文本区域将随着框架大小的调整而平等地增长/缩小。

    boxlayout 可以处理大小调整吗?

    是的,它可以处理这种类型的调整大小。盒子布局尊重组件的最大尺寸。因此,如果您覆盖面板的getMaximumSize() 方法以返回getPreferredSize(),那么面板将不会增加高度。

    因此,文本区域和表格的滚动窗格将获得额外的空间。再次,同样的担忧。每个组件最初将根据其首选大小分配空间。

    【讨论】:

      【解决方案3】:

      您应该为 gridbagconstraints 使用一个变量。这样你就可以做到c.gridy++; c.gridx=0;c.gridx++;
      以后插入新组件会更容易。 显式 d.gridy=4; 使插入新组件变得困难。

      【讨论】:

        猜你喜欢
        • 2011-09-22
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 2013-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多