在 Java 应用程序中使用 CSS [英] Use CSS in Java Applications
问题描述
我使用 Java 已经有一段时间了,现在我想创建一个新的应用程序,我想知道是否有可能通过新的按钮和类似的东西使 Java 看起来更现代.
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.
所以我开始寻找将 CSS(用于网络编程)应用于 Swing 组件的解决方案,但没有找到真正的解决方案.
So I started searching for solutions to apply CSS (used in web-programming) to Swing components but didn't find a real solution.
我不想使用很多非内置选项,所以使用框架和库应该是我最后的选择.
I dont want to use a lot of not build-in options, so using Frameworks and Libraries should be my last option.
我的问题是:有没有办法将 CSS 应用于我的 Java UI?如果不可能:是否有另一种方法通过向其添加某种样式表来使整个 GUI 看起来不同?
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?
推荐答案
您应该查找 javafx.您可以将 css 文件附加到 GUI.它很容易使用.如果你愿意,我可以给你一些示例代码.
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.
这些是关于如何使用它的一些很好的教程:https://www.youtube.com/watch?v=FLkOX4Eez6o
These are some good tutorials on how to use it: https://www.youtube.com/watch?v=FLkOX4Eez6o
我用eclipse创建了一个css文件右键项目->选择新建->其他->CSS文件
I use eclipse to create a css file right click the project -> select new -> other -> CSS file
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文件------------------------------>
<---------------------------------CSS File------------------------------>
.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;
}
这篇关于在 Java 应用程序中使用 CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!