我贴下代码,我想把第二个JPAnel里面的按钮移动到第一个的下方,我用了gridy为什么还是无法移动啊
- Java code
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(); }}
------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame
{
private JTextField displayField;// 计算结果显示区
private String lastCommand;// 保存+,-,*,/,=命令
private double result;// 保存计算结果
private boolean start;// 判断是否为数字的开始
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
public Calculator()
{
super("java_刘阳");
container = getContentPane();
layout = new GridBagLayout();
container.setLayout(layout);
constraints = new GridBagConstraints();
start = true;
result = 0;
lastCommand = "=";
displayField = new JTextField(20);
displayField.setHorizontalAlignment(JTextField.RIGHT);
//注意这里
constraints.gridx =0;
constraints.gridy =0;
constraints.gridwidth = 3; //在第几格显示
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 100; //调整比例
constraints.weighty = 100;
layout.setConstraints(displayField, constraints);
container.add(displayField);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
addButton("Backspace", 0, 1, 2, 1, insert);
addButton("CE", 2, 1, 1, 1, insert);
addButton("C", 3, 1, 1, 1, insert);
addButton("7", 0, 2, 1, 1, insert);
addButton("8", 1, 2, 1, 1, insert);
addButton("9", 2, 2, 1, 1, insert);
addButton("/", 3, 2, 1, 1, command);
//
// addButton("4", 0, 3, 1, 1, insert);
//
// addButton("5", 1, 3, 1, 1, insert);
//
// addButton("6", 2, 3, 1, 1, insert);
// addButton("*", 3, 3, 1, 1, command);
//
// addButton("1", 0, 4, 1, 1, insert);