记javafx+MyBatis
【摘要】 记javafx+mybatis
环境:JDK1.8+IDEA
#javafx获取屏幕属性值
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.geometry.Rectangle2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class getScreen extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("SceneDemo");
BorderPane borderPane =new BorderPane();
TextArea textArea =new TextArea();
HBox hbox =new HBox();
Button openbaidu =new Button("打开百度");
Button getScreenvalue = new Button("获取屏幕属性值");
hbox.getChildren().addAll(openbaidu,getScreenvalue);
openbaidu.setOnAction((e)->{
HostServices hostServices = getHostServices();
hostServices.showDocument("https://www.baidu.com/");
});
getScreenvalue.setOnAction((e)->{
Screen primary = Screen.getPrimary();
double dpi = primary.getDpi();
System.out.println("当前屏幕dpi:"+dpi);
Rectangle2D rec1 = primary.getBounds();
Rectangle2D rec2 = primary.getVisualBounds();
textArea.appendText("\n----全部屏幕--------");
// textArea.appendText("\n左上角x:"+rec1.getMinX()+"左上角y"+rec1.getMinY());
// textArea.appendText("\n右下角x--"+ rec1.getMaxX()+"右下角y--"+ rec1.getMaxY());
textArea.appendText("\n宽度:"+rec1.getWidth()+"高度"+rec1.getHeight());
textArea.appendText("\n----可以看到的屏幕--------");
// textArea.appendText("\n左上角x:"+rec2.getMinX()+"左上角y"+rec2.getMinY());
// textArea.appendText("\n右下角x--"+ rec2.getMaxX()+"右下角y--"+ rec2.getMaxY());
textArea.appendText("\n宽度:"+rec2.getWidth()+"高度"+rec2.getHeight());
});
borderPane.setTop(hbox);
borderPane.setCenter(textArea);
Scene scene =new Scene(borderPane,400,400);
scene.setCursor(Cursor.CLOSED_HAND);//手
/**
* scene.setCursor(Cursor.HAND);//手,箭头啥的
* Cursor CROSSHAIR 光标十字光标
* Cursor . DEFAULT 光标默认值
* Cursor DISAPPEAR 光标消失
* Cursor CLOSED_HAND 光标闭合手
* Contextmenudemo 上下文菜单演示
* Cursor E _ RESIZE 光标E _ RESIZE
*/
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {launch();}
}
#测试javafx+MyBatis
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.domain.User;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class mybatisjavafxDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("sqlDemo");
BorderPane borderPane =new BorderPane();
HBox hBox =new HBox(5);
Button button =new Button("查询");
hBox.getChildren().addAll(menu11(),button);
borderPane.setTop(hBox);
TextArea textArea =new TextArea();
final TextArea bottom =new TextArea();
//borderPane.setBottom(bottom);
borderPane.setCenter(bottom);
//创建查询事件
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 1.定义mybatis主配置文件名称,从类路径开始(target/clasess)
String resource = "mybatis-config.xml";
//读取mybatis-config.xml文件
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
//创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//(关键)获取sqlSessionFactory对象,从SqlSessionFactory中获取sqlSession
SqlSession sqlSession =sqlSessionFactory.openSession();
//(关键)指定要执行的sql语句的标识。(sql映射文件的namesapce+id标识)
String Sqlmapper ="org.example.dao.UserDao.selectUsers";
//执行sql语句
List<User> userList = sqlSession.selectList(Sqlmapper);
//输出结果
//userList.forEach(users -> System.out.println(users));
for (User u:userList) {
System.out.println(u);
bottom.appendText(String.valueOf(u)+"\n");}
//释放资源
sqlSession.close();
}
});
Scene scene = new Scene(borderPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
public MenuBar menu11(){
MenuBar menuBar =new MenuBar();
Menu create = new Menu("新建");
MenuItem project =new MenuItem("项目");
create.getItems().addAll(project);
Menu system = new Menu("系统设置");
MenuItem aboutus =new MenuItem("关于我们");
MenuItem settings =new MenuItem("系统设置");
MenuItem exit =new MenuItem("退出");
exit.setOnAction(e->{
Platform.exit();
});
system.getItems().addAll(create,aboutus,settings,new SeparatorMenuItem(),exit);
Menu goodsManage = new Menu("商品管理");
MenuItem g1 =new MenuItem("q1");
MenuItem g2 =new MenuItem("q2");
CheckMenuItem t1 =new CheckMenuItem("1");
CheckMenuItem t2 =new CheckMenuItem("2");
Slider slider =new Slider();
CustomMenuItem customMenuItem =new CustomMenuItem(slider);
customMenuItem.setHideOnClick(false);
goodsManage.getItems().addAll(g1,g2,t1,t2,customMenuItem);
menuBar.getMenus().addAll(system,goodsManage);
return menuBar;
};
}
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)