免责声明:我是Java的新手,但我已经构建了13年的.NET应用程序。
我正在尝试构建这个Java应用程序,它为辅导做一些基本的计算。说实话,这不是一个大的程序,但我甚至无法进入Hello, World!
状态!我有一个要求让它变得困难:
应使用javax.swing组件jButton,jLabel,jTextField,jTextArea,jFrame和jPanel构建GUI。
因此,我下载了NetBeans 7.3并创建了一个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
的构建类型。在我撞到墙壁时,我发现example here(并非直接相关)使用了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();
}
});
}
我如何满足上述要求?我真的以错误的方式处理这件事吗?
/*
* 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));
}
}
答案 0 :(得分:5)
此代码可用作applet或桌面应用。这里:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
// <applet code=TutoringCalculator width=400 height=400></applet>
public class TutoringCalculator extends JApplet {
// The size of an applet is set by the HTML!
//private static final int JFXPANEL_WIDTH_INT = 300;
//private static final int JFXPANEL_HEIGHT_INT = 250;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
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();
}
});
}
private JPanel swingContainer;
@Override
public void init() {
swingContainer = new JPanel(new BorderLayout());
add(swingContainer, BorderLayout.CENTER);
createScene();
}
private void createScene() {
JButton btn = new JButton();
btn.setText("Say 'Hello World'");
JRootPane root = new JRootPane();
root.getContentPane().add(btn);
swingContainer.add(root);
}
}
我无法想象你为什么要使用RootPane
,所以我按原样离开了。
JFrame
但不是JApplet
。由于applet的开发,调试和部署非常困难,我建议你完全专注于让它在一个框架中工作。setLocationByPlatform(true)
。有关演示,请参阅this answer。