在 Java 应用程序中使用 CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35372288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use CSS in Java Applications
提问by JetStream
I am working with Java for quite some time now and now I wanted to make a new Application and I was wondering, if it would be possible to make Java look more modern with new buttons and things like this.
我使用 Java 已经有一段时间了,现在我想创建一个新的应用程序,我想知道是否有可能通过新的按钮和类似的东西使 Java 看起来更现代。
So I started searching for solutions to apply CSS (used in web-programming) to Swing components but didn't find a real solution.
因此,我开始寻找将 CSS(用于 Web 编程)应用于 Swing 组件的解决方案,但没有找到真正的解决方案。
I dont want to use a lot of not build-in options, so using Frameworks and Libraries should be my last option.
我不想使用很多非内置选项,所以使用框架和库应该是我最后的选择。
My question is: Is there a way to apply CSS to my Java UI? And if it's not possible: is there another way of making the whole GUI look different by adding some kind of style-sheet to it?
我的问题是:有没有办法将 CSS 应用于我的 Java UI?如果不可能:是否有另一种方法通过向其添加某种样式表来使整个 GUI 看起来不同?
采纳答案by W.Sar
You should look up javafx. You can attach a css file to a GUI. it is fairly easy to use. I can give you some example code if you want.
您应该查找 javafx。您可以将 css 文件附加到 GUI。它很容易使用。如果你愿意,我可以给你一些示例代码。
These are some good tutorials on how to use it: https://www.youtube.com/watch?v=FLkOX4Eez6o
这些是关于如何使用它的一些很好的教程:https: //www.youtube.com/watch?v=FLkOX4Eez6o
I use eclipse to create a css file right click the project -> select new -> other -> CSS file
我用eclipse创建了一个css文件右键项目->选择新建->其他->CSS文件
import javafx.application.*;
import java.text.*;
import java.util.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.input.*;
import javafx.stage.*;
import javafx.geometry.*;
public class Display extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception{
HBox root = new HBox();
Button button = new Button("button");
Label label = new Label("Label");
root.getChildren().addAll(button, label);
Scene scene = new Scene(root, 700,300);
scene.getStylesheets().add("Style.css");
stage.setScene(scene);
stage.setTitle("Title");
stage.show();
}
}
<---------------------------------CSS File------------------------------>
<---------------------------------CSS 文件-------------- ---------------->
.root{
-fx-background-color: linear-gradient(#383838, #838383);
-fx-font-size: 11pt;
}
.label{
-fx-text-fill: #e8e8e8;
}
.button{
-fx-background-color: linear-gradient(#dc9556, #ab4642);
-fx-background-radius: 100;
}
.text-area{
-fx-background-color: white;
-fx-background-radius: 0;
}
回答by Simply Me
What you want is javafx. It allows you to develop an app for desktop and web as well, but you can style it using CSS.
你想要的是javafx。它也允许您为桌面和 Web 开发应用程序,但您可以使用 CSS 对其进行样式设置。
回答by JagerSOE
If you are looking to add CSS to your UI (and you haven't already started), I would take a look at javaFX, which would allow you to do just that.
如果您想将 CSS 添加到您的 UI(并且您还没有开始),我会看看 javaFX,它可以让您做到这一点。
Here is an Oracle tutorial on styling the UI with CSS:
http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/apply-css.htm#CHDGHCDG
这是一个关于使用 CSS 设置 UI 样式的 Oracle 教程:http:
//docs.oracle.com/javase/8/javafx/user-interface-tutorial/apply-css.htm#CHDGHCDG
JavaFX:
http://docs.oracle.com/javafx/2/overview/jfxpub-overview.htm
JavaFX:http:
//docs.oracle.com/javafx/2/overview/jfxpub-overview.htm

