
你看看,
------------------------------------------------------------------------------------------------
public class MainFrame extends JFrame implements ActionListener {
// add
public static void main(String[] args){
new MainFrame("MainFrame")
}
InsertPanel ip = null
SelectPanel sp = null
SelectscorePanel sp2 = null
Insert2Panel ip2 = null
DeletePanel dp = null
JPanel pframe
JButton jb1, jb2, jb3
JMenuItem jm11, jm21, jm22, jm23, jm31, jm32
CardLayout clayout
MainFrame(String s) {
super(s)
JMenuBar mb = new JMenuBar()
this.setJMenuBar(mb)
JMenu m1 = new JMenu("系统")
JMenu m2 = new JMenu("基本信息")
JMenu m3 = new JMenu("成绩")
mb.add(m1)
mb.add(m2)
mb.add(m3)
jm11 = new JMenuItem("退出系统")
jm21 = new JMenuItem("输入")
jm22 = new JMenuItem("查询")
jm23 = new JMenuItem("删除")
jm31 = new JMenuItem("输入成绩")
jm32 = new JMenuItem("查询成绩")
m1.add(jm11)
m2.add(jm21)
m2.add(jm22)
m2.add(jm23)
m3.add(jm31)
m3.add(jm32)
jb1 = new JButton(i1)
jb2 = new JButton(i2)
jb3 = new JButton(i3)
JToolBar tb = new JToolBar("系统工具")
tb.add(jb1)
tb.add(jb2)
tb.add(jb3)
add(tb, BorderLayout.NORTH)
jm11.addActionListener(this)
jm21.addActionListener(this)
jm22.addActionListener(this)
jm23.addActionListener(this)
jm31.addActionListener(this)
jm32.addActionListener(this)
jb1.addActionListener(this)
jb2.addActionListener(this)
jb3.addActionListener(this)
clayout = new CardLayout()
pframe = new JPanel(clayout)
add(pframe)
JPanel mainp = new JPanel(new BorderLayout())
JLabel mainl = new JLabel("学生信息管理平台", SwingConstants.CENTER)
mainl.setFont(new Font("Serif", Font.BOLD, 30))
mainp.add(mainl)
pframe.add(mainp, "main")
clayout.show(pframe, "main")
// add
setVisible(true)
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jm21 | e.getSource() == jb1) {
if (ip == null) {
ip = new InsertPanel()
pframe.add(ip, "insert")
}
clayout.show(pframe, "insert")
this.setTitle("输入学生信息")
} else if (e.getSource() == jm22 | e.getSource() == jb2) {
if (sp == null) {
sp = new SelectPanel()
pframe.add(sp, "select")
}
clayout.show(pframe, "select")
this.setTitle("查询学生信息")
} else if (e.getSource() == jm22 | e.getSource() == jb2) {
if (sp == null) {
sp = new SelectPanel()
pframe.add(sp, "select")
}
clayout.show(pframe, "select")
this.setTitle("查询学生信息")
} else if (e.getSource() == jm23) {
if (dp == null) {
dp = new DeletePanel()
pframe.add(dp, "delete")
}
clayout.show(pframe, "delete")
this.setTitle("输入学生成绩")
} else if (e.getSource() == jm31) {
if (ip2 == null) {
ip2 = new Insert2Panel()
pframe.add(ip2, "insert2")
}
clayout.show(pframe, "insert2")
this.setTitle("输入学生成绩")
} else if (e.getSource() == jm32) {
if (sp2 == null) {
sp2 = new SelectscorePanel()
pframe.add(sp2, "select2")
}
clayout.show(pframe, "select2")
this.setTitle("查询学生成绩")
} else if ((e.getSource() == jm11) | (e.getSource() == jb3))
System.exit(0)
}
}
=====================第一个类=============================/**
*
* 实现了系统计算器连续按"="和按"+","-","*","/"进行累记运算的模式
*
* */
import javax.swing.*
import java.awt.*
import java.awt.event.*
public class CalculatorFrame extends JFrame {
private JTextField txtDis = new JTextField("0", 16)//显示文本框
private JButton[] btnGrp = new JButton[17]//17个按钮
private JPanel jpnMain = new JPanel()//主面板
private JPanel jpnNorth = new JPanel()//上面板
private JPanel jpnSouth = new JPanel()//下面板
private double numSaved//保存的前一个数
private String operator = ""//保存的运算符号
private String lastPress = ""//上一次按的按钮("数字","运算符" 或 "等号")
/*构造函数*/
public CalculatorFrame() {
//窗口设置
setTitle("计算器")
setSize(210, 250)
buttonInit()//按钮初始化
//主面板设置
add(jpnMain)
jpnMain.setLayout(null)
jpnMain.add(jpnNorth)
jpnMain.add(jpnSouth)
jpnMain.add(btnGrp[15])
btnGrp[15].setBounds(8, 180, 188, 30)//添加“=”按钮
//上面板设置
jpnNorth.setBounds(8, 10, 190, 30)
jpnNorth.add(txtDis)
txtDis.setHorizontalAlignment(JTextField.RIGHT)//设置文本右对齐
txtDis.setEditable(false)
//下面板设置
jpnSouth.setBounds(8, 60, 190, 120)
jpnSouth.setLayout(new GridLayout(4, 4))
addSouthJpn()//添加17个按钮
//添加监听器
addListener()
}
/*17个按钮显示值初始化*/
public void buttonInit() {
for (int i = 0i <10i++) {
btnGrp[i] = new JButton("" + i)
}
btnGrp[10] = new JButton(".")
btnGrp[11] = new JButton("+")
btnGrp[12] = new JButton("-")
btnGrp[13] = new JButton("*")
btnGrp[14] = new JButton("/")
btnGrp[15] = new JButton("=")
btnGrp[16] = new JButton("C")
for (int i = 0i <17i++) {
btnGrp[i].setFont(new Font("Dialog", Font.PLAIN, 18))
}
}
/*按网格顺序添加除“=”外16个按钮*/
public void addSouthJpn() {
int[] index={7,8,9,14,4,5,6,13,1,2,3,12,0,16,10,11}
for(int i=0i<index.lengthi++){
jpnSouth.add(btnGrp[index[i]])
}
}
/*给按钮添加监听器*/
public void addListener() {
//给数字按钮添加监听器,btnGrp[10]是"."号
for (int i = 0i <= 10i++) {
btnGrp[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String button = ( (JButton) e.getSource()).getText()
//如果有运算 *** 作符或文本框数字为0并且按钮不为“。”
if (lastPress == "运算符" || lastPress == "等号"
|| txtDis.getText().equals("0") &&button != ".") {
txtDis.setText("")//清空文本框
}
if (lastPress == "等号") {
operator = ""//上次按的是=号,这次按数字,清空运算符
}
if (button == "." &&txtDis.getText().indexOf(".") != -1) {
} //这次按的是.文本框内已经包含.这种情况什么都不做,防止.重复
else {
txtDis.setText(txtDis.getText() + button)//文本框添加内容
}
lastPress = "数字"
}
})
}
//给运算 *** 作符按钮添加监听器
for (int i = 11i <= 14i++) {
btnGrp[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (operator != "" &&lastPress == "数字") { //运算符不为空并且之前按的是数字
(btnGrp[15].getActionListeners())[0].actionPerformed(e)//手动调用=号的监听响应实现=号功能
}
numSaved = Double.parseDouble(txtDis.getText())//保存当前文本框的数字
operator = ( (JButton) e.getSource()).getText()//保存运算 *** 作符
lastPress = "运算符"//添加运算符标记,下次文本框清空
}
})
}
//给等号按钮添加监听器
btnGrp[15].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double numNow = Double.parseDouble(txtDis.getText())//当前文本框的数字
//运算符匹配 *** 作
if (operator.equals("+")) {
txtDis.setText( (numSaved + numNow) + "")
}
else if (operator.equals("-")) {
if (lastPress == "等号") { //累记运算时的算法(一直按等号)
txtDis.setText( (numNow - numSaved) + "")
}
else {
txtDis.setText( (numSaved - numNow) + "")
}
}
else if (operator.equals("*")) {
txtDis.setText( (numSaved * numNow) + "")
}
else if (operator.equals("/")) {
if (lastPress == "等号") { //累记运算时的算法(一直按等号)
txtDis.setText( (numNow / numSaved) + "")
}
else {
txtDis.setText( (numSaved / numNow) + "")
}
}
if (lastPress != "等号") { //是第一次,以后按累记运算
numSaved = numNow//保存数改为后一个数
}
lastPress = "等号"//添加运算符标记,下次文本框清空
}
})
//给复位符“C”按钮添加监听器
btnGrp[16].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtDis.setText("0")
numSaved = 0
operator = ""
lastPress = ""
}
})
}
}
=====================第二个类=============================
import javax.swing.*
public class Test {
public Test() {
CalculatorFrame calframe = new CalculatorFrame()//新窗口
calframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//设置关闭
calframe.setLocationRelativeTo(null)//窗口置中
calframe.setVisible(true)//显示窗口
calframe.setDefaultLookAndFeelDecorated(false)//使用windows视感
calframe.setResizable(false)//窗口不可调大小
}
public static void main(String[] args) {
Test test = new Test()
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)