一.String包中常用控件类
1.SWING菜单类
JMenuBar,JMenu,JMenuItem
2.文本窗格类
JTextPane
3.按钮类,标签类以及分割线控件
1)按钮类 JButton
2)图标类 ImageIcon
3)标签类 JLabel
4)分割线类JSeparator
package swingtest; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test_Swing1 extends JFrame{ /** * @param args */ JMenuBar jMenuBar1=new JMenuBar(); JMenu jMenu_file=new JMenu("文件"); JMenu jMenu_help=new JMenu("帮助"); JMenuItem jMenu_help_about=new JMenuItem("关于"); JMenuItem jMenu_file_open=new JMenuItem("打开"); JMenuItem jMenu_file_exit=new JMenuItem("退出"); public Test_Swing1() { Container contentPane=getContentPane(); contentPane.setLayout(null); setTitle("学生信息系统"); jMenuBar1.add(jMenu_file); jMenu_file.add(jMenu_file_open); jMenu_file.add(jMenu_file_exit); jMenuBar1.add(jMenu_help); jMenu_help.add(jMenu_help_about); setJMenuBar(jMenuBar1); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we){ System.exit(0); } }); setSize(250,100); setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new Test_Swing1(); } }
二.布局管理器
(1)定义容器类对象
Container contentPane=getContentPane();
(2)对容器类对象设置布局
contentPane.setLayout(null);
(3)设置控件的左上角坐标(x,y)和宽度w,高度h
[控件].setBounds(int x, int y, int w, int h)
(4)加入控件
[容器].add(控件)
三.字体类和颜色类
1.字体类Font
Font(String name,int style,int size)
2.颜色类Color
1)Color(int rgb)
2)Color(int r, int g ,int b)
3)Color(float r,float g,float b)
四.表格控件
1.表格类JTable
2.滚动窗格类JScroolPane
3.默认表格模型类DefaultTableModel
五.SWING综合
package swingtest; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class Test_Swing6 extends Test_Swing4 implements ActionListener{ int num=0; boolean showflag=false,aboutflag=false; private int rows=0,cols=4; private Object[] rowData=new Object[cols]; private DefaultTableModel model=new DefaultTableModel(); private JTable table=new JTable(model); demoFrame demoWin; aboutFrame aboutWin; public Test_Swing6() { Container contentPane=getContentPane(); contentPane.setLayout(null); setTitle("学生信息系统"); jMenu_file_exit.addActionListener(this); jMenu_help_about.addActionListener(this); jButtonQuery.addActionListener(this); jButtonDel.addActionListener(this); jButtonAdd.addActionListener(this); jButtonRevise.addActionListener(this); jButtonUndo.addActionListener(this); jButtonShow.addActionListener(this); demoWin=new demoFrame("学生信息表"); demoWin.setSize(300, 200); aboutWin=new aboutFrame("关于"); aboutWin.setSize(200,100); // TODO Auto-generated constructor stub } boolean flagadd=false; public void actionPerformed(ActionEvent e) { if(e.getSource()==jMenu_file_exit) System.exit(0); if(e.getSource()==jMenu_help_about) { aboutflag=!aboutflag; if(aboutflag) aboutWin.setVisible(true); else aboutWin.setVisible(true); } if(e.getSource()==jButtonQuery) { boolean flagquery=false; String str1=jTextFieldNum.getText(); int k=0; int rowNum=model.getRowCount(); while (k<rowNum) { String str2=(model.getValueAt(k, 0).toString()); if(str1.equals(str2)) { jTextFieldNum.setText(str2); jTextFieldName.setText(model.getValueAt(k, 1).toString()); jTextFieldSchool.setText(model.getValueAt(k,2 ).toString()); jTextFieldClass.setText(model.getValueAt(k, 3).toString()); jTextPane.setForeground(Color.red); jTextPane.setText("查询学号"+str1+"成功!见下表"); flagquery=true; break; } k++; } if(k==rowNum) flagquery=true; if(!flagquery) jTextPane.setText("您查询的学号不存在"); } if(e.getSource()==jButtonDel) { boolean flagdel=false; String str1=jTextFieldNum.getText(); int k=0; int rowNum=model.getRowCount(); while (k<rowNum) { String str2=(model.getValueAt(k, 0).toString()); if(str1.equals(str2)) { model.removeRow(k); jTextPane.setForeground(Color.blue); jTextPane.setText("删除学号"+str1+"成功!"); num--; flagdel=true; break; } k++; } if(!flagdel) jTextPane.setText("删除的学号不存在"); } if(e.getSource()==jButtonAdd) { String emptyS=""; boolean flagadd=false; String str1=jTextFieldNum.getText(); int k=0; int rowNum=model.getRowCount(); while (k<rowNum) { String str2=(model.getValueAt(k, 0).toString()); if(str1.equals(str2)) { jTextPane.setForeground(Color.red); jTextPane.setText("学号重复"); break; } k++; } if(k==rowNum) flagadd=true; if(flagadd) { rowData=new Object[cols]; rowData[0]=jTextFieldNum.getText(); rowData[1]=jTextFieldName.getText(); rowData[2]=jTextFieldSchool.getText(); rowData[3]=jTextFieldClass.getText(); if(emptyS.equals(rowData[0])) { jTextPane.setForeground(Color.red); jTextPane.setText("输入的学号不能为空"); } else if(emptyS.equals(rowData[1])) { jTextPane.setForeground(Color.red); jTextPane.setText("输入的姓名不能为空"); } else{ jTextPane.setForeground(Color.blue); model.insertRow(num, rowData); num++; jTextPane.setText("学号"+jTextFieldNum.getText()+"添加成功"); } } } if(e.getSource()==jButtonRevise) { boolean flagrevise=false; String str1=jTextFieldNum.getText(); int k=0; int rowNum=model.getRowCount(); while (k<rowNum) { String str2=(model.getValueAt(k, 0).toString()); if(str1.equals(str2)) { model.setValueAt(jTextFieldName.getText(), k, 1); model.setValueAt(jTextFieldSchool.getText(), k, 2); model.setValueAt(jTextFieldClass.getText(), k, 3); jTextPane.setForeground(Color.blue); jTextPane.setText("修改学号"+str1+"成功"); flagrevise=true; break; } k++; } if(!flagrevise) { jTextPane.setForeground(Color.red); jTextPane.setText("修改的学号不存在"); } } if(e.getSource()==jButtonUndo) { jTextPane.setText(""); jTextFieldInNum.setText(""); jTextFieldName.setText(""); jTextFieldSchool.setText(""); jTextFieldClass.setText(""); jTextFieldNum.setText(""); } if(e.getSource()==jButtonShow) { showflag=! showflag; if(!showflag) demoWin.setVisible(true); else demoWin.setVisible(false); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Test_Swing6(); } class demoFrame extends Frame { demoFrame(String title) { super(title); model.addColumn("学号"); model.addColumn("姓名"); model.addColumn("学院"); model.addColumn("班级"); JScrollPane jp=new JScrollPane(table); add(jp); } } class aboutFrame extends Frame{ aboutFrame(String title) { super("关于"); add(new JTextArea("学生信息系统")); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); aboutflag=false;} }); } } }