[Java] java swing计算器 →→→→→进入此内容的聊天室

来自 , 2021-02-04, 写在 Java, 查看 171 次.
URL http://www.code666.cn/view/d94e18a8
  1. import java.awt.*;
  2. import java.awt.event.WindowAdapter;
  3. import java.awt.event.WindowEvent;
  4.  
  5. public class CalFrame extends Frame {
  6.   //http://www.2cto.com/kf/201405/298165.html
  7.         public void launchFrame() {
  8.                 this.setTitle("test");
  9.                 this.setBounds(20, 20, 300, 450);
  10.                 this.setMaximumSize(null);
  11.                 this.setVisible(true);
  12.                 this.setResizable(false);
  13.                 this.addWindowListener(new WindowAdapter() {
  14.  
  15.                         @Override
  16.                         public void windowClosing(WindowEvent e) {
  17.                                 System.exit(0);
  18.                         }
  19.                 });
  20.  
  21.                 MenuBar menu = new MenuBar();
  22.                 Menu file = new Menu("File");
  23.                 Menu edit = new Menu("Edit");
  24.                 Menu source = new Menu("Source");
  25.                 Menu refa = new Menu("Refactor");
  26.                 menu.add(file);
  27.                 menu.add(edit);
  28.                 menu.add(source);
  29.                 menu.add(refa);
  30.                 this.setMenuBar(menu);
  31.  
  32.                 Panel p = new Panel();
  33.                 p.setLayout(new BorderLayout(5, 5));
  34.                 p.setBackground(Color.LIGHT_GRAY);
  35.                 this.add(p);
  36.  
  37.                 TextArea t = new TextArea("0", 2, 1, TextArea.SCROLLBARS_NONE);
  38.                 t.setBounds(new Rectangle(280, 40));
  39.                 t.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  40.                 t.setEditable(false);
  41.                 System.out.println(t.getWidth() + "----" + t.getHeight());
  42.                 p.add(t, BorderLayout.NORTH);
  43.  
  44.                 Panel sub = new Panel();
  45.                 sub.setBackground(Color.GRAY);
  46.                 sub.setBounds(new Rectangle(280, (p.getHeight() - t.getHeight())));
  47.                 p.add(sub, BorderLayout.CENTER);
  48.                 sub.setLayout(new GridLayout(6, 5, 5, 5));
  49.        
  50.                 for (int i = 0; i < 5; i++) {
  51.                         for (int j = 0; j < 6; j++) {
  52.                                 Button b = new Button("i");
  53.                                 int width = sub.getWidth() / 5;
  54.                                 int height = sub.getHeight() / 6;
  55.                                 b.setSize(width, height);
  56.                                 b.setBackground(Color.LIGHT_GRAY);
  57.                                 sub.add(b);              
  58.                         }
  59.                 }
  60.                
  61.  
  62.         }
  63.  
  64.         public static void main(String[] args) {
  65.                 new CalFrame().launchFrame();
  66.         }
  67.  
  68. }
  69.  

回复 "java swing计算器"

这儿你可以回复上面这条便签

captcha