|
楼主 |
发表于 2009-1-26 22:13:51
|
显示全部楼层
计算器界面设计:
1. 使用Swing组件, JFrame,JLabel,JButton,JTextField,JMenu,JMenuItem及布局管理器。
代码如下(先不添加菜单):
import java.awt.event.*;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class CalculationD extends JFrame{
public Container contentPane;
public JPanel northPanel;
public JPanel leftPanel;
public JPanel centerPanel;
public JPanel rightPanel;
public JTextField inputField1;
public JTextField inputField2;
public JTextField inputField3;
public JLabel D1;
public JLabel D2;
public JLabel D3;
public JButton plusButton;
public JButton minusButton;
public JButton mulButton;
public JButton divButton;
public JButton exitButton;
public JButton answerButton;
public JButton clearButton;
public JButton onButton;
public JButton twButton;
public JButton thButton;
public JButton foButton;
public JButton fiButton;
public JButton siButton;
public JButton seButton;
public JButton eiButton;
public JButton niButton;
public JButton zeButton;
public JButton poButton;
public CalculationD(){
super("My Calculator");
contentPane=this.getContentPane();
this.setSize(250,400);
leftPanel =new JPanel();
leftPanel.setLayout(new GridLayout(4,1));
D1=new JLabel(" D1");
D2=new JLabel(" D2");
D3=new JLabel("Answer ");
leftPanel.add(D1);
leftPanel.add(D2);
leftPanel.add(new JLabel(""));
leftPanel.add(D3);
centerPanel=new JPanel();
centerPanel.setLayout(new GridLayout(3,1));
inputField1=new JTextField();
inputField2=new JTextField();
inputField3=new JTextField();
centerPanel.add(inputField1);
centerPanel.add(inputField2);
centerPanel.add(inputField3);
inputField3.setEditable(false);
rightPanel=new JPanel();
rightPanel.setLayout(new GridLayout(4,4));
clearButton=new JButton("clear");
plusButton=new JButton("+");
minusButton=new JButton("-");
mulButton=new JButton("*");
divButton=new JButton("/");
onButton=new JButton("1");
twButton=new JButton("2");
thButton=new JButton("3");
foButton=new JButton("4");
fiButton=new JButton("5");
siButton=new JButton("6");
seButton=new JButton("7");
eiButton=new JButton("8");
niButton=new JButton("9");
zeButton=new JButton("0");
poButton=new JButton(".");
rightPanel.add(onButton);
rightPanel.add(twButton);
rightPanel.add(thButton);
rightPanel.add(clearButton);
rightPanel.add(foButton);
rightPanel.add(fiButton);
rightPanel.add(siButton);
rightPanel.add(plusButton);
rightPanel.add(seButton);
rightPanel.add(eiButton);
rightPanel.add(niButton);
rightPanel.add(minusButton);
rightPanel.add(zeButton);
rightPanel.add(poButton);
rightPanel.add(mulButton);
rightPanel.add(divButton);
contentPane.add(leftPanel,BorderLayout.WEST);
contentPane.add(centerPanel,BorderLayout.CENTER);
contentPane.add(rightPanel,BorderLayout.SOUTH);
this.setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
((Window)e.getComponent()).dispose();
System.exit(0); } } );
}
public static void main(String[] args){
new CalculationD();
}
}
运行结果如下,此时不具有任何计算,只可以实现关闭窗口的功能: |
|