package login;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class Demo extends JFrame {
private static final long serialVersionUID = 1L;
static int counter =0;//用来记录输入课程的个数
static String []a1=new String [100];//用来存储课程名字
static String []a2=new String [100];//用来存储教师名字
static String []a3=new String [100];//用来存储上课楼层
static String []a4=new String [100];//用来存储教室名字
public static void main(String args[]) throws Exception{
JFrame f = new JFrame("课程添加程序");
f.setBounds(500, 100, 500, 500);//设置窗体的大小
f.setResizable(false);//设置窗体不可以被放大或者缩小
Container c = f.getContentPane();
c.setBackground(Color.WHITE);//设置窗体背景颜色为白色
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体点击关闭时停止运行
JButton b1 = new JButton("保存");//设置按钮
JLabel l1 = new JLabel("课程名称:"); //设置标签
JLabel l2 = new JLabel("任课教师:");//设置标签
JLabel l3 = new JLabel("上课地点");//设置标签
JComboBox<String> jb1 =new JComboBox<>();//设置下拉列表
JComboBox<String> jb2 =new JComboBox<>();//设置下拉列表
jb1.addItem("王建民");jb1.addItem("刘丹");jb1.addItem("刘立嘉");jb1.addItem("王辉");jb1.addItem("杨子光");//向下拉列表中添加组件
jb2.addItem("一教");jb2.addItem("二教");jb2.addItem("三教");jb2.addItem("基教");//向下拉列表中添加组件
JTextField t3 = new JTextField(10);//设置文本域的长度
f.setLayout(null);//设置绝对布局
JButton b2 =new JButton("重置");
b2.setBounds(180, 420, 150, 30);
JTextField t2 =new JTextField(20);
l1.setBounds(20, 40, 90,90);l1.setFont(new Font("楷体",Font.BOLD,15));//设置每个零件的具体位置,字体,大小,粗细情况
l2.setBounds(20, 140, 90,90);l2.setFont(new Font("楷体",Font.BOLD,15));
l3.setBounds(20, 240, 90,90);l3.setFont(new Font("楷体",Font.BOLD,15));
jb1.setBounds(100, 170, 150,30);jb1.setFont(new Font("楷体",Font.PLAIN,20));
jb2.setBounds(100, 270,150,30);jb2.setFont(new Font("楷体",Font.BOLD,20));
t3.setBounds(100, 70, 150,30);t3.setFont(new Font("楷体",Font.BOLD,20));
b1.setBounds(180, 380, 150,30);b1.setFont(new Font("楷体",Font.BOLD,20));
t2.setBounds(250,270,150,30);b1.setFont(new Font("楷体",Font.BOLD,20));
c.add(b1);c.add(l1);c.add(l2);c.add(l3);c.add(jb1);c.add(jb2);c.add(t3);c.add(t2);//添加组件
f.setVisible(true);//设置窗体可见
b1.setBackground(Color.GREEN);//设置按钮的背景颜色
c.add(b2);b2.setBackground(Color.GREEN);b2.setFont(new Font("楷体",Font.BOLD,20));
b1.addActionListener(new ActionListener() {//添加点击按钮时的动作监听
public void actionPerformed(ActionEvent e) {
int u=0;//用来记录是否重复
String s =(String) t3.getText();//获取文本域中输入的课程名字
for(int i=0;i<counter;i++) {
if(s.equals(a1[i]))
{
JOptionPane.showMessageDialog(null,"课程重复");//判断课程重复
u=1;
t3.setText("");
t2.setText("");
}
}
if(u==0) {
a1[counter]=(String) t3.getText();
a2[counter]=(String) jb1.getSelectedItem();
a3[counter]=(String) jb2.getSelectedItem();
a4[counter]=(String) t2.getText();
counter++;
try {
write();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t3.setText("");
t2.setText("");
}
});
}
public static void write() throws IOException {
FileWriter fw =new FileWriter("E:\\JAVA\\loginSystem.java\\src\\loginSystem\\java\\text.txt");
BufferedWriter bw =new BufferedWriter(fw);
int i=0;
for(i=0;i<counter;i++) {
System.out.println((i+1)+": "+a1[i]+" "+a2[i]+" "+a3[i]+" "+a4[i]);
bw.newLine();
}
bw.close();
}
}
原文地址:https://www.cnblogs.com/hang-hang/p/11712923.html