之前听朋友说过,要我把swing的控件分别写成一个类,再组合成一个界面,昨天试了一下,果然好用:
//主类
package com.project.view2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import com.project.prompt2.*;
public class FL_Date_Check extends JFrame implements ActionListener{
Appyle_Exit Ekm;
Date_gui Tmp,Tmpx;
public FL_Date_Check(){
setLayout(new GridLayout(3, 1));
Tmp = new Date_gui();
Tmpx = new Date_gui();
Ekm = new Appyle_Exit("fl_tk");
Ekm.setFl_tmp(this);
add(Tmp.showBorder3(new TitledBorder("开始日期")));
add(Tmpx.showBorder3(new TitledBorder("结束日期")));
add(Ekm.showBorder4(new TitledBorder("最终信息")));
int width = 250;
int height = 270;
this.setSize(width,height);
Toolkit tk = this.getToolkit();
Dimension dm = tk.getScreenSize();
this.setLocation((int)(dm.getWidth()-width)/2,(int)(dm.getHeight()-height)/2);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new FL_Date_Check();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(Ekm.getJB1())){
System.out.println("+++++++++++++++++++++++++++++++++");
String Comx = (String) Tmp.getCom1().getSelectedItem();
String Comy = (String) Tmp.getCom2().getSelectedItem();
String Comz = (String) Tmp.getCom3().getSelectedItem();
System.out.println("开始日期:"+Comx+" ++ "+Comy+" ++ "+Comz);
String Comx1 = (String) Tmpx.getCom1().getSelectedItem();
String Comy1 = (String) Tmpx.getCom2().getSelectedItem();
String Comz1 = (String) Tmpx.getCom3().getSelectedItem();
System.out.println("结束日期:"+Comx1+" ++ "+Comy1+" ++ "+Comz1);
}else if (e.getSource().equals(Ekm.getJB2())){
System.out.println("取消");
}
}
}
//日期类
package com.project.prompt2;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import com.project.tools.*;
public class Date_gui extends JFrame{
JLabel JL1,JL2;
JComboBox com1,com2,com3;
public JPanel showBorder3(Border b) {
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(1,3,5,5));
String xk = Cam_All_Info.Time_Now;
String[] strarray=xk.split("");
String[] strarrax=xk.split("-");
JPanel jp2 = new JPanel();
jp2.setLayout(new FlowLayout());
Vector<String> tempu=new Vector<String>();
tempu.add(strarrax[0]);
tempu.add("2015");
tempu.add("2016");
tempu.add("2017");
tempu.add("2018");
com1 = new JComboBox<String>(tempu);
com1.setFocusable(false);
com1.setOpaque(false);
com1.setBackground(Color.white);
com1.setFont(MyTools.f2);
jp2.add(com1);
JPanel jp3 = new JPanel();
jp3.setLayout(new FlowLayout());
Vector<String> tempx=new Vector<String>();
tempx.add(strarrax[1]);
for (int i = 1;i < 13;i++)
{
if(i < 10){
String Tmp = String.valueOf(i);
tempx.add(0+Tmp);
}else{
String Tmp = String.valueOf(i);
tempx.add(Tmp);
}
}
com2 = new JComboBox<String>(tempx);
com2.setFocusable(false);
com2.setOpaque(false);
com2.setBackground(Color.white);
com2.setFont(MyTools.f2);
jp3.add(com2);
JPanel jp4 = new JPanel();
jp4.setLayout(new FlowLayout());
Vector<String> tempy=new Vector<String>();
String[] strarraz= strarrax[2].split(" ");
tempy.add(strarraz[0]);
for (int i = 1;i < 32;i++)
{
if(i < 10){
String Tmp = String.valueOf(i);
tempy.add(0+Tmp);
}else{
String Tmp = String.valueOf(i);
tempy.add(Tmp);
}
}
com3 = new JComboBox<String>(tempy);
com3.setFocusable(false);
com3.setOpaque(false);
com3.setBackground(Color.white);
com3.setFont(MyTools.f2);
jp4.add(com3);
jp.add(jp2);
jp.add(jp3);
jp.add(jp4);
jp.setBorder(b);
return jp;
}
public JComboBox getCom1() {
return com1;
}
public void setCom1(JComboBox com1) {
this.com1 = com1;
}
public JComboBox getCom2() {
return com2;
}
public void setCom2(JComboBox com2) {
this.com2 = com2;
}
public JComboBox getCom3() {
return com3;
}
public void setCom3(JComboBox com3) {
this.com3 = com3;
}
}
//确定应用类
package com.project.prompt2;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import com.project.view2.FL_Date_Check;
public class Appyle_Exit extends JFrame implements ActionListener{
JButton JB1,JB2;
JLabel JL1,JL2,JL3,JL4;
String Type_Tmp;
FL_Date_Check Fl_tmp;
public Appyle_Exit(String Type_Tmp)
{
this.Type_Tmp=Type_Tmp;
}
public JPanel showBorder4(Border b) {
JPanel jp = new JPanel();
JB1 = new JButton(" 确定(Y) ");
JB1.addActionListener(this);
JL4 = new JLabel( " " );
JB2 = new JButton(" 取消(N)");
JB2.addActionListener(this);
jp.add(JB1);
jp.add(JL4);
jp.add(JB2);
return jp;
}
public void actionPerformed(ActionEvent e) {
if (Type_Tmp.equals("fl_tk")){
Fl_tmp.actionPerformed(e);
}
}
public FL_Date_Check getFl_tmp() {
return Fl_tmp;
}
public void setFl_tmp(FL_Date_Check fl_tmp) {
Fl_tmp = fl_tmp;
}
public JButton getJB1() {
return JB1;
}
public void setJB1(JButton jB1) {
JB1 = jB1;
}
public JButton getJB2() {
return JB2;
}
public void setJB2(JButton jB2) {
JB2 = jB2;
}
}
结果如下: