从大一开始就有人做计算器,那时候由于对gui不太熟悉,再加上当时认为算法自己理解地不是很透彻,所以没有自己做过。最近一直想努力做好gui,所以做了一个计算器的gui。
这篇文章来实例自己如何一步一步没话界面,有好建议的希望提出来。
首先,上version 1:
代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* This class is to build the frame layout of the project. What's more, add
* actionListener to every button.
* @author Tian Meng
* @version 2011-4-5
*/
public class GuiFrame extends JFrame
{
JTextArea displayMessage =new JTextArea("\n0",2,26);
JButton[] numbers =new JButton[10];//数字键
JButton[] identifyOfNumbers =new JButton[2];//小数点和正负号
JButton[] operator =new JButton[8];// + ; - ; * ; / ; = ; sqrt ;1/x ; %
JButton[] clearer =new JButton[2]; // reset 和 backspace
/**
* constructor of this class
*/
public GuiFrame()
{
initButtons();
makeFrame();
setVisible(true);
pack();
}
/**
*Initailize the buttons in the frame
*/
private void initButtons()
{
init_numbers();
init_identifyOfNumbers();
init_operator();
init_clearer();
}
/**
* Initalize the number buttons anb add actionlistener
*/
private void init_numbers()
{
String[] nub ={"7","8","9","4","5","6","1","2","3","0"};
for(int i=0;i<10;i++)
{
numbers[i]=new JButton(nub[i]);
numbers[i].setForeground(Color.BLUE);
numbers[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
}
/**
* Initalize the "+/-" and " . " buttons, then add actionlistener
*/
private void init_identifyOfNumbers()
{
for(int i=0;i<2;i++)
{
if(i==0)
{
identifyOfNumbers[i]= new JButton("+/-");
}
else
{
identifyOfNumbers[i]= new JButton(".");
}
identifyOfNumbers[i].setForeground(Color.BLUE);
identifyOfNumbers[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
}
/**
* Initalize the operators buttons anb add actionlistener
*/
private void init_operator()
{
String[] op={"/","sqrt","*","%","-","1/x","+","="};
for(int i=0;i<8;i++)
{
operator[i] =new JButton(op[i]);
operator[i].setForeground(Color.RED);
operator[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
}
/**
* Initalize the clear buttons anb add actionlistener
*/
private void init_clearer()
{
for(int i=0;i<2;i++)
{
if(i==0)
{
clearer[i]= new JButton("BackSpace");
}
else
{
clearer[i]= new JButton("Clear");
}
clearer[i].setForeground(Color.RED);
clearer[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
}
/**
* this method is to construct the frame layout
*/
private void makeFrame()
{
Container con=getContentPane();
con.setLayout(new BorderLayout());
//displayMessage in the north part
JPanel northPanel =new JPanel();
con.add(northPanel,BorderLayout.NORTH);
displayMessage.setEnabled(false);
displayMessage.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
northPanel.add(displayMessage);
//clearer in the center part
JPanel centerPanel =new JPanel();
con.add(centerPanel,BorderLayout.CENTER);
centerPanel.add(clearer[0]);
centerPanel.add(clearer[1]);
//numbers and operators in the south part
JPanel southPanel =new JPanel(new BorderLayout());
JPanel numPanel =new JPanel(new GridLayout(4,3,3,3));
JPanel opePanel =new JPanel(new GridLayout(4,2,3,3));
con.add(southPanel,BorderLayout.SOUTH);
southPanel.add(numPanel,BorderLayout.CENTER);
southPanel.add(opePanel,BorderLayout.EAST);
for(int i=0;i<10;i++)
{
numPanel.add(numbers[i]);
}
numPanel.add(identifyOfNumbers[0]);
numPanel.add(identifyOfNumbers[1]);
numPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
for(int i=0;i<8;i++)
{
opePanel.add(operator[i]);
}
opePanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
}
效果图如下:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
version2 To be continued ...