我要用java开发一个计算器程序,我用的是gridbaglayout,我想在右下角放一个大“=”按钮,但是这个按钮的高度不会像预期的那样扩展到最后一个空格,我的主要代码如下所示,请帮我建议如何计算:我的问题一是这样的
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import org.omg.CORBA.PUBLIC_MEMBER;
public class MyCalculater {
private Frame frame = new Frame("My-Calculator");
private GridBagConstraints gBagConstraints = new GridBagConstraints();
private GridBagLayout gBagLayout = new GridBagLayout();
private Panel screen = new Panel();
private Panel arithSymbolArea = new Panel();
private Panel digitalArea = new Panel();
private JButton[] digitalButtons = new JButton[10];
public void init() {
// Put a screen for output
screen.add(new TextField(40));
frame.add(screen, BorderLayout.NORTH);
// To put the Arith symbols
arithSymbolArea.setLayout(new GridLayout(6, 4));
frame.add(arithSymbolArea);
digitalArea.setLayout(gBagLayout);
initDigitalArea();
frame.add(digitalArea, BorderLayout.EAST);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
private void initDigitalArea() {
JButton mc = new JButton("mc");
JButton mplus = new JButton("m+");
JButton mminus = new JButton("m-");
JButton mr = new JButton("mr");
JButton ac = new JButton("AC");
JButton pn = new JButton("+/-");
JButton divide = new JButton("÷");
JButton times = new JButton("×");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton equal = new JButton("=");
JButton dot = new JButton(".");
for (int i = 0; i < 10; i++) {
digitalButtons[i] = new JButton(String.valueOf(i));
}
gBagConstraints.fill = GridBagConstraints.BOTH;
gBagConstraints.weightx = 1;
addButton(mc);
addButton(mplus);
addButton(mminus);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
addButton(mr);
gBagConstraints.gridwidth = 1;
gBagConstraints.gridheight = 1;
addButton(ac);
addButton(pn);
addButton(divide);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
addButton(times);
gBagConstraints.gridwidth = 1;
gBagConstraints.gridheight = 1;
addButton(digitalButtons[7]);
addButton(digitalButtons[8]);
addButton(digitalButtons[9]);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
addButton(minus);
gBagConstraints.gridwidth = 1;
gBagConstraints.gridheight = 1;
addButton(digitalButtons[4]);
addButton(digitalButtons[5]);
addButton(digitalButtons[6]);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
addButton(plus);
gBagConstraints.gridwidth = 1;
gBagConstraints.gridheight = 1;
addButton(digitalButtons[1]);
addButton(digitalButtons[2]);
addButton(digitalButtons[3]);
gBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gBagConstraints.gridheight = 2;
addButton(equal);
gBagConstraints.gridheight = 1;
gBagConstraints.gridwidth = 2;
addButton(digitalButtons[0]);
gBagConstraints.gridwidth = 1;
addButton(dot);
}
private void addButton(JButton button) {
gBagLayout.setConstraints(button, gBagConstraints);
digitalArea.add(button);
}
public static void main(String[] args) {
new MyCalculater().init();
}
private void calculate() {
}
class DigitalButton extends JButton {
public DigitalButton() {
// TODO Auto-generated constructor stub
this.setBackground(new Color(0, 0, 0));
}
}
class ArithSymbol extends JButton {
public ArithSymbol() {
// TODO Auto-generated constructor stub
this.setBackground(new Color(128, 128, 128));
}
}
}
1条答案
按热度按时间k10s72fa1#
试试这个