1. package LXF;
  2.  
  3.  
  4. import java.awt.Container;
  5.  
  6.  
  7. import javax.swing.JButton;
  8.  
  9.  
  10. import javax.swing.JFrame;
  11.  
  12.  
  13. import javax.swing.WindowConstants;
  14.  
  15.  
  16. public class DYL extends JFrame{
  17.  
  18. public void Create(String title) {
  19.  
  20. JFrame jf =new JFrame(title);
  21.  
  22. Container c=jf.getContentPane();
  23.  
  24. jf.setVisible(true);
  25.  
  26. jf.setLayout(null);
  27.  
  28. jf.setSize(400,400);
  29.  
  30. jf.setBounds(0,0,230,300);
  31.  
  32. JButton b1=new JButton("1");
  33.  
  34. JButton b2=new JButton("2");
  35.  
  36. JButton b3=new JButton("3");
  37.  
  38. JButton b4=new JButton("4");
  39.  
  40. JButton b5=new JButton("5");
  41.  
  42. JButton b6=new JButton("6");
  43.  
  44. JButton b7=new JButton("7");
  45.  
  46. JButton b8=new JButton("8");
  47.  
  48. JButton b9=new JButton("9");
  49.  
  50. JButton b10=new JButton("0");
  51.  
  52. JButton b11=new JButton("+");
  53.  
  54. JButton b12=new JButton("-");
  55.  
  56. JButton b13=new JButton("*");
  57.  
  58. JButton b14=new JButton("/");
  59.  
  60. JButton b15=new JButton("en");
  61.  
  62. b1.setBounds(10,10,50,50);
  63.  
  64. b2.setBounds(60,10,50,50);
  65.  
  66. b3.setBounds(110,10,50,50);
  67.  
  68. b4.setBounds(10,60,50,50);
  69.  
  70. b5.setBounds(60,60,50,50);
  71.  
  72. b6.setBounds(110,60,50,50);
  73.  
  74. b7.setBounds(10,110,50,50);
  75.  
  76. b8.setBounds(60,110,50,50);
  77.  
  78. b9.setBounds(110,110,50,50);
  79.  
  80. b10.setBounds(10,160,50,50);
  81.  
  82. b11.setBounds(60,160,50,50);
  83.  
  84. b12.setBounds(110,160,50,50);
  85.  
  86. b13.setBounds(160,10,50,50);
  87.  
  88. b14.setBounds(160,60,50,50);
  89.  
  90. b15.setBounds(160,110,50,100);
  91.  
  92. c.add(b1);
  93.  
  94. c.add(b2);
  95.  
  96. c.add(b3);
  97.  
  98. c.add(b4);
  99.  
  100. c.add(b5);
  101.  
  102. c.add(b6);
  103.  
  104. c.add(b7);
  105.  
  106. c.add(b8);
  107.  
  108. c.add(b9);
  109.  
  110. c.add(b10);
  111.  
  112. c.add(b11);
  113.  
  114. c.add(b12);
  115.  
  116. c.add(b13);
  117.  
  118. c.add(b14);
  119.  
  120. c.add(b15);
  121.  
  122. jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  123.  
  124. }
  125.  
  126. public static void main(String[] args) {
  127.  
  128. new ShiYan10().Create("计算器");
  129.  
  130. }
  131.  
  132.  
  133. }
  134.  
  135. 2.总结
  136.  
  137. 通过此次实验,很好地熟悉了图形用户界面的设计原则
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. package shiyan;
  145. import java.awt.*;
  146. import javax.swing.*;
  147. import java.awt.event.*;
  148.  
  149. public class Jisuanqi extends Frame implements ActionListener{
  150. public Jisuanqi() {
  151. super("计算器");
  152. Frame ff=new Frame("framework test");
  153. ff.setSize(400,100);
  154. ff.setLocation(300,240);
  155. ff.setLayout(new FlowLayout());
  156. final TextField f1=new TextField("10",8);
  157. ff.add(f1);
  158.  
  159. Label l1=new Label("+");
  160. ff.add(l1);
  161. TextField f2=new TextField("10",8);
  162. ff.add(f2);
  163. Button b1=new Button("=");
  164. ff.add(b1);
  165. TextField f3=new TextField(10);
  166. ff.add(f3);
  167. ff.addWindowListener(new myclose());
  168. ff.setVisible(true);
  169. b1.addActionListener(new ActionListener()
  170. {public void actionPerformed(ActionEvent e)
  171. {double c;
  172. String s1=f1.getText();
  173. double a=Integer.parseInt(s1);
  174. String s2=f2.getText();
  175. double b=Integer.parseInt(s2);
  176. c=a+b;
  177. String m=String.valueOf(c);
  178. f3.setText(m);
  179. }
  180.  
  181. private double Integer(String s) {
  182. return 0;
  183. }
  184. });
  185.  
  186. }
  187.  
  188. class myclose implements WindowListener{
  189.  
  190. public void windowActivated(WindowEvent arg0) {
  191.  
  192. }
  193.  
  194. public void windowClosed(WindowEvent arg0) {
  195. }
  196.  
  197. public void windowClosing(WindowEvent arg0) {
  198. System.exit(0);
  199. }
  200.  
  201. public void windowDeactivated(WindowEvent arg0) {
  202. }
  203.  
  204. public void windowDeiconified(WindowEvent arg0) {
  205. // TODO Auto-generated method stub
  206. }
  207.  
  208. public void windowIconified(WindowEvent arg0) {
  209. }
  210.  
  211. public void windowOpened(WindowEvent arg0) {
  212. }
  213. }
  214.  
  215. public static void main(String[] args) {
  216. new Jisuanqi();
  217. }
  218.  
  219. public void actionPerformed(ActionEvent arg0)
  220.  
  221. {
  222. }
  223.  
  224. }
  225.  
  226.  
  227. 二、结果
  228.  
  229. 10+10=20
  230.  
  231. 三、心得
  232.  
  233.  
  234. 1、创建按钮以后,要用add把它添加到窗口上
  235.  
  236. 2、得到一个输入框中的内容用gettext
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. package ziwojieshao;
  245. import java.awt.FlowLayout;
  246. import javax.swing.*;
  247. import java.awt.Container;
  248. import java.awt.event.ActionEvent;
  249. import java.awt.event.ActionListener;
  250.  
  251. public class AA extends JFrame{
  252. public AA()
  253. {
  254. JFrame jf1=new JFrame ("Hello");
  255. jf1.setLayout(new FlowLayout());
  256. jf1.getContentPane().add(new JButton("姓名")) ;
  257. jf1.getContentPane().add(new JTextField("林晓菲,09)) ;
  258.  
  259. JRadioButton j1=new JRadioButton("",true);
  260. JRadioButton j2=new JRadioButton("");
  261. ButtonGroup g=new ButtonGroup();
  262. g.add(j1);
  263. g.add(j2);
  264. JPanel p1=new JPanel();
  265. p1.add(j1);
  266. p1.add(j2);
  267. jf1.getContentPane().add(p1);
  268. jf1.setSize(320,210);
  269.  
  270. String proList[] = { "年龄","民族" ,"籍贯","学号","学院","专业"};
  271. JComboBox comboBox;
  272. Container conPane = getContentPane();
  273. comboBox = new JComboBox(proList);
  274. comboBox.setEditable(true);
  275. conPane.add(comboBox);
  276. JTextField b=new JTextField(20);
  277. jf1.add(conPane);
  278. jf1.add(b);
  279. comboBox.addActionListener(new ActionListener()
  280. {public void actionPerformed(ActionEvent e)
  281. {
  282. if(comboBox.getSelectedItem().equals("年龄"))
  283. b.setText("21");
  284. else if(comboBox.getSelectedItem().equals("民族"))
  285. b.setText("");
  286. else if(comboBox.getSelectedItem().equals("籍贯"))
  287. b.setText("青海 湟中");
  288. else if(comboBox.getSelectedItem().equals("学号"))
  289. b.setText("20173311114");
  290. else if(comboBox.getSelectedItem().equals("学院"))
  291. b.setText("计算机学院");
  292. else if(comboBox.getSelectedItem().equals("专业"))
  293. b.setText("网络工程");
  294. }
  295. });
  296.  
  297.  
  298. jf1.setVisible(true);
  299. }
  300. public static void main (String[] args) {
  301. new AA()
  302. }
  303.  
  304. 二.实验心得
  305.  
  306. 实验操作起来还是有一定的难度的。

  

版权声明:本文为Java199-lxf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Java199-lxf/p/11042270.html