0

所以对于这个程序,我想JToolBar在左边有一个,在右边有这个频谱面板。我目前正在使用 a 添加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);
    }

}
4

1 回答 1

0

我建议您进行以下更改。

main.add(rightside, BorderLayout.EAST);

main.add(rightside, BorderLayout.CENTER);
于 2013-05-20T05:58:58.373 回答