-1

我有一个JFrame带有 3 个容器的JPanel容器,一个在顶部,另一个在左侧,最后一个在中间,占据了框架的其余部分。问题是当我尝试SpringLayout为侧面板设置 a 时,它不会显示在面板上。框架具有默认的边框布局。

public final class board extends JFrame {

    public final JPanel top = new JPanel();
    public final JPanel side = new JPanel();
    public final JPanel center = new JPanel();

    public board(){

        initComponents();
        initWindow();

    }

    public void initComponents(){

        Font font = new Font("HelvLight", Font.PLAIN, 20);
        Font fontT = new Font("Century Gothic", Font.BOLD, 30);
        SpringLayout layoutT = new SpringLayout(); 
        JSeparator sep = new JSeparator();
        JLabel title = new JLabel("TITLE");
        JLabel calendar = new JLabel("Calendar");
        JButton settings = new JButton("C"); //Add Icon

        title.setFont(fontT);
        calendar.setFont(font);
        title.setForeground(Color.WHITE);
        calendar.setForeground(Color.WHITE);
        sep.setBackground(Color.white);
        sep.setForeground(Color.white);

        //layoutT.putConstraint(SpringLayout.WEST, calendar, 50, SpringLayout.EAST, center);

        top.setLayout(new BorderLayout());
        top.add(settings, BorderLayout.EAST);

        top.add(title, BorderLayout.CENTER); // Not centered
        top.setBackground(Color.decode("#4C004C"));

        center.setBackground(Color.green);

        side.setBackground(Color.decode("#0f001e"));
        side.setLayout(layoutT);
        side.add(calendar, SpringLayout.SOUTH);
        side.add(sep);
    }

    public void initWindow() {
        Toolkit tk = Toolkit.getDefaultToolkit();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize((int)  tk.getScreenSize().getWidth() ,
                (int)  tk.getScreenSize().getHeight()
              );
        //setResizable(false);

        add(top, BorderLayout.NORTH);
        add(side, BorderLayout.WEST);
        add(center, BorderLayout.CENTER);

        pack();
        setVisible(true);
    }
}

这就是它应该为我工作的方式,但事实并非如此。

4

1 回答 1

1

问题是当我尝试为侧面板设置 SpringLayout 时,它不会显示在面板上。

SpringLayout是一个复杂的布局管理器。

    side.setLayout(layoutT);
    side.add(calendar, SpringLayout.SOUTH);

您没有正确使用它。

阅读 Swing 教程中有关如何使用 SpringLayout的部分以获取工作示例。

但是,根据提供的代码,您可能可以使用其他标准布局管理器之一。也许aBoxLayout会更容易。

于 2016-12-15T15:39:18.093 回答