1

我正在尝试用java制作一个GUI,这是我第一次在java中使用GUI,我正在努力学习这是我要存档的布局

以上是我试图创建的。但我无法以这种方式设计它,这是我的代码:

//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);

这是我得到的我的布局,还在中途

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

4

3 回答 3

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

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

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

这是第二个,你将 frame 设置为GridLayout,所以你不应该使用BorderLayout约束。

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

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

这是与centerPanel.setLayout(new GridLayout(1,2));1x2 网格布局

这是与`

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

带有 2x2 网格和虚拟标签的布局

于 2013-10-18T23:56:49.960 回答
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类。

于 2013-10-19T00:27:32.517 回答
0

我从其他人那里看到的答案不好,现在这个很好。只需输入:

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

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

于 2015-01-01T21:59:46.423 回答