JPanel p1 = new JPanel(){ private Color color = new Color(141,169,232); @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(color); g.fillRect(0,0,getWidth(),getHeight()); } };
JLabel lblTitle = new JLabel("学生信息管理系统"); p1.add(lblTitle); add(p1,BorderLayout.NORTH); JPanel p2 = new ColorPanel(2) ; p2.add(new JButton("提交")); p2.add(new JButton("退出")); add(p2,BorderLayout.SOUTH); Box box = Box.createVerticalBox(); add(box,BorderLayout.CENTER); FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT,5,10); JPanel p3 =new ColorPanel(3%2); p3.setLayout(flowLayout); p3.add(new JLabel("学生姓名")); p3.add(new JTextField(10)); box.add(p3); JPanel p4 = new ColorPanel(1); p4.setLayout(flowLayout); p4.add(new JLabel("性别")); JRadioButton male = new JRadioButton("男",true); JRadioButton female = new JRadioButton("女",true); ButtonGroup bg = new ButtonGroup(); bg.add(male); bg.add(female); p4.add(male); p4.add(female); JPanel p5 = new ColorPanel(2); p5.setLayout(flowLayout); p5.add(new JLabel("出生年月日")); String[] years = new String[]{"1981年","1982年","1983年"}; JComboBox<String> year = new JComboBox<>(years); String[] months = new String[]{"1月","2月","3月"}; JComboBox<String> month = new JComboBox<>(months); String[] days = new String[]{"1日","2日","3日"}; JComboBox<String> day = new JComboBox<>(days); p5.add(year); p5.add(month); p5.add(day); box.add(p5);
JPanel p6 = new ColorPanel(1); p6.setLayout(flowLayout); JCheckBox checkSpecial = new JCheckBox("特招生源",false); //不知道为何,这里不和背景色混合 checkSpecial.setBackground(p6.getBackground()); p6.add(checkSpecial); box.add(p6); JPanel p7 = new ColorPanel(2); p7.setLayout(flowLayout); JCheckBox part = new JCheckBox("党员",false); JLabel lblpartTime = new JLabel("入党时间"); lblpartTime.setEnabled(false); JTextField FieldpartTime = new JTextField(10); p7.add(part); p7.add(lblpartTime); p7.add(FieldpartTime); box.add(p7); JPanel p8 = new ColorPanel(1); p8.setLayout(flowLayout); p8.add(new JLabel("其他说明")); box.add(p8); setVisible(true); pack(); } }public class StudentManagerFrame { public static void main(String[] args) throws Exception { Enumeration<Object> keys = UIManager.getDefaults().keys(); Object key = null; Object value = null; while (keys.hasMoreElements()) { key = keys.nextElement(); value = UIManager.get(key); if(value instanceof Font) { UIManager.put(key, new Font(Font.DIALOG,Font.PLAIN,12)); } } // 个人喜欢nimbus,java7 UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); new InfoFrame(); System.out.println("Hello World!"); } }
每行内部用FlowLayout,组件靠左
import javax.swing.*;
import java.awt.*;
import java.util.*;class ColorPanel extends JPanel
{
private Color color1 = new Color(223,223,223);
private Color color2 = new Color(241,241,241);
public ColorPanel(int i)
{
switch (i)
{
case 1:setBackground(color1); break;
case 2:setBackground(color2); break;
default:setBackground(color2); break;
}
}}class InfoFrame extends JFrame
{
InfoFrame()
{
super("学生信息管理系统");
setBounds(0,0,500,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel(){
private Color color = new Color(141,169,232);
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(color);
g.fillRect(0,0,getWidth(),getHeight());
}
};
JLabel lblTitle = new JLabel("学生信息管理系统");
p1.add(lblTitle);
add(p1,BorderLayout.NORTH); JPanel p2 = new ColorPanel(2) ;
p2.add(new JButton("提交"));
p2.add(new JButton("退出"));
add(p2,BorderLayout.SOUTH); Box box = Box.createVerticalBox();
add(box,BorderLayout.CENTER);
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT,5,10); JPanel p3 =new ColorPanel(3%2);
p3.setLayout(flowLayout);
p3.add(new JLabel("学生姓名"));
p3.add(new JTextField(10));
box.add(p3); JPanel p4 = new ColorPanel(1);
p4.setLayout(flowLayout);
p4.add(new JLabel("性别"));
JRadioButton male = new JRadioButton("男",true);
JRadioButton female = new JRadioButton("女",true);
ButtonGroup bg = new ButtonGroup();
bg.add(male);
bg.add(female);
p4.add(male);
p4.add(female); JPanel p5 = new ColorPanel(2);
p5.setLayout(flowLayout);
p5.add(new JLabel("出生年月日"));
String[] years = new String[]{"1981年","1982年","1983年"};
JComboBox<String> year = new JComboBox<>(years);
String[] months = new String[]{"1月","2月","3月"};
JComboBox<String> month = new JComboBox<>(months);
String[] days = new String[]{"1日","2日","3日"};
JComboBox<String> day = new JComboBox<>(days);
p5.add(year);
p5.add(month);
p5.add(day);
box.add(p5);
JPanel p6 = new ColorPanel(1);
p6.setLayout(flowLayout);
JCheckBox checkSpecial = new JCheckBox("特招生源",false);
//不知道为何,这里不和背景色混合
checkSpecial.setBackground(p6.getBackground());
p6.add(checkSpecial);
box.add(p6); JPanel p7 = new ColorPanel(2);
p7.setLayout(flowLayout);
JCheckBox part = new JCheckBox("党员",false);
JLabel lblpartTime = new JLabel("入党时间");
lblpartTime.setEnabled(false);
JTextField FieldpartTime = new JTextField(10);
p7.add(part);
p7.add(lblpartTime);
p7.add(FieldpartTime);
box.add(p7); JPanel p8 = new ColorPanel(1);
p8.setLayout(flowLayout);
p8.add(new JLabel("其他说明"));
box.add(p8);
setVisible(true);
pack();
}
}public class StudentManagerFrame
{
public static void main(String[] args) throws Exception
{
Enumeration<Object> keys = UIManager.getDefaults().keys();
Object key = null;
Object value = null;
while (keys.hasMoreElements()) {
key = keys.nextElement();
value = UIManager.get(key);
if(value instanceof Font) {
UIManager.put(key, new Font(Font.DIALOG,Font.PLAIN,12));
}
}
// 个人喜欢nimbus,java7
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); new InfoFrame();
System.out.println("Hello World!");
}
}
import java.awt.*;
import java.awt.event.*;public class StuManaSys extends JFrame implements ActionListener{
//北部
JLabel jbl1;
JPanel jp9;
//处理中间部分
JPanel jp2,jp3,jp4,jp5,jp6,jp7,jp8;
JLabel jbl2,jbl3,jbl4,jbl5,jbl6;
JCheckBox jcb1,jcb2;
JTextField jtfd1,jtfd2;
JRadioButton jrbt1,jrbt2;
ButtonGroup bg;
JComboBox jbb1,jbb2,jbb3;
//南部
JPanel jp1;
JButton jb_1,jb_2;
public static void main(String[] args) {
// TODO Auto-generated method stub
StuManaSys stuManaSys=new StuManaSys();
}
public StuManaSys(){
//北部处理
jbl1=new JLabel("学生基本信息输入",JLabel.CENTER);
jp9=new JPanel(new FlowLayout(FlowLayout.CENTER) );
jp9.setBackground(Color.blue);
jp9.add(jbl1);
//处理中部
jp8=new JPanel(new GridLayout(6, 1));
jbl2=new JLabel("学生姓名");
jtfd1=new JTextField(10);
jp2=new JPanel(new FlowLayout(FlowLayout.LEFT));
jp2.add(jbl2);
jp2.add(jtfd1);
jbl3=new JLabel("性别");
jrbt1=new JRadioButton("男");
jrbt2=new JRadioButton("女");
bg=new ButtonGroup();
bg.add(jrbt1);
bg.add(jrbt2);
jp3=new JPanel(new FlowLayout(FlowLayout.LEFT));
jp3.setBackground(Color.lightGray);
jp3.add(jbl3);
jp3.add(jrbt1);
jp3.add(jrbt2);
jbl4=new JLabel("出生年月日");
String aa[]={"1975年","1976年","1977年","1978年","1979年","1980年"};
jbb1=new JComboBox(aa);
String bb[]={"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
jbb2=new JComboBox(bb);
String cc[]={"1日","2日","3日","4日","5日","6日","7日","8日","9日","10日",
"11日","12日","13日","14日","15日","16日",};
jbb3=new JComboBox(cc);
jp4=new JPanel(new FlowLayout(FlowLayout.LEFT));
jp4.add(jbl4);
jp4.add(jbb1);
jp4.add(jbb2);
jp4.add(jbb3);
jcb1=new JCheckBox("特招生源");
jp5=new JPanel(new FlowLayout(FlowLayout.LEFT));
jp5.setBackground(Color.lightGray);
jp5.add(jcb1);
jcb2=new JCheckBox("党员");
jbl5=new JLabel("入党时间");
jbl5.setForeground(Color.gray);
jtfd2=new JTextField(6);
jp6=new JPanel(new FlowLayout(FlowLayout.LEFT));
jp6.add(jcb2);
jp6.add(jbl5);
jp6.add(jtfd2);
jbl6=new JLabel("其他说明");
jp7=new JPanel(new FlowLayout(FlowLayout.LEFT));
jp7.setBackground(Color.lightGray);
jp7.add(jbl6);
jp8.add(jp2);
jp8.add(jp3);
jp8.add(jp4);
jp8.add(jp5);
jp8.add(jp6);
jp8.add(jp7);
//南部处理
jb_1=new JButton("提交");
jb_1.addActionListener(this);
jb_2=new JButton("退出");
jb_2.addActionListener(this);
jp1=new JPanel();
jp1.add(jb_1);
jp1.add(jb_2);
this.add(jp9,"North");
this.add(jp8,"Center");
this.add(jp1,"South");
this.setTitle("学生管理系统");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 360);
this.setVisible(true);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}}