can';在java中无法获得简单的布局
我正在尝试用java制作GUI,这是我第一次尝试用java制作GUI,我正在努力学习 以上是我试图创建的内容。但我无法以这种方式进行设计,以下是我的代码:can';在java中无法获得简单的布局,java,swing,layout,jpanel,flowlayout,Java,Swing,Layout,Jpanel,Flowlayout,我正在尝试用java制作GUI,这是我第一次尝试用java制作GUI,我正在努力学习 以上是我试图创建的内容。但我无法以这种方式进行设计,以下是我的代码: //Frame: JFrame frame; //Menu : JMenuBar menuBar; JMenu menu1,menu2; JMenuItem menuItem; //Panels: JPanel topPanel; JPanel center
//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);
这是我得到的
问题是我得到的标签和字段在同一行,不明白为什么,请帮助谢谢
frame.setLayout(new GridLayout(3,1));
这是你的第一个错误。GridLayout
的单元格大小都相同,但您希望中心部分更高
frame.add(topPanel,BorderLayout.NORTH);
....
frame.add(centerPanel,BorderLayout.CENTER);
这是第二个,您将框架设置为具有GridLayout
,因此不应使用BorderLayout
约束
在我看来,正确的做法是删除第一行,并保留框架的默认边框布局
至于你的网格问题,这是因为你没有填充网格。
如果在网格中插入4个控件,或使用(1,2)初始化网格,将获得预期结果
这是使用centerPanel.setLayout(新的GridLayout(1,2))代码>:
这是我的`
centerPanel.add(typeLabel);
centerPanel.add(vList);
centerPanel.add(new JLabel());
centerPanel.add(new JLabel());
这个怎么样:
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);
}
}
}
我想你明白了
注意:我用过这门课。我从别人那里看到了不好的答案,现在这门是好的。仅键入:
JPanel面板=新的JPanel();
panel.setLayout(空)代码>
并在JPANEL上设置对象的位置和大小,您可以将其放置在任何位置。谢谢,我现在知道了,您再也不能告诉我如何将标签粘贴到右侧。即车辆类型应触摸左侧的下拉列表typeLabel.setHorizontalAlignment(SwingConstants.right)代码>