实验十二:swing图形界面设计 - 土包子666
- package LXF;
- import java.awt.Container;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.WindowConstants;
- public class DYL extends JFrame{
- public void Create(String title) {
- JFrame jf =new JFrame(title);
- Container c=jf.getContentPane();
- jf.setVisible(true);
- jf.setLayout(null);
- jf.setSize(400,400);
- jf.setBounds(0,0,230,300);
- JButton b1=new JButton("1");
- JButton b2=new JButton("2");
- JButton b3=new JButton("3");
- JButton b4=new JButton("4");
- JButton b5=new JButton("5");
- JButton b6=new JButton("6");
- JButton b7=new JButton("7");
- JButton b8=new JButton("8");
- JButton b9=new JButton("9");
- JButton b10=new JButton("0");
- JButton b11=new JButton("+");
- JButton b12=new JButton("-");
- JButton b13=new JButton("*");
- JButton b14=new JButton("/");
- JButton b15=new JButton("en");
- b1.setBounds(10,10,50,50);
- b2.setBounds(60,10,50,50);
- b3.setBounds(110,10,50,50);
- b4.setBounds(10,60,50,50);
- b5.setBounds(60,60,50,50);
- b6.setBounds(110,60,50,50);
- b7.setBounds(10,110,50,50);
- b8.setBounds(60,110,50,50);
- b9.setBounds(110,110,50,50);
- b10.setBounds(10,160,50,50);
- b11.setBounds(60,160,50,50);
- b12.setBounds(110,160,50,50);
- b13.setBounds(160,10,50,50);
- b14.setBounds(160,60,50,50);
- b15.setBounds(160,110,50,100);
- c.add(b1);
- c.add(b2);
- c.add(b3);
- c.add(b4);
- c.add(b5);
- c.add(b6);
- c.add(b7);
- c.add(b8);
- c.add(b9);
- c.add(b10);
- c.add(b11);
- c.add(b12);
- c.add(b13);
- c.add(b14);
- c.add(b15);
- jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
- public static void main(String[] args) {
- new ShiYan10().Create("计算器");
- }
- }
- 2.总结
- 通过此次实验,很好地熟悉了图形用户界面的设计原则
- package shiyan;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class Jisuanqi extends Frame implements ActionListener{
- public Jisuanqi() {
- super("计算器");
- Frame ff=new Frame("framework test");
- ff.setSize(400,100);
- ff.setLocation(300,240);
- ff.setLayout(new FlowLayout());
- final TextField f1=new TextField("10",8);
- ff.add(f1);
- Label l1=new Label("+");
- ff.add(l1);
- TextField f2=new TextField("10",8);
- ff.add(f2);
- Button b1=new Button("=");
- ff.add(b1);
- TextField f3=new TextField(10);
- ff.add(f3);
- ff.addWindowListener(new myclose());
- ff.setVisible(true);
- b1.addActionListener(new ActionListener()
- {public void actionPerformed(ActionEvent e)
- {double c;
- String s1=f1.getText();
- double a=Integer.parseInt(s1);
- String s2=f2.getText();
- double b=Integer.parseInt(s2);
- c=a+b;
- String m=String.valueOf(c);
- f3.setText(m);
- }
- private double Integer(String s) {
- return 0;
- }
- });
- }
- class myclose implements WindowListener{
- public void windowActivated(WindowEvent arg0) {
- }
- public void windowClosed(WindowEvent arg0) {
- }
- public void windowClosing(WindowEvent arg0) {
- System.exit(0);
- }
- public void windowDeactivated(WindowEvent arg0) {
- }
- public void windowDeiconified(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void windowIconified(WindowEvent arg0) {
- }
- public void windowOpened(WindowEvent arg0) {
- }
- }
- public static void main(String[] args) {
- new Jisuanqi();
- }
- public void actionPerformed(ActionEvent arg0)
- {
- }
- }
- 二、结果
- 10+10=20
- 三、心得
- 1、创建按钮以后,要用add把它添加到窗口上
- 2、得到一个输入框中的内容用gettext
- package ziwojieshao;
- import java.awt.FlowLayout;
- import javax.swing.*;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class AA extends JFrame{
- public AA()
- {
- JFrame jf1=new JFrame ("Hello");
- jf1.setLayout(new FlowLayout());
- jf1.getContentPane().add(new JButton("姓名")) ;
- jf1.getContentPane().add(new JTextField("林晓菲,09)) ;
- JRadioButton j1=new JRadioButton("男",true);
- JRadioButton j2=new JRadioButton("女");
- ButtonGroup g=new ButtonGroup();
- g.add(j1);
- g.add(j2);
- JPanel p1=new JPanel();
- p1.add(j1);
- p1.add(j2);
- jf1.getContentPane().add(p1);
- jf1.setSize(320,210);
- String proList[] = { "年龄","民族" ,"籍贯","学号","学院","专业"};
- JComboBox comboBox;
- Container conPane = getContentPane();
- comboBox = new JComboBox(proList);
- comboBox.setEditable(true);
- conPane.add(comboBox);
- JTextField b=new JTextField(20);
- jf1.add(conPane);
- jf1.add(b);
- comboBox.addActionListener(new ActionListener()
- {public void actionPerformed(ActionEvent e)
- {
- if(comboBox.getSelectedItem().equals("年龄"))
- b.setText("21");
- else if(comboBox.getSelectedItem().equals("民族"))
- b.setText("汉");
- else if(comboBox.getSelectedItem().equals("籍贯"))
- b.setText("青海 湟中");
- else if(comboBox.getSelectedItem().equals("学号"))
- b.setText("20173311114");
- else if(comboBox.getSelectedItem().equals("学院"))
- b.setText("计算机学院");
- else if(comboBox.getSelectedItem().equals("专业"))
- b.setText("网络工程");
- }
- });
- jf1.setVisible(true);
- }
- public static void main (String[] args) {
- new AA()
- }
- 二.实验心得
- 实验操作起来还是有一定的难度的。