JAVA SE GridBagLayout 问题
我贴下代码,我想把第二个JPAnel里面的按钮移动到第一个的下方,我用了gridy为什么还是无法移动啊
import java.awt.*;import javax.swing.*;public class prj1 extends JFrame{ JLabel customerInfo=new JLabel("客户信息"); JLabel customerName=new JLabel("客户名称"); JLabel labelFind=new JLabel("查询"); JLabel customerAddr=new JLabel("客户地址"); JCheckBox find=new JCheckBox(); JComboBox comboCName=new JComboBox(); Container c; JPanel topPanel=new JPanel(); JPanel bottomPanel=new JPanel(); GridBagLayout gbl=new GridBagLayout(); GridBagLayout gbl2=new GridBagLayout(); GridBagConstraints gbc=new GridBagConstraints(); GridBagConstraints gbc2=new GridBagConstraints(); public prj1() { super("维修单收发货程序"); c=this.getContentPane(); topPanel.add(customerInfo); topPanel.add(customerName); topPanel.add(labelFind); bottomPanel.add(customerAddr); bottomPanel.add(find); this.setLayout(gbl); gbc.fill=GridBagConstraints.BOTH; gbc.anchor=GridBagConstraints.WEST; gbl.setConstraints(topPanel, gbc); gbc2.gridy=50; gbl2.setConstraints(bottomPanel, gbc2); comboCName.addItem("yangliweng"); comboCName.addItem("kmsfan"); comboCName.addItem("ylw"); topPanel.add(comboCName); c.add(topPanel); c.add(bottomPanel); this.setSize(400,400); this.setVisible(true); } public static void main(String args[]) { prj1 p=new prj1(); }}
//
// addButton("2", 1, 4, 1, 1, insert);
//
// addButton("3", 2, 4, 1, 1, insert);
//
// addButton("-", 3, 4, 1, 1, command);
//
// addButton("0", 0, 5, 1, 1, insert);
//
// addButton("+/-", 1, 5, 1, 1, insert);// 只显示"-"号,"+"没有实用价值
//
// addButton(".", 2, 5, 1, 1, insert);
//
// addButton("+", 3, 5, 1, 1, command);
//
// addButton("=", 0, 6, 4, 1, command);
setSize(300, 300);
setVisible(true);
}
private void addButton(String label, int row, int column, int with,
int height, ActionListener listener)
{
JButton button = new JButton(label);
constraints.gridx = row;
constraints.gridy = column;
constraints.gridwidth = with;
constraints.gridheight = height;
constraints.fill = GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button, constraints);
container.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
displayField.setText("");
start = false;
if (input.equals("+/-"))
displayField.setText(displayField.getText() + "-");
}
if (!input.equals("+/-"))
{
if (input.equals("Backspace"))
{
String str = displayField.getText();
if (str.length() > 0)
displayField
.setText(str.substring(0, str.length() - 1));
}
else if (input.equals("CE") || input.equals("C"))
{
displayField.setText("0");
start = true;
}
else
displayField.setText(displayField.getText() + input);
}
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String command = evt.getActionCommand();
if (start)
{
lastCommand = command;
}
else
{
calculate(Double.parseDouble(displayField.getText()));
lastCommand = command;
start = true;
}
}
}
public void calculate(double x)
{
if (lastCommand.equals("+"))
result += x;
else if (lastCommand.equals("-"))
result -= x;
else if (lastCommand.equals("*"))
result *= x;
else if (lastCommand.equals("/"))
result /= x;
else if (lastCommand.equals("="))
result = x;
displayField.setText("" + result);
}
public static void main(String[] args)
{
Calculator calculator = new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
楼主把上面的代码改改,就可以满足你的需求