调整GUI JPanel以适应

时间:2013-05-20 05:10:19

标签: java swing jframe jpanel layout-manager

所以对于这个程序,我试图在左边有一个JToolBar,在右边有这个频谱面板。我目前正在使用BorderLayout添加,但正如您所看到的,我添加的光谱(青色)周围有一个黑色边框(下面的面板)。为什么它没有填补右侧的JPanel?

http://imgur.com/pTqMeGM

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class Spectrum extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /* 
     * 
     */

    public Spectrum(){
        JPanel main = new JPanel(new BorderLayout());
        JPanel rightside = new JPanel();
        JLabel spectrum = new JLabel("spectrum goes here");
        JToolBar toolbar = new JToolBar(null, JToolBar.VERTICAL);
        JButton button1 = new JButton("Icon 1");
        JButton button2 = new JButton("Icon 2");

        main.setBackground(Color.RED);
        main.setPreferredSize(new Dimension(800, 500));

        rightside.setBackground(Color.black);

        spectrum.setPreferredSize(new Dimension(750,500));
        spectrum.setOpaque(true);
        spectrum.setBackground(Color.cyan);

        toolbar.setPreferredSize(new Dimension(50, 500));
        toolbar.setFloatable(false);

        button1.setOpaque(true);
        button2.setOpaque(true);
        button1.setBackground(Color.blue);
        button2.setBackground(Color.green);

        toolbar.add(button1);
        toolbar.add(button2);

        rightside.add(spectrum);
        main.add(toolbar, BorderLayout.WEST);
        main.add(rightside, BorderLayout.EAST);

        setContentPane(main);

        pack();
        setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:0)

我建议你做以下改变。

main.add(rightside, BorderLayout.EAST);

main.add(rightside, BorderLayout.CENTER);