Use CSS in Java Applications

北城余情 提交于 2020-08-05 07:11:12

问题


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.

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.

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?


回答1:


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.

These are some good tutorials on how to use it: https://www.youtube.com/watch?v=FLkOX4Eez6o

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 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;
  }



回答2:


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.




回答3:


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.

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

JavaFX:
http://docs.oracle.com/javafx/2/overview/jfxpub-overview.htm



来源:https://stackoverflow.com/questions/35372288/use-css-in-java-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!