Java调整JPanel各个组件的大小 [英] Java resizing individual components of JPanel

查看:138
本文介绍了Java调整JPanel各个组件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JPanel with Box.createVerticalBox() layout containing five JPanels. (1) Labels, (2) a table (3) a JTextField (4) a JTextArea (5) buttons.On resize:

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

labels should stick to top left corner and keep the same size,
JTextField should stick to left size between (2) and (4) and expand to full width of the frame
Buttons should stick to bottom right corner and keep the same size,
JTable and JTextArea should expand to full width of the frame and equally divide remaining space

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

I've tried several layouts, but couldn't make resizing work. To run this program two classes are required 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

And the 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可以调整大小吗?

Which Layout handles resizing the best? Can boxlayout handle resizing?

推荐答案

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

GridBagLayout is the best layout manager for your app. See https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

这篇关于Java调整JPanel各个组件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆