質問

I am trying to make an GUI in java ,this is my first venture with GUI in java and I am trying to learnTHIS IS THE LAYOUT I WANT TO ARCHIVE

Above is what I trying to create.But I simple can't get to design it in that way ,here is my code:

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

Here is what I getMY layout,still midway

THing is I get the label and the field on the same line, don't understand why,please help thanks.

役に立ちましたか?

解決

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

This is your first mistake. The cells of a GridLayout are all the same size, while you want the central part to be higher.

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

This is the second one, you set frame to have a GridLayout, so you shouldn't use BorderLayout constraints.

The correct thing to do, in my opinion, is to remove the first line, and leave the frame with the default border layout.

As for your issue with the grid, it's due to the fact that you're not filling the grid. If you insert 4 controls in the grid, or initialize the grid with (1,2), you will get the expected outcome.

This is with centerPanel.setLayout(new GridLayout(1,2));: Layout with 1x2 grid

And this is with `

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

Layout with 2x2 grid and dummy labels

他のヒント

What about this:

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

}

I think you get the idea.

NOTE: I've used this VerticalLayout class.

Not good answers I see from others, now this one is good. Just type:

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

And set the LOCATIONS and SIZES to the objects on the JPANEL and you can place them anywhere.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top