如何在应用程序中使用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.
相关问答
更多ASP.NET身份DbContext混淆(ASP.NET Identity DbContext confusion)
使用DbContext添加信息 - ASP.NET MVC(Adding information using DbContext - ASP.NET MVC)
我的DbContext如何处理?(How is my DbContext being disposed?)
在ASP.NET应用程序中注入.InPerRequestScope是否必须使用DbContext?(Is it mandatory for DbContext to be injected .InPerRequestScope in ASP.NET application?)
DBContext类ASP.Net MVC(DBContext class ASP.Net MVC)
获取实体的DbContext(Get DbContext for Entities)
ASP.NET MVC - DbContext的良好实践(ASP.NET MVC - DbContext good practices)
如何从MVC业务对象引用Entity Framework DbContext?(How to refer to Entity Framework DbContext from MVC business objects?)
如何在ASP.NET MVC应用程序区域中使用不同的Entity Framework DbContext?(How to use different Entity Framework DbContext in the Area of ASP.NET MVC application?)
使用Entity Framework DbContext的类库(Class library using Entity Framework DbContext)
相关文章
更多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自我简介
最新问答
更多您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
在计算机拍照在哪里进入
使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
单页应用程序:页面重新加载(Single Page Application: page reload)
在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!