Swing實現計算器GUI

jopen 10年前發布 | 24K 次閱讀 Swing Java開發

package swing;

import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea;

/**

  • @author He Jinlong
  • @version 2.0 第一個版本沒有使用網格布局,顯示出來的效果很糟糕,通過使用網格布局,終于實現了想要的結果 */ public class NewCalculator extends JFrame {

    // 定義菜單欄 private JMenuBar bar; private JMenu mune; private JMenuItem itemhelp; private JMenuItem itemcaculater; // 建立2個大的容器 private JPanel textPanel;// 存放顯示屏的容器 private JPanel btnPanel;// 存放按鈕的容器 private GridBagLayout gb;// 定義網格布局 private GridBagConstraints gbc;// 定義網格的布局的大管家 private JButton btn[] = new JButton[18];// 定義按鈕數組 private JTextArea textArea;// 定義顯示屏 private String btnStr[] = new String[18];// 定義按鈕內容

    /**

    • 構造方法,用來初始化計算器 */ public NewCalculator() { init(); addComponent(); setJMenuBar(bar); setResizable(false); setLocation(500, 220); setTitle("我的計算器"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setSize(240, 300); }

      /**

    • 初始化各個小組件 */ public void init() { bar = new JMenuBar();

      mune = new JMenu("幫助");

      itemhelp = new JMenuItem("幫助"); itemcaculater = new JMenuItem("關于計算機"); itemhelp.addActionListener(menuListener); itemcaculater.addActionListener(menuListener);

      textPanel = new JPanel(); btnPanel = new JPanel();

      gb = new GridBagLayout(); gbc = new GridBagConstraints();

      gbc.gridwidth = 1; gbc.gridheight = 1; gbc.fill = GridBagConstraints.BOTH; gbc.insets = new Insets(2, 2, 2, 2); gbc.gridx = 0; gbc.gridy = 0;

      textArea = new JTextArea(3, 20); btnStr = new String[] { " c ", " EDL ", " * ", " / ", " 1 ",

           "  2 ", "  3 ", "  + ", "  4 ", "  5 ", "  6 ", "  - ", "  7 ",
           "  8 ", "  9 ", "  = ", "  0 ", "  . " };
      

      for (int i = 0; i < btn.length; i++) {

       btn[i] = new JButton(btnStr[i]);
       btn[i].addActionListener(buttonListener);
      

      }

      }

      /**

    • 將各個小組件添加到容器中 */ public void addComponent() { textArea.setEditable(false); textPanel.add(textArea);

      btnPanel.setLayout(gb); for (int i = 0; i < btn.length; i++) {

       int x = 1;
       if (i == 15) {
           gbc.gridheight = 2;
       } else if (i == 17) {
           gbc.gridwidth = 2;
       }
       gb.setConstraints(btn[i], gbc);
       btnPanel.add(btn[i]);
       gbc.gridwidth = 1;
       gbc.gridheight = 1;
       gbc.gridx += x;
       if ((i + 1) % 4 == 0) {
           gbc.gridx = 0;
           gbc.gridy += 1;
       }
      
      

      }

      add(BorderLayout.NORTH, textPanel);

      add(BorderLayout.CENTER, btnPanel);

      bar.add(mune);

      mune.add(itemhelp); mune.add(itemcaculater); }

      // 實例化一個監聽器,用來監聽按鈕點擊事件 private ActionListener buttonListener = new ActionListener() {

      @Override public void actionPerformed(ActionEvent e) {

       String str = ((JButton) e.getSource()).getText();
       JOptionPane.showMessageDialog(null, "You clicked " + str
               + " button");
      

      } };

      // 同上,用來監聽菜單上的點擊事件 private ActionListener menuListener = new ActionListener() {

      @Override public void actionPerformed(ActionEvent e) {

       String str = ((JMenuItem) e.getSource()).getText();
       JOptionPane.showMessageDialog(null, "You clicked " + str
               + " button");
      

      } };

      public static void main(String[] args) { new NewCalculator(); } }</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!