1. 程式人生 > >java-幾何圖形計算器

java-幾何圖形計算器

幾何圖形器運用了java的基礎知識和swing工具集完成,用了兩天零碎的時間完成,使用工具:IDEA

主介面使用了選單和下拉框,下拉框可以選擇要進行的幾何圖形計算,並且添加了一個按鈕,在上面放上圖片,讓介面不那麼單調

通過編寫該程式讓我對多型有了更深的瞭解,BoxPanel類有兩個元件,一個標籤和一個文字框,水平對齊,其他類可以通過實現該類,獲得整齊的元件排列

public class BoxPanel extends JPanel {
    JTextField text;
    public BoxPanel(String s,int n){
        Box box=Box.createHorizontalBox();
        box.add(new JLabel(s));
        text=new JTextField(" ",n);
        text.setHorizontalAlignment(JTextField.RIGHT);
        text.setFont(new Font("Arial",Font.BOLD,15));
        box.add(text);
        add(box);
    }
    public JTextField getJText(){
        return text;
    }
}

 


        Box box=Box.createVerticalBox();
        bpwidth=new BoxPanel("請輸入矩形的高: ",10);
        widthText=bpwidth.getJText();
        widthText.addFocusListener(this);

        bpheight=new BoxPanel("請輸入矩形的高",10);
        heightText=bpheight.getJText();
        heightText.addFocusListener(this);

        buttonpanel=new JPanel();
        resultButton=new JButton("計算結果 ");
        resultButton.addActionListener(this);
        clearButton=new JButton("清空");
        clearButton.addActionListener(this);
        buttonpanel.add(resultButton);
        buttonpanel.add(clearButton);

本程式沒有遇到什麼困難的問題,順利的寫完,程式碼在我的github