在JFrame上放置JPanel是不正确的

时间:2014-07-02 19:28:06

标签: java swing user-interface awt layout-manager

我编写了一个程序,使用swing / awt框架为我的作业编写GUI。到目前为止,我能够将各个部分组合在一起,但是当我将它们全部放入JFrame时,它们并没有像预期的那样出现。

我最近开始研究Java GUI框架,并且不确定我的代码中缺少什么。我怎样才能正常工作?

我还附上我得到的输出的屏幕截图(见底部)。

public class MainFrame extends JFrame {

    public MainFrame() {
    addComponentsToPane(this.getContentPane());
    }

    private void addComponentsToPane(Container pane) {

        // Set layout
        GridBagConstraints gbc = new GridBagConstraints();
        this.setTitle("Test tool");
        this.setSize(600, 650);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(2, 1));

        // Add video JComponent
        mMainPanel = new MainPanel();
        pane.add(mMainPanel, 0);

        // Add conference screen panel
        mFeedPanel = new FeedPanel();
        pane.add(mFeedPanel, 1);

        // Add a button panel
        mButtonPanel = new ButtonPanel();
        pane.add(mButtonPanel, 2);

        this.setResizable(true);
        this.setVisible(true);
        this.pack();
    }
}

// In actual output, there is 1 screen in this panel. 
// mScreen1 is derived from JComponent object.
public class MainPanel() extends JPanel {

    public MainPanel() {
    addMainPanelComponents();
    }

    private void addMainPanelComponents() {
        this.setSize(352, 240);
        setBackground(Color.yellow);
        setLayout(new GridLayout(1, 2));
        add(mScreen);
        setVisible(true);
    }
}

// In actual output, there are 3 screens in this panel. I have shown code for 1 screen only
// mScreen1 is derived from JComponent object.
public class FeedPanel extends JPanel {

    public FeedPanel() {
    addFeedPanelComponents();
    }

    private void addFeedPanelComponents() {

        String img1 = "images/screen1.png";

        setSize(352, 150);
        setBackground(Color.yellow);
        setLayout(new GridLayout(1, 3));                

        Image image1 = ImageIO.read(new File(img1));
        mScreen1.setImage(image1);
        add(mScreen1);
        setVisible(true);
    }
}

public class ButtonPanel extends JPanel {

    public ButtonPanel() {
    addButtonPanelComponents();
    }

    private void addButtonPanelComponents() {

        this.setSize(352, 150);
        this.setBackground(Color.yellow);
        this.setLayout(new GridLayout(1, 
                                      5));

        // Add Button to panel
        mStartButton = new JButton("Start");
        this.add(mStartButton);
        mStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                StartButtonActionListener(ae);
            }
        });

        mStopButton = new JButton("Stop");
        this.add(mStopButton);
        mStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                StopButtonActionListener(ae);
            }
        });
        setVisible(true);
    }
}

默认情况下会运行代码。

enter image description here

手动调整框架大小后会出现这种情况。 enter image description here

2 个答案:

答案 0 :(得分:3)

试试这个:

    public class Main{

    private JFrame f;
    private JLabel l1, l2, l3,l4;
    private JPanel p1, p2, p3;
    private JButton b1, b2, b3;

    public Main(){

    this.f = new JFrame();
    this.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.f.setLayout(new GridLayout(3,1));

    this.p1 = new JPanel();
    this.p1.setLayout(null)
    this.p1.setSize(yoursize);

    this.l1 = new JLabel();
    this.l1.setBounds(x,y,xspan,yspan);

    this.p1.add(l1);

    this.p2 = new JPanel();
    this.p2.setLayout(new GridLayout(1,3));

    this.l2 = new JLabel();
    this.l3 = new JLabel();
    this.l4 = new JLabel();

    this.p2.add(l2);
    this.p2.add(l3);
    this.p2.add(l4);

    this.p3 = new JPanel();
    this.p3.setLayout(new GridLayout(1,3));

    this.b1 = new JButton();
    this.b2 = new JButton();  
    this.b3 = new JButton();

    this.p3.add(b1);       
    this.p3.add(b2);
    this.p3.add(b3);

    this.f.add(p1);
    this.f.add(p2);
    this.f.add(p3);

    this.f.pack();
    this.f.setResizeable(false)
    }}

添加视频组件而不是标签,您可以根据需要更改组件的颜色。 此外,如果您想要更多地控制组件的大小和位置,请使用null布局并使用setBounds()函数单独放置它们,如上面的程序所示。这肯定是耗时的,但使布局更加完美。

答案 1 :(得分:3)

BorderLayout,GirdLayout和BoxLayout的组合可以为你做到这一点(实际上它不是唯一的选择)。

以下是代码:

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GridLayoutTest {
    public void createUI(){
        JFrame frame = new JFrame();

        JPanel topPanel = new TopPanel();
        JPanel buttomPanel = new ButtomPanel();


        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(topPanel,BorderLayout.CENTER);
        mainPanel.add(buttomPanel,BorderLayout.SOUTH);
        frame.add(mainPanel,BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        GridLayoutTest test = new GridLayoutTest();
        test.createUI();
    }

    @SuppressWarnings("serial")
    class TopPanel extends JPanel{
        public TopPanel(){
            setLayout(new GridLayout(2, 3));
            ImageIcon icon = new ImageIcon("capture.png");
            JLabel label1 = new JLabel(icon);
            label1.setVisible(false);
            JLabel label2 = new JLabel(icon);
            JLabel label3 = new JLabel(icon);
            label3.setVisible(false);
            JLabel label4 = new JLabel(icon);
            JLabel label5 = new JLabel(icon);
            JLabel label6 = new JLabel(icon);
            add(label1);
            add(label2);
            add(label3);
            add(label4);
            add(label5);
            add(label6);
        }
    }

    @SuppressWarnings("serial")
    class ButtomPanel extends JPanel{
        public ButtomPanel(){
            JButton startButton = new JButton("start");
            JButton stopButton = new JButton("stop");
            JButton recordButton = new JButton("record");

            setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            add(Box.createHorizontalGlue());
            add(startButton);
            add(Box.createHorizontalStrut(10));
            add(stopButton);
            add(Box.createHorizontalStrut(10));
            add(recordButton);
            add(Box.createHorizontalGlue());
        }
    }
}

BoxLayout非常好,也提供空白区域,帮助您将组件居中。

    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    add(Box.createHorizontalGlue());
    add(startButton);
    add(Box.createHorizontalStrut(10));
    add(stopButton);
    add(Box.createHorizontalStrut(10));
    add(recordButton);
    add(Box.createHorizontalGlue());

在第一个组件之前添加胶水,在最后一个组件之后添加胶水将帮助您使组件居中,添加支柱可以帮助您提供所需的空白区域。您可以参考https://stackoverflow.com/a/22525005/3378204了解更多详情。

效果如下:

enter image description here

enter image description here

enter image description here

BoxLayout不会影响组件的大小。希望它可以帮到你。