无法在java中获得简单的布局

时间:2013-10-18 23:33:33

标签: java swing layout jpanel flowlayout

我正在尝试在java中创建一个GUI,这是我在java中首次尝试使用GUI,我正在尝试学习THIS IS THE LAYOUT I WANT TO ARCHIVE

以上就是我要创建的内容。但我简单无法以这种方式设计它,这是我的代码:

//Frame:   
     JFrame frame;
     //Menu :
     JMenuBar menuBar;
     JMenu menu1,menu2;
     JMenuItem menuItem;
     //Panels:
     JPanel topPanel;
     JPanel centerPanel;
     JPanel bpttomPanel;  
     String[] vTypeStrings = { "Select vehicle","Car", "Boat", "Truck", };
     //Labels:
     JLabel typeLabel;
     //ComboBoxes:
     JComboBox vList;;

     //Frame creation   
     frame= new JFrame("frame1");
     frame.setSize(450,250);
     frame.setLocation(200,300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setLayout(new GridLayout(3,1));

     //Create the menu bar.
     menuBar = new JMenuBar();

     //Create menu bar items
     menu1 = new JMenu("File");
     menu1.setMnemonic('F');
     menuBar.add(menu1);

     menu2 = new JMenu("Help");
     menu2.setMnemonic('H');
     menuBar.add(menu2);

     //Adding items to  each menu 
     menuItem = new JMenuItem("Load", 'L');
     menu1.add(menuItem);
     menuItem = new JMenuItem("Exit", 'X');
     menu1.add(menuItem);
     //Second menu
     menuItem = new JMenuItem("About",'A');
     menu2.add(menuItem);

     //Adding menu to frame
     frame.setJMenuBar(menuBar);



     //Top Panel
      topPanel = new JPanel(new FlowLayout());
      frame.add(topPanel,BorderLayout.NORTH);
      JLabel headLabel=new JLabel("Snedden's Ordering system");//Heading label
      topPanel.add(headLabel);
      headLabel.setFont(new Font("Serif", Font.PLAIN, 24));
      headLabel.setForeground(new Color(0xff0000));


     //Center Panel
     centerPanel = new JPanel();
     centerPanel.setLayout(new GridLayout(2,2,2,2));
     vList = new JComboBox(vTypeStrings);
     vList.setSelectedIndex(0);
     typeLabel=new JLabel("Vehicle Type");
     typeLabel.setLabelFor(vList);
     centerPanel.add(typeLabel);
     centerPanel.add(vList);
     frame.add(centerPanel,BorderLayout.CENTER);



     frame.setVisible(true);

这是我得到的MY layout,still midway

是的,我得到的标签和字段在同一行,不明白为什么,请帮助谢谢。

3 个答案:

答案 0 :(得分:0)

frame.setLayout(new GridLayout(3,1));

这是你的第一个错误。 GridLayout的单元格大小相同,而您希望中心部分更高。

frame.add(topPanel,BorderLayout.NORTH);
....
frame.add(centerPanel,BorderLayout.CENTER);

这是第二个,您将框架设置为GridLayout,因此不应使用BorderLayout约束。

在我看来,正确的做法是删除第一行,并使框架保留默认的边框布局。

至于你对网格的问题,这是因为你没有填充网格。 如果在网格中插入4个控件,或者使用(1,2)初始化网格,则会得到预期的结果。

这是centerPanel.setLayout(new GridLayout(1,2));Layout with 1x2 grid

这是用`

centerPanel.add(typeLabel);
centerPanel.add(vList);
centerPanel.add(new JLabel());
centerPanel.add(new JLabel());

Layout with 2x2 grid and dummy labels

答案 1 :(得分:0)

这个怎么样:

import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
    public Gui()
    {
        super("Gui");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setLocationRelativeTo(null);
        setVisible(true);

        add(new Form(), BorderLayout.EAST);
        add(new ButtonRow(), BorderLayout.SOUTH);

    }

    private class ButtonRow extends JPanel {
        private JButton button1;
        private JButton button2;
        private JButton button3;
        private JButton button4;

        public ButtonRow()
        {
            setLayout(new FlowLayout());
            button1 = new JButton("button 1");
            button2 = new JButton("button 2");
            button3 = new JButton("button 3");
            button4 = new JButton("button 4");
            add(button1);
            add(button2);
            add(button3);
            add(button4);
        }
    }

    public static void main(String args[]) 
    {
        new Gui();
    }


    private class Form extends JPanel {
        String[] vTypeStrings = { "Select vehicle","Car", "Boat", "Truck", };
        public Form()
        {
            setLayout(new VerticalLayout());
            JComboBox vList = new JComboBox(vTypeStrings);
            add(new Control("choose", vList));
            add(new Control("label 1"));
            add(new Control("label 2"));
            add(new Control("label 3"));
            add(new Control("label 4"));
        }
    }

    private class Control extends JPanel {
        private JLabel label;
        private JTextField text;

        public Control(String lbl)
        {
            label = new JLabel(lbl);
            text = new JTextField(15);
            setLayout(new FlowLayout());
            add(label);
            add(text);
        }

        public Control(String lbl, JComboBox list)
        {
            label = new JLabel(lbl);
            setLayout(new FlowLayout());
            add(label);
            add(list);
        }
    }

}

我想你明白了。

注意:我已使用此VerticalLayout课程。

答案 2 :(得分:0)

我从别人那里得到的答案不是很好,现在这个答案很好。只需输入:

JPanel panel = new JPanel(); panel.setLayout(null);

将LOCATIONS和SIZES设置为JPANEL上的对象,您可以将它们放在任何位置。