Experiment 111 213

Experiment 11 realize the function 

Source:

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CaculatorDemo extends JFrame {

private static final long serialVersionUID = 1L;

the StringBuilder sBuilder the StringBuilder = new new Private ();
Private Double A; // intermediate variable for storing a first number of input
private Double b; // intermediate variables, a second input for storing the number of
private Double double1; // means for receiving the calculation results
private Integer i; // i for indicating Math


public CaculatorDemo() {
this.setTitle("计算器");
this.setSize(318, 457);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JPanel panel = new JPanel();
this.getContentPane().add(panel);
panel.setLayout(null);


// define a label for displaying input data and results
Final the JLabel = new new label the JLabel ();
label.setBounds (0, 0, 300, 50);
label.setFont (new new the Font ( "Dialog",. 1, 30) );
label.setOpaque (to true); // default due jlabel transparent, the background color is provided directly not valid, the opaque first set to true
label.setBackground (Color.white);
panel.add (label);


// 定义按钮组件
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button0 = new JButton("0");
JButton buttonadd = new JButton("+");// 加
JButton buttonminus = new JButton("-");// 减
JButton buttontime = new JButton("×");// 乘
JButton buttondivid = new JButton("÷");// addition decimalJButton buttondecimal = new JButton ( ".
equalJButton buttonequal = new JButton ( "=

JButton buttondelet = new JButton("←");// 删除
JButton buttonclear = new JButton("C");// 清除


// 定义按钮组件位置
button0.setBounds(0, 50, 100, 60);
button0.setFont(new Font("dialog", 1, 30));
panel.add(button0);
button1.setBounds(100, 50, 100, 60);
button1.setFont(new Font("dialog", 1, 30));
panel.add(button1);
button2.setBounds(200, 50, 100, 60);
button2.setFont(new Font("dialog", 1, 30));
panel.add(button2);
button3.setBounds(0, 110, 100, 60);
button3.setFont(new Font("dialog", 1, 30));
panel.add(button3);
button4.setBounds(100, 110, 100, 60);
button4.setFont(new Font("dialog", 1, 30));
panel.add(button4);
button5.setBounds(200, 110, 100, 60);
button5.setFont(new Font("dialog", 1, 30));
panel.add(button5);
button6.setBounds(0, 170, 100, 60);
button6.setFont(new Font("dialog", 1, 30));
panel.add(button6);
button7.setBounds(100, 170, 100, 60);
button7.setFont(new Font("dialog", 1, 30));
panel.add(button7);
button8.setBounds(200, 170, 100, 60);
button8.setFont(new Font("dialog", 1, 30));
panel.add(button8);
button9.setBounds(0, 230, 100, 60);
button9.setFont(new Font("dialog", 1, 30));
panel.add(button9);
buttonadd.setBounds(100, 230, 100, 60);
buttonadd.setFont(new Font("dialog", 1, 30));
panel.add(buttonadd);//加
buttonminus.setBounds(200, 230, 100, 60);
buttonminus.setFont(new Font("dialog", 1, 30));
panel.add (buttonminus); // Save
buttontime.setBounds(0, 290, 100, 60);
buttontime.setFont(new Font("dialog", 1, 30));
panel.add(buttontime);//乘
buttondivid.setBounds(100, 290, 100, 60);
buttondivid.setFont(new Font("dialog", 1, 30));
panel.add(buttondivid);//除
buttonequal.setBounds(200, 290, 100, 60);
buttonequal.setFont(new Font("dialog", 1, 30));
panel.add(buttonequal);//等于
buttondecimal.setBounds(0, 350, 100, 60);
buttondecimal.setFont(new Font("dialog", 1, 30));
panel.add(buttondecimal);//小数点
buttonclear.setBounds(100, 350, 100, 60);
buttonclear.setFont(new Font("dialog", 1, 30));
panel.add(buttonclear);//复位
buttondelet.setBounds(200, 350, 100, 60);
buttondelet.setFont(new Font("dialog", 1, 30));
panel.add(buttondelet);//删除


// set to the respective operation buttons listener
// numerical input operation. 9 ~ 0
button0.addActionListener (the ActionListener new new () {
public void the actionPerformed (the ActionEvent E) {
sBuilder.append ( "0");
label.setText (sBuilder.toString ());
}
});
button1.addActionListener (the ActionListener new new () {
public void the actionPerformed (the ActionEvent E) {
sBuilder.append ( ". 1");
label.setText (sBuilder.toString ());
}
});
Button2 .addActionListener (new ActionListener () {


@Override
public void actionPerformed(ActionEvent e) {
sBuilder.append("2");
label.setText(sBuilder.toString());
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("3");
label.setText(sBuilder.toString());
}
});
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("4");
label.setText(sBuilder.toString());
}
});
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("5");
label.setText(sBuilder.toString());
}
});
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("6");
label.setText(sBuilder.toString());
}
});
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("7");
label.setText(sBuilder.toString());
}
});
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("8");
label.setText(sBuilder.toString());
}
});
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append("9");
label.setText(sBuilder.toString());
}
});


// 输入运算符操作,需要先判断a是否为0.0
buttonadd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("+");
i = 0;
}
});
buttonminus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("-");
i = 1;
}
});
buttontime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("×");
i = 2;
}
});
buttondivid.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("÷");
i = 3;
}
});
buttonequal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 该判断中间变量是否为空
if (!"".equals(sBuilder.toString()) && (!(a == 0.0))) {
b = Double.parseDouble(sBuilder.toString());
if (i == 0) {
double1 = a + b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
} else if (i == 1) {
double1 = a - b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
} else if (i == 2) {
double1 = a * b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
} else if (i == 3) {
double1 = a / b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
} else {
label.setText(sBuilder.toString());
}
}
}
});
buttondecimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder.append(".");
label.setText(sBuilder.toString());
}
});
buttonclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sBuilder = new StringBuilder();
label.setText("");
}
});
buttondelet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!"".equals(sBuilder.toString())) {
sBuilder.deleteCharAt(sBuilder.length() - 1);
label.setText(sBuilder.toString());
}
}
});


this.setVisible(true);
}

public static void main(String[] args) {
new CaculatorDemo();
}
}

 

Experiment 12 SWING interface design

Source:

ackage jiemian;


import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Container;

public class shao extends JFrame{
public shao()

{

JFrame jf1=new JFrame ("简历");
jf1.setLayout(new FlowLayout());

. jf1.getContentPane () add (new JButton ( " name"));
. jf1.getContentPane () the Add (new new JTextField ( "River before the Eastern Zhou Dynasty", 25));

jf1.getContentPane().add(new JButton("性别")) ;
jf1.getContentPane().add(new JTextField("男",25)) ;

jf1.getContentPane().add(new JButton("年龄")) ;
jf1.getContentPane().add(new JTextField("19",25)) ;

jf1.getContentPane().add(new JButton("民族")) ;
jf1.getContentPane().add(new JTextField("藏",25)) ;

. jf1.getContentPane () add (new JButton ( " place of origin"));
jf1.getContentPane () the Add (new new JTextField ( "Qinghai Yushu", 25)).;

. jf1.getContentPane () add (new JButton ( " Academy"));
. jf1.getContentPane () the Add (new new JTextField ( "Computer Academy", 25));

. jf1.getContentPane () add (new JButton ( " Professional"));
. jf1.getContentPane () the Add (new new JTextField ( "Network", 25));

JPanel p1=new JPanel();
jf1.getContentPane().add(p1);
jf1.setSize(420,310);

Container conPane = getContentPane(); 
jf1.add(conPane);
jf1.setVisible(true);

}

public static void main (String[] args) {

new shao(); 
}

}

Experiment 13 window design

Source:

package chuangkou;

import java.awt.Font;
import javax.swing.*;

public class p {

static void main public (String [] args) {
// TODO method of automatically generating stubs
JFrame frame = new JFrame ( "CV"); // create a form
JPanel p = new JPanel (); // Create panel
frame.add (the p-);
frame.setResizable (false); // not drag form
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setBounds (500,300,560,340);



JTextField Field1 = new new JTextField (); // create a text box
p.add (Field1);
JTextField Field2 = new new JTextField (); // create a text box
p.add (Field2);
JTextField Field3 = new new JTextField (); // create a text box
p.add (Field3);
JTextField Field4 = new new JTextField ( ); // create a text box
p.add (the Field4);


the JLabel the JLabel new new Ll = ( "name:"); // create labels
p.add (Ll);
the JLabel = new new L2 of the JLabel ( "gender:");
P .add (L2);
JLabel L3 = new JLabel ( "Professional:");
p.add (L3);
the JLabel new new L4 = the JLabel ( "Age:");
p.add (L4);
the JLabel = new new L5 of the JLabel ( "Interest:") ;
p.add (L5 of);
the JLabel = new new L6 of the JLabel ( "national:");
p.add (L6 of);
the JLabel = new new L7 of the JLabel ( "origin:");
p.add (L7 of);

the JRadioButton new new R1 = JRadioButton ( "male"); // create a single box
JRadioButton r2 = new JRadioButton ( "F"); 
JRadioButton r3 = new new JRadioButton ( "network"); // create a single box
JRadioButton r4 = new JRadioButton ( "object Interconnection project ");
JRadioButton r5 = new new JRadioButton (" software Engineering "); // create a single box


ButtonGroup group1 = new ButtonGroup (); // create a group, r1 and r2 together
ButtonGroup group2 = new ButtonGroup ( );
group1.add (R1);
group1.add (R2);
group2.add(r3);
group2.add(r4);
group2.add (R5);
p.add (R1);
p.add (R2);
p.add (R3);
p.add (R4);
p.add (R5);
frame.add (P);


a JCheckBox box1 = new JCheckBox ( "basketball"); // create a check box
JCheckBox box2 = new JCheckBox ( "soccer");
JCheckBox BOX3 = new new JCheckBox ( "volleyball");
JCheckBox box4 = new new JCheckBox ( "badminton"); 
the p- .add (box1);
p.add (BOX2);
p.add (BOX3);
p.add (box4);
frame.add (P);

p.setLayout (null); // custom component position

L1.setBounds (110,35,250,25);
L1.setFont (new new the Font ( "bold", Font.BOLD, 15)); // set assembly position
L2.setBounds (110,60,90,30);
L2.setFont (new new font ( "bold", Font.BOLD, 15));
L3.setBounds (110,170,340,35);
L3.setFont (new new font ( "bold", Font.BOLD,15));
L4.setBounds(110,140,140,25);
L4.setFont(new Font("黑体",Font.BOLD,15));
L5.setBounds(100,210,140,25);
L5.setFont(new Font("黑体",Font.BOLD,15));
L6.setBounds(110,115,340,25);
L6.setFont(new Font("黑体",Font.BOLD,15));
L7.setBounds(110,90,340,25);
L7.setFont(new Font("黑体",Font.BOLD,15));

r1.setBounds(200,60,90,30);
r2.setBounds(300,60,110,30);
r3.setBounds(170,172,110,40);
r4.setBounds(280,172,115,40);
r5.setBounds(390,172,130,40);

box1.setBounds (170,210,100,25);
box2.setBounds (270,210,100,25);
box3.setBounds (370,210,100,25);
box4.setBounds (470,210,100,25);

Field1.setBounds (170,35,250,22);
Field2.setBounds (170,140,250,22);
Field3.setBounds (170,90,250,22);
Field4.setBounds (170,115,250,22); 

frame.setVisible (to true); // set the status display window

}

}

Guess you like

Origin www.cnblogs.com/jcdz/p/11073667.html
111