如何在应用程序中使用javax.swing组件?(How do I use javax.swing components in an application?)
免责声明:我是Java的新手,但我已经构建了13年的.NET应用程序。
我正在尝试构建这个Java应用程序,它为辅导做一些基本的计算。 说实话,这不是一个大的计划,但我甚至无法将它带到
Hello, World!
州! 我有一个要求让它变得困难:GUI应该使用javax.swing组件jButton,jLabel,jTextField,jTextArea,jFrame和jPanel构建。
因此,我下载了NetBeans 7.3并
JavaFX in Swing
应用程序中创建了一个JavaFX in Swing
。 默认代码显然有效,但它使用Button
而不是JButton
:private void createScene() { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); fxContainer.setScene(new Scene(root)); }
因此,当我将其更改为使用
JButton
我还必须更改root
构建的类型。 在我撞到墙壁的时候,我发现了一个使用JRootPane
的示例 (不直接相关),我认为这可能会取代StackPane
。 所以我重构了这样的代码:private void createScene() { JButton btn = new JButton(); btn.setText("Say 'Hello World'"); JRootPane root = new JRootPane(); root.getContentPane().add(btn); fxContainer.setScene(new Scene(root)); }
并且该代码很好,除了
fxContainer.setScene(new Scene(root));
因为root
不是Parent
。仅供参考,应用程序类实现
JApplet
并具有如下所示的main
和init
:public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { } JFrame frame = new JFrame("Tutoring Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new TutoringCalculator(); applet.init(); frame.setContentPane(applet.getContentPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); applet.start(); } }); } @Override public void init() { fxContainer = new JFXPanel(); fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT)); add(fxContainer, BorderLayout.CENTER); // create JavaFX scene Platform.runLater(new Runnable() { @Override public void run() { createScene(); } }); }
我如何满足上述要求? 我真的以错误的方式处理这件事吗?
SSCCE
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tutoringcalculator; import java.awt.BorderLayout; import java.awt.Dimension; import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JRootPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; /** * * @author Owner */ public class TutoringCalculator extends JApplet { private static final int JFXPANEL_WIDTH_INT = 300; private static final int JFXPANEL_HEIGHT_INT = 250; private static JFXPanel fxContainer; /** * @param args the command line arguments */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { } JFrame frame = new JFrame("Tutoring Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new TutoringCalculator(); applet.init(); frame.setContentPane(applet.getContentPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); applet.start(); } }); } @Override public void init() { fxContainer = new JFXPanel(); fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT)); add(fxContainer, BorderLayout.CENTER); // create JavaFX scene Platform.runLater(new Runnable() { @Override public void run() { createScene(); } }); } private void createScene() { JButton btn = new JButton(); btn.setText("Say 'Hello World'"); JRootPane root = new JRootPane(); root.getContentPane().add(btn); fxContainer.setScene(new Scene(root)); } }
Disclaimer: I am very new to Java, but I've been building .NET applications for 13 years.
I'm trying to build this Java application that does some basic calculations for tutoring. It's honestly not that big of a program, but I can't even get it to the
Hello, World!
state! I have a requirement that's making it difficult:GUI should be built using javax.swing components jButton, jLabel, jTextField, jTextArea, jFrame, and jPanel.
So, I downloaded NetBeans 7.3 and created a
JavaFX in Swing
application. The default code obviously works but it usesButton
instead ofJButton
:private void createScene() { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); fxContainer.setScene(new Scene(root)); }
so I when I change it to use a
JButton
I have to also change the type theroot
is built from. While banging my head against the wall I found an example here (not directly related) that used theJRootPane
and I thought that might work in place of theStackPane
. So I refactored the code like this:private void createScene() { JButton btn = new JButton(); btn.setText("Say 'Hello World'"); JRootPane root = new JRootPane(); root.getContentPane().add(btn); fxContainer.setScene(new Scene(root)); }
and that code is fine except for
fxContainer.setScene(new Scene(root));
becauseroot
isn't aParent
.FYI, the application class implements
JApplet
and has amain
andinit
that looks like this:public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { } JFrame frame = new JFrame("Tutoring Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new TutoringCalculator(); applet.init(); frame.setContentPane(applet.getContentPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); applet.start(); } }); } @Override public void init() { fxContainer = new JFXPanel(); fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT)); add(fxContainer, BorderLayout.CENTER); // create JavaFX scene Platform.runLater(new Runnable() { @Override public void run() { createScene(); } }); }
How can I fulfill the requirement stated above? Am I really going about this whole thing the wrong way?
SSCCE
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tutoringcalculator; import java.awt.BorderLayout; import java.awt.Dimension; import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JRootPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; /** * * @author Owner */ public class TutoringCalculator extends JApplet { private static final int JFXPANEL_WIDTH_INT = 300; private static final int JFXPANEL_HEIGHT_INT = 250; private static JFXPanel fxContainer; /** * @param args the command line arguments */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { } JFrame frame = new JFrame("Tutoring Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new TutoringCalculator(); applet.init(); frame.setContentPane(applet.getContentPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); applet.start(); } }); } @Override public void init() { fxContainer = new JFXPanel(); fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT)); add(fxContainer, BorderLayout.CENTER); // create JavaFX scene Platform.runLater(new Runnable() { @Override public void run() { createScene(); } }); } private void createScene() { JButton btn = new JButton(); btn.setText("Say 'Hello World'"); JRootPane root = new JRootPane(); root.getContentPane().add(btn); fxContainer.setScene(new Scene(root)); } }
原文:
最满意答案
Xavier,欢迎代码优先!
是的,代码优先是一种很好的方法,承诺太多了。 但现在你已经赶上了。 微软(或据我所知,除此之外)没有聪明的想法,他提出了一种平滑的方式来智能地改变表而不会危及数据和可能的架构。
2年前,实施策略是放弃并重新构建数据库。 这是无法忍受的,因为我们中的许多人没有SU访问权限,而且已经停止了。
为了我从代码中找到的所有优点,我首选DB。 虽然无法轻松保存数据,但注释可以通过好友类进行。
微软已经提出了一些聪明的迁移策略。 我强烈建议你阅读这两篇文章。 代码项目第二名:
1) http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
2) http://www.codeproject.com/Articles/504720/EntityplusFrameworkplusCodeplusFirstplusMigrations
无论您决定继续使用Code-First,它们都应该具有启发性。 我听起来像一个评论家,但我在一个人的优势和另一个人的稳定性之间徘徊。
最后,我认为你不应该保留2个dbcontexts。 您的POCO应在1个上下文中合并。
Xavier, welcome to code-first!
Yes, code-first was a brilliant approach that promised soooo much. But now you've hit the catch. There's no clever mind at Microsoft (or outside as far as I know) who has come up with a smooth way to alter tables intelligently without endangering the data and possibly schema.
2 Years ago, the implementation strategy was to drop and re-build the DB. That was just intolerable as many of us didn't have SU access and were stopped in our tracks.
For all the advantages I found from code first, I prefer DB first. While data can't be preserved easily, annotations can through buddy classes.
Microsoft has come up with some clever Migration Strategies. I strongly suggest you read both articles. Code Project 2nd:
1) http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
2) http://www.codeproject.com/Articles/504720/EntityplusFrameworkplusCodeplusFirstplusMigrations
whether you decide to continue with Code-First, they should be enlightening. I sound like a critic but I'm torn between advantages of one and stability of the other.
Finally, I don't think you should preserve 2 dbcontexts. Your POCOs should be consolidated under 1 context.
相关问答
更多-
我将使用继承自IdentityDbContext的单个Context类。 这样,您可以让上下文知道您的类与IdentityUser和IdentityDbContext的角色之间的任何关系。 IdentityDbContext中的开销很少,它基本上是一个带有两个DbSets的常规DbContext。 一个用于用户,一个用于角色。 I would use a single Context class inheriting from IdentityDbContext. This way you can have ...
-
我认为您的问题是因为您没有将ArticleSubject列表保存到您的数据库中。 您需要先将它们添加到ArticleSubjects DbSet中,然后调用上下文的SaveChanges方法: context.ArticleSubjects.AddRange(articleSubjects); context.SaveChanges(); 现在,如果您使用迁移 ,则在模型更改的情况下,您不会丢弃数据库,因此您可能希望使用@Sam变体来避免重复。 另外,为了执行自定义初始化程序,您必须通过Database属 ...
-
当您使用IoC容器时,您永远不应该调用容器管理的新服务。 在此示例中,您不应使用: using(var context = new MyContext()) _messageRepo = new MessageRepository(context); _idRepo = new IdentityRepository(context); 应该注入您的依赖项(例如,通过构造函数)。 如何使用AutoFac注册您的存储库? 也许你的存储库configurard是单身? 当存储库被重用于第二个http请求时,这可 ...
-
是的,这是强制性的。 你的问题是这样的: 线程A加载一个实体 线程B修改该实体 线程A的下一个请求使用第一个请求中的缓存实体,忽略更改 Yes, it is mandatory. Your problem works like this: Thread A loads an entity Thread B modifies that entity The next request to Thread A uses the cached entity from the first request, ignor ...
-
如果您使用Code First方法,则可以更改Movies属性的名称。 实体框架使用类名(在本例中为class Movie )来计算表名。 默认情况下,它使用以下约定:从类名创建复数并尝试在数据库中查找此类表。 关于数据库名称:即使没有连接字符串,EF也会尝试创建数据库,但它会给它一个不同的名称(例如WebApplication1.MovieDBContext )。 If you are usign Code First approach you can change name of Movies prop ...
-
获取实体的DbContext(Get DbContext for Entities)[2024-01-08]
所以我猜你使用的是EDMX设计器提供的默认代码生成器 - 它将使用ObjectContext和基于EntityObject的重量级实体。 如果你想使用DbContext,你必须: 关闭默认代码生成 - 在属性窗口中删除EDMX文件的自定义工具 下载并安装DbContext T4生成器 (您可以直接从Visual Studio中的扩展管理器获取它) 在EF设计器中,从设计器表面的上下文菜单中选择“ 添加代码生成项” (不在实体上) 现在EF将为你的项目添加两个.tt文件 - 一个负责为你的EDMX文件中定义的 ... -
150实体不是一个巨大的DbContext,但它高于EF在第一个DbContext的初始化中开始出现性能问题的大小。 如果您可以在逻辑上将实体分成负责区域(称为有界上下文),那么您可以考虑使用多个DbContext。 此外,您的应用是否需要使用所有这些实体? 如果没有,您可以简化一些事情。 另请注意,您需要至少EF6才能有效地工作,以前版本的Entity Framework存在多个上下文的问题。 使用多个上下文时也必须小心。 许多人遇到麻烦是因为他们从一个上下文中获取实体,然后在另一个上下文中调用保存更改, ...
-
依赖注入肯定听起来像你在这之后。 我的偏好是ninject,所以下面是我如何用EF做这个的一个例子。 安装Ninject.MVC3(在nuget上可用) 转到\ app_start \ NinjectWebCommon.cs(由上面的包添加)并将以下内容添加到RegisterServices方法 kernel.Bind
().ToSelf().InRequestScope(); //binding in the context in request scope, this will le ... -
Xavier,欢迎代码优先! 是的,代码优先是一种很好的方法,承诺太多了。 但现在你已经赶上了。 微软(或据我所知,除此之外)没有聪明的想法,他提出了一种平滑的方式来智能地改变表而不会危及数据和可能的架构。 2年前,实施策略是放弃并重新构建数据库。 这是无法忍受的,因为我们中的许多人没有SU访问权限,而且已经停止了。 为了我从代码中找到的所有优点,我首选DB。 虽然无法轻松保存数据,但注释可以通过好友类进行。 微软已经提出了一些聪明的迁移策略。 我强烈建议你阅读这两篇文章。 代码项目第二名: 1) http ...
-
确保每个Web.config中都有相同的连接字符串,并且它们指向同一个数据库。 请注意AttachDbFilename相对路径将指向不同的目录。 我打赌你的连接字符串看起来像这样:
相关文章
更多- Custom SOLR Search Components - 2 Dev Tricks
- Professional Android 4 Application Development
- Chapter 9 - Securing Your Application -- Professional ASP.NET MVC 1.0
- Hibernate在Web应用程序中使用示例
- Bentley.STAAD.RCDC.V8i.04.01.01.03 1CD
- 《Java 2 图形设计卷I:AWT》(Graphic Java 1.2 Mastering the JFC Volume I:AWT )扫描版[PDF]
- HTML5应用缓存(application cache)【HTML5教程 - 第十二篇】
- Hadoop I/O系统介绍
- 有没有基于Swing的开源工具,支持组件拖动效果
- Who AM I Casting Crowns自我简介
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。