JPanel中的组件不会自动换行


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;
public class FileCutter
{
    public static void main(String[] args)
    {
        CutterWindow cw = new CutterWindow();
        cw.setResizable(false);
        cw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cw.setVisible(true);
    }
}
class CutterWindow extends JFrame
{
    private JTabbedPane tabbedPane = new JTabbedPane();
    public static final int DEFAULT_WIDTH = 470;
    public static final int DEFAULT_HEIGHT = 480;
    public CutterWindow()
    {
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        tabbedPane.add("File Cut",new FileCutPanel());
        this.add(tabbedPane);
    }
}
class FileCutPanel extends JPanel
{   
    private JLabel lblFileName =  new JLabel("File Name:");
    private JTextField txtFileName = new JTextField();
    private JLabel lblFileSize = new JLabel("File Size:");
    private JTextField txtFileSize = new JTextField();
    private JButton btnViewFiles = new JButton("...");
    private JPanel panelSelectOperatingFile = new JPanel();
    public FileCutPanel()
    {   
        panelSelectOperatingFile.setLayout(new FlowLayout());
        panelSelectOperatingFile.add(lblFileName);
        txtFileName.setColumns(20);
        txtFileName.setEditable(false);
        panelSelectOperatingFile.add(txtFileName);
        panelSelectOperatingFile.add(btnViewFiles);
        panelSelectOperatingFile.add(lblFileSize);
        panelSelectOperatingFile.add(txtFileSize);
        txtFileSize.setColumns(20);
        txtFileSize.setEditable(false);
        this.add(panelSelectOperatingFile);
    }
}

的结果是,在panelSelectOperatingFile面板中的五个组件在同一行,超过窗口的宽度。我知道JPanel的默认布局是flowlayout,我希望当上一行没有空格时,组件将放在下一行。我试着找出问题,但是我不能

不要使用panelSelectOperatingFile面板。只需将您的组件添加到您正在扩展的JPanel

你不能嵌套FlowLayouts。原因如下:

Oracle FlowLayout教程

相关内容

  • 没有找到相关文章

最新更新