public Main(){
super(" MyCalculator"); //名前
resultField=new JTextField("0");
resultField.setEditable(false);
resultField.setHorizontalAlignment(JTextField.RIGHT);
s1=new JButton(" 1 ");
s2=new JButton(" 2 ");
s3=new JButton(" 3 ");
s4=new JButton(" 4 ");
s5=new JButton(" 5 ");
s6=new JButton(" 6 ");
s7=new JButton(" 7 ");
s8=new JButton(" 8 ");
s9=new JButton(" 9 ");
s0=new JButton(" 0 ");
b1=new JButton(" + ");
b2=new JButton(" - ");
b3=new JButton(" * ");
b4=new JButton(" / ");
f1=new JButton(" . ");
f2=new JButton(" = ");
f3=new JButton(" C "); GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
add(layout, resultField, 0, 0, 4, 1);
add(layout, s1, 1, 0, 1, 1);
add(layout, s2, 1, 1, 1, 1);
add(layout, s3, 1, 2, 1, 1);
add(layout, s4, 2, 0, 1, 1);
add(layout, s5, 2, 1, 1, 1);
add(layout, s6, 2, 2, 1, 1);
add(layout, s7, 3, 0, 1, 1);
add(layout, s8, 3, 1, 1, 1);
add(layout, s9, 3, 2, 1, 1);
add(layout, f1, 4, 0, 1, 1);
add(layout, s0, 4, 1, 1, 1);
add(layout, b1, 1, 3, 1, 1);
add(layout, b2, 2, 3, 1, 1);
add(layout, b3, 3, 3, 1, 1);
add(layout, b4, 4, 3, 1, 1);
add(layout, f2, 4, 2, 1, 1);
add(layout, f3, 5, 0, 4, 1);
}
private void add(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
}
哪有错误呢 弹出但是画面特别小 我又设置画面的大小 为什么呢
super(" MyCalculator"); //名前
resultField=new JTextField("0");
resultField.setEditable(false);
resultField.setHorizontalAlignment(JTextField.RIGHT);
s1=new JButton(" 1 ");
s2=new JButton(" 2 ");
s3=new JButton(" 3 ");
s4=new JButton(" 4 ");
s5=new JButton(" 5 ");
s6=new JButton(" 6 ");
s7=new JButton(" 7 ");
s8=new JButton(" 8 ");
s9=new JButton(" 9 ");
s0=new JButton(" 0 ");
b1=new JButton(" + ");
b2=new JButton(" - ");
b3=new JButton(" * ");
b4=new JButton(" / ");
f1=new JButton(" . ");
f2=new JButton(" = ");
f3=new JButton(" C "); GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
add(layout, resultField, 0, 0, 4, 1);
add(layout, s1, 1, 0, 1, 1);
add(layout, s2, 1, 1, 1, 1);
add(layout, s3, 1, 2, 1, 1);
add(layout, s4, 2, 0, 1, 1);
add(layout, s5, 2, 1, 1, 1);
add(layout, s6, 2, 2, 1, 1);
add(layout, s7, 3, 0, 1, 1);
add(layout, s8, 3, 1, 1, 1);
add(layout, s9, 3, 2, 1, 1);
add(layout, f1, 4, 0, 1, 1);
add(layout, s0, 4, 1, 1, 1);
add(layout, b1, 1, 3, 1, 1);
add(layout, b2, 2, 3, 1, 1);
add(layout, b3, 3, 3, 1, 1);
add(layout, b4, 4, 3, 1, 1);
add(layout, f2, 4, 2, 1, 1);
add(layout, f3, 5, 0, 4, 1);
}
private void add(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
}
哪有错误呢 弹出但是画面特别小 我又设置画面的大小 为什么呢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货