才开始接触java,正在一点点学习。对javafx也是赶鸭子上架,想一点,学一点,写一点,但总算是在作业截止日期前把程序完成了。
和同学分享程序时,同学说你得把注释写上。老脸一红,这确实是个坏习惯,一旦投入起来就不会想到注释。趁着现在刚学,赶紧养成习惯!
=========================================================================
一、作业要求
(1)利用JavaFx完成开发界面如图所示,实现如下功能
选中“查看已有名片”按钮,程序处于查看状态,可以在“名片列表”中选择要查看的名片,程序在“名片详实信息”栏显示该名片的详实信息,此状态中名片信息处于不可编辑状态
选中“添加新名片”按钮,程序处于添加状态,所有关于明天信息的项目都处于可编辑状态,设置完成相关信息后,单击“添加”按钮,在“名片列表”中出现新添加名片的名称。单击“清空”按钮可以清空还没提交的内容
在查看状态和添加状态下,都可以通过点击“爱好”、“学历”单选按钮以显示名片的不同附加信息
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class Work1 extends Application {
public static final ObservableList names = FXCollections.observableArrayList();
//可观察的数组 name 用于向listview传入可观测的数据
static List lname = new ArrayList();
static List laddress = new ArrayList();
static List lcon = new ArrayList();
static List lemail= new ArrayList();
static int addcount=0;
static int ccount=0;
static List lsing = new ArrayList();
static List ldance = new ArrayList();
static List linternet = new ArrayList();
static List lfootball = new ArrayList();
static List lbasketball = new ArrayList();
static List lvolleyball = new ArrayList();
static List lxueshi = new ArrayList();
static List lshuoshi = new ArrayList();
static List lboshi = new ArrayList();
static List lother = new ArrayList();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Text tSelectAct = new Text("选择动作");
Text tDetailInfor = new Text("名片详细信息");
Text tList =new Text("名片列表");
Text tAddInfor = new Text("名片附加信息");
RadioButton rbcheck = new RadioButton("查看");
RadioButton rbadd = new RadioButton("添加");
rbadd.setSelected(true);
ToggleGroup groupsa = new ToggleGroup();
rbadd.setToggleGroup(groupsa);
rbcheck.setToggleGroup(groupsa);
Separator ssa1 = new Separator();
ssa1.setMaxWidth(Region.USE_COMPUTED_SIZE);
Separator ssa2 = new Separator();
ssa2.setMaxWidth(Region.USE_COMPUTED_SIZE);
ListView cilist= new ListView(names);
cilist.setEditable(false);
VBox vBoxSA1 = new VBox(rbcheck,rbadd);
vBoxSA1.setStyle("-fx-padding: 5;" +
"-fx-border-insets: 5;"+
"-fx-spacing:8;"+
"-fx-border-style: solid inside;"+
"-fx-border-width: 2;"+
"-fx-border-radius: 8;"+
"-fx-border-color: LightGray;");
VBox vBoxSA2 = new VBox(cilist);
vBoxSA2.setStyle("-fx-padding:10;" +
"-fx-border-insets: 5;"+
"-fx-spacing:5;"+
"-fx-border-style: solid inside;"+
"-fx-border-width: 2;"+
"-fx-border-radius: 8;"+
"-fx-border-color: LightGray;");
VBox vBoxSA3 = new VBox(ssa1,ssa2);
VBox vBox1 = new VBox(tSelectAct,vBoxSA1,vBoxSA3,tList,vBoxSA2);
vBox1.setMaxWidth(250);
vBox1.setMaxHeight(525);
vBox1.setStyle("-fx-padding:15;" +
"-fx-border-insets: 5;"+
"-fx-spacing:10;");
vBox1
Text DIName = new Text("姓名");
TextField tfname = new TextField();
Text DIAddress = new Text("地址");
TextField tfaddress = new TextField();
Text DICon = new Text("联系方式");
TextField tfcon = new TextField();
Text DIEmail = new Text("E-mail");
TextField tfemail = new TextField();
RadioButton rbhobby = new RadioButton("爱好");
rbhobby.setSelected(true);
RadioButton rbxueli = new RadioButton("学历");
ToggleGroup groupdi = new ToggleGroup();
rbhobby.setToggleGroup(groupdi);
rbxueli.setToggleGroup(groupdi);
Button btadd=new Button("添加");
Button btclear = new Button("清空");
Separator sdi1 = new Separator();
sdi1.setMaxWidth(Region.USE_COMPUTED_SIZE);
Separator sdi2 = new Separator();
sdi2.setMaxWidth(Region.USE_COMPUTED_SIZE);
VBox vBoxsdi = new VBox(sdi1,sdi2);
CheckBox sing = new CheckBox("唱歌");
CheckBox dance =new CheckBox("跳舞");
CheckBox internet =new CheckBox("上网");
CheckBox football =new CheckBox("足球");
CheckBox basketball =new CheckBox("篮球");
CheckBox volleyball =new CheckBox("排球");
sing.setSelected(false);
dance.setSelected(false);
internet.setSelected(false);
football.setSelected(false);
basketball.setSelected(false);
basketball.setSelected(false);
RadioButton xueshi =new RadioButton("学士");
RadioButton shuoshi =new RadioButton("硕士");
RadioButton boshi =new RadioButton("博士");
RadioButton other =new RadioButton("其他");
ToggleGroup groupai = new ToggleGroup();
xueshi.setToggleGroup(groupai);
shuoshi.setToggleGroup(groupai);
boshi.setToggleGroup(groupai);
other.setToggleGroup(groupai);
GridPane gridPaneDI1 = new GridPane();
gridPaneDI1.setHgap(60);
gridPaneDI1.setVgap(30);
gridPaneDI1.setPadding(new Insets(20,10,10,10));
gridPaneDI1.setStyle("-fx-padding:15;" +
"-fx-border-insets: 5;"+
"-fx-spacing:5;"+
"-fx-border-style: solid inside;"+
"-fx-border-width: 2;"+
"-fx-border-radius: 8;"+
"-fx-border-color: LightGray;");
gridPaneDI1.add(DIName,0,0,2,1);
gridPaneDI1.add(DIAddress,0,1,2,1);
gridPaneDI1.add(DICon,0,2,2,1);
gridPaneDI1.add(DIEmail,0,3,2,1);
gridPaneDI1.add(tfname,1,0,2,1);
gridPaneDI1.add(tfaddress,1,1,2,1);
gridPaneDI1.add(tfcon,1,2,2,1);
gridPaneDI1.add(tfemail,1,3,2,1);
gridPaneDI1.add(rbhobby,0,4);
gridPaneDI1.add(rbxueli,1,4);
gridPaneDI1.add(btadd,2,4);
gridPaneDI1.add(btclear,3,4);
GridPane gridPaneDI2 = new GridPane();
gridPaneDI2.setHgap(80);
gridPaneDI2.setVgap(20);
gridPaneDI2.setPadding(new Insets(10,10,10,10));
gridPaneDI2.setStyle("-fx-padding:15;" +
"-fx-border-insets: 5;"+
"-fx-spacing:15;"+
"-fx-border-style: solid inside;"+
"-fx-border-width: 2;"+
"-fx-border-radius: 8;"+
"-fx-border-color: LightGray;"+
"-fx-alignment:center");
gridPaneDI2.add(sing,0,0);
gridPaneDI2.add(dance,1,0);
gridPaneDI2.add(internet,2,0);
gridPaneDI2.add(football,0,1);
gridPaneDI2.add(basketball,1,1);
gridPaneDI2.add(volleyball,2,1);
HBox hBoxDI = new HBox(xueshi,shuoshi,boshi,other);
hBoxDI.setStyle("-fx-padding:15;" +
"-fx-border-insets: 5;"+
"-fx-spacing:15;"+
"-fx-border-style: solid inside;"+
"-fx-border-width: 2;"+
"-fx-border-radius: 8;"+
"-fx-border-color: LightGray;"+
"-fx-alignment:center");
hBoxDI.setVisible(false);
hBoxDI.setManaged(false);
VBox vBox2 = new VBox(tDetailInfor,gridPaneDI1,vBoxsdi,tAddInfor,gridPaneDI2,hBoxDI);
vBox2.setStyle("-fx-padding:15;"+
"-fx-border-insets: 5;"+
"-fx-spacing:10;");
/
Separator separator = new Separator();
separator.setOrientation(Orientation.VERTICAL);
separator.setMinHeight(150);
separator.setMaxHeight(Region.USE_COMPUTED_SIZE);
HBox hBox = new HBox(vBox1,separator,vBox2);
Scene scene = new Scene(hBox,800,600);
primaryStage.setScene(scene);
primaryStage.setTitle("名片信息管理");
primaryStage.show();
groupdi.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
@Override
public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
if(rbhobby.isSelected()){
hBoxDI.setVisible(false);
hBoxDI.setManaged(false);
gridPaneDI2.setVisible(true);
gridPaneDI2.setManaged(true);
}
if (rbxueli.isSelected()){
gridPaneDI2.setVisible(false);
gridPaneDI2.setManaged(false);
hBoxDI.setVisible(true);
hBoxDI.setManaged(true);
}
}
});
groupsa.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
@Override
public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
if (rbcheck.isSelected()){
tfname.setEditable(false);
tfaddress.setEditable(false);
tfcon.setEditable(false);
tfemail.setEditable(false);
cilist.setEditable(false);
tfname.setStyle("-fx-background-color:LightGray;");
tfaddress.setStyle("-fx-background-color:LightGray;");
tfcon.setStyle("-fx-background-color:LightGray;");
tfemail.setStyle("-fx-background-color:LightGray;");
sing.setDisable(true);
dance.setDisable(true);
internet.setDisable(true);
football.setDisable(true);
basketball.setDisable(true);
volleyball.setDisable(true);
xueshi.setDisable(true);
boshi.setDisable(true);
shuoshi.setDisable(true);
other.setDisable(true);
btadd.setDisable(true);
}else{
btadd.setDisable(false);
tfname.setEditable(true);
tfaddress.setEditable(true);
tfcon.setEditable(true);
tfemail.setEditable(true);
tfname.setStyle("-fx-background-color:#FFFFFF;");
tfaddress.setStyle("-fx-background-color:#FFFFFF;");
tfcon.setStyle("-fx-background-color:#FFFFFF;");
tfemail.setStyle("-fx-background-color:#FFFFFF;");
sing.setDisable(false);
dance.setDisable(false);
internet.setDisable(false);
football.setDisable(false);
basketball.setDisable(false);
volleyball.setDisable(false);
xueshi.setDisable(false);
boshi.setDisable(false);
shuoshi.setDisable(false);
other.setDisable(false);
btadd.setDisable(false);
}
}
});
btclear.setOnAction((ActionEvent e)->{
tfname.clear();
tfaddress.clear();
tfcon.clear();
tfemail.clear();
sing.setSelected(false);
dance.setSelected(false);
internet.setSelected(false);
football.setSelected(false);
basketball.setSelected(false);
football.setSelected(false);
volleyball.setSelected(false);
xueshi.setSelected(false);
shuoshi.setSelected(false);
boshi.setSelected(false);
other.setSelected(false);
});
if(rbadd.isSelected()==true){
btadd.setOnAction((ActionEvent e)->{
lname.add(tfname.getText());
laddress.add(tfaddress.getText());
lcon.add(tfcon.getText());
lemail.add(tfemail.getText());
names.add(lname.get(addcount));
lsing.add(sing.isSelected());
ldance.add(dance.isSelected());
linternet.add(internet.isSelected());
lfootball.add(football.isSelected());
lbasketball.add(basketball.isSelected());
lvolleyball.add(volleyball.isSelected());
lxueshi.add(xueshi.isSelected());
lshuoshi.add(shuoshi.isSelected());
lboshi.add(boshi.isSelected());
lother.add(other.isSelected());
addcount++;
});
}
cilist.getSelectionModel().selectedItemProperty().addListener(observable -> {
ccount=cilist.getSelectionModel().getSelectedIndex();
tfname.setText((String) lname.get(ccount));
tfaddress.setText((String) laddress.get(ccount));
tfcon.setText((String) lcon.get(ccount));
tfemail.setText((String) lemail.get(ccount));
sing.setSelected((boolean)lsing.get(ccount));
dance.setSelected((boolean) ldance.get(ccount));
internet.setSelected((boolean) linternet.get(ccount));
football.setSelected((boolean) lfootball.get(ccount));
basketball.setSelected((boolean) lbasketball.get(ccount));
volleyball.setSelected((boolean) lvolleyball.get(ccount));
xueshi.setSelected((boolean) lxueshi.get(ccount));
shuoshi.setSelected((boolean) lshuoshi.get(ccount));
boshi.setSelected((boolean) lboshi.get(ccount));
other.setSelected((boolean) lother.get(ccount));
});
}
}
运行结果