1 import java.awt.BorderLayout; 2 3 import javax.swing.*; 4 public class ForJava { 5 6 public static void main(String[] args) { 7 JFrame frame=new CCal(400,300); 8 frame.setVisible(true); 9 } 10 } 11 class CCal extends JFrame{ 12 public CCal(int w,int h){ 13 setLocation(200, 160); 14 setSize(w, h); 15 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 17 18 JPanel displayPanel=new JPanel(); 19 JButton cbtn=new JButton(); 20 cbtn.setText("c"); 21 displayPanel.add(cbtn); 22 23 JTextArea display=new JTextArea(); 24 display.setColumns(20); 25 displayPanel.add(display); 26 getContentPane().add(displayPanel, BorderLayout.NORTH); 27 28 JPanel panelTemp1=new JPanel(); 29 panelTemp1.setLayout(new BorderLayout()); 30 getContentPane().add(panelTemp1); 31 32 JPanel cachePanel=new JPanel(); 33 panelTemp1.add(cachePanel, BorderLayout.NORTH); 34 JButton mbtnMR=new JButton(); 35 mbtnMR.setText("MR"); 36 cachePanel.add(mbtnMR); 37 JButton mbtnMC=new JButton(); 38 mbtnMC.setText("MC"); 39 cachePanel.add(mbtnMC); 40 JButton mbtnMS=new JButton(); 41 mbtnMS.setText("MS"); 42 cachePanel.add(mbtnMS); 43 JButton mbtnMP=new JButton(); 44 mbtnMP.setText("M+"); 45 cachePanel.add(mbtnMP); 46 47 48 JPanel panelTemp2=new JPanel(); 49 panelTemp2.setLayout(new BorderLayout()); 50 panelTemp1.add(panelTemp2); 51 52 JPanel panelButton1=new JPanel(); 53 panelTemp2.add(panelButton1, BorderLayout.NORTH); 54 JButton mbtn1=new JButton(); 55 mbtn1.setText("1"); 56 panelButton1.add(mbtn1); 57 JButton mbtn2=new JButton(); 58 mbtn2.setText("2"); 59 panelButton1.add(mbtn2); 60 JButton mbtn3=new JButton(); 61 mbtn3.setText("3"); 62 panelButton1.add(mbtn3); 63 JButton mbtnP=new JButton(); 64 mbtnP.setText("+"); 65 panelButton1.add(mbtnP); 66 67 JPanel panelTemp3=new JPanel(); 68 panelTemp3.setLayout(new BorderLayout()); 69 panelTemp2.add(panelTemp3); 70 71 JPanel panelButton2=new JPanel(); 72 panelTemp3.add(panelButton2, BorderLayout.NORTH); 73 JButton mbtn4=new JButton(); 74 mbtn4.setText("4"); 75 panelButton2.add(mbtn4); 76 JButton mbtn5=new JButton(); 77 mbtn5.setText("5"); 78 panelButton2.add(mbtn5); 79 JButton mbtn6=new JButton(); 80 mbtn6.setText("6"); 81 panelButton2.add(mbtn6); 82 JButton mbtnMi=new JButton(); 83 mbtnMi.setText("-"); 84 panelButton2.add(mbtnMi); 85 86 JPanel panelTemp4=new JPanel(); 87 panelTemp4.setLayout(new BorderLayout()); 88 panelTemp3.add(panelTemp4); 89 90 JPanel panelButton3=new JPanel(); 91 panelTemp4.add(panelButton3, BorderLayout.NORTH); 92 JButton mbtn7=new JButton(); 93 mbtn7.setText("7"); 94 panelButton3.add(mbtn7); 95 JButton mbtn8=new JButton(); 96 mbtn8.setText("8"); 97 panelButton3.add(mbtn8); 98 JButton mbtn9=new JButton(); 99 mbtn9.setText("9"); 100 panelButton3.add(mbtn9); 101 JButton mbtnMu=new JButton(); 102 mbtnMu.setText("*"); 103 panelButton3.add(mbtnMu); 104 105 JPanel panelTemp5=new JPanel(); 106 panelTemp5.setLayout(new BorderLayout()); 107 panelTemp4.add(panelTemp5); 108 109 JPanel panelButton4=new JPanel(); 110 panelTemp5.add(panelButton4, BorderLayout.NORTH); 111 JButton mbtn0=new JButton(); 112 mbtn0.setText("0"); 113 panelButton4.add(mbtn0); 114 JButton mbtnPorM=new JButton(); 115 mbtnPorM.setText("+/-"); 116 panelButton4.add(mbtnPorM); 117 JButton mbtnEq=new JButton(); 118 mbtnEq.setText("="); 119 panelButton4.add(mbtnEq); 120 JButton mbtnDiv=new JButton(); 121 mbtnDiv.setText("/"); 122 panelButton4.add(mbtnDiv); 123 } 124 125 126 }
要使几行按钮排列不被重叠,就需要JPanel的嵌套:
1 JPanel up1=new JPanel(); 2 /*增加button等*/ 3 getContentPane().add(up1,BorderLayout.north)); 4 5 JPanel down1 = new JPanel(); 6 down1.setLayout(new BorderLayout()); 7 getContentPane().add(down1,BorderLayout.north)); 8 /*down1不增加button这些控件,只作为下一个Jpanel的底层/* 9 10 JPanel up2 = new JPanel(); 11 down1.add(up2,BorderLayout.NORTH)); 12 /*添加控件*/ 13 14 JPanel down2 = new Jpanel(); 15 down2.setLayout(new BorderLayout()); 16 down1.add(down2); 17 //.......................
时间: 2024-10-22 08:09:01