Przycisk o danym kolorze i kształcie

0

Świeżo zaczynam Jave ale niestety nie mogę tego robić na spokojnie bo na studiach wolą od razu dać projekt i co tydzień sprawdzić postępy... Tak jak w temacie. Jak stworzyć taki przycisk?

Ps. Jak mam ogarnąć bibliotekę która ma tyle klas i dziedziczą po sb. JButton bd posiadał ze 100 jak nie więcej metod a najgorsze jest to że w żadnej dokumentacji nie jest to uwzględnione i trzeba szukać :(

2

JavaFX + CSS

EDIT

Przykład:

/*
 * FILE UseOfCSSInJavaFx.java
 */

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class UseOfCSSInJavaFx extends Application {
	public Parent createContent() {
		
		/* layout */
		BorderPane layout = new BorderPane();
		
		/* layout -> center */
		VBox vBox = new VBox(10);
		
		/* layout -> center -> buttons */
		String stylesheet = UseOfCSSInJavaFx.class.getResource("StylesheetForButtons.css").toExternalForm();
		
		Button button_1 = new Button("Google...");
		button_1.setId("button_1");
		button_1.getStylesheets().add(stylesheet);
		
		Button button_2 = new Button("...prawdę...");
		button_2.setId("button_2");
		button_2.getStylesheets().add(stylesheet);
		
		Button button_3 = new Button("...Ci powie.");
		button_3.setId("button_3");
		button_3.getStylesheets().add(stylesheet);
				
		/* layout -> center (add items) */
		vBox.getChildren().addAll(button_1, button_2, button_3);
		vBox.setAlignment(Pos.CENTER);
		
		/* return layout */
		layout.setCenter(vBox);
		
		return layout;
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		stage.setScene(new Scene(createContent()));
		stage.setWidth(200);
		stage.setHeight(200);
		stage.show();
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}
/*
 * FILE StylesheetForButtons.css
 */

#button_1 {
    -fx-background-color: 
        #090a0c,
        linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),
        linear-gradient(#20262b, #191d22),
        radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));
    -fx-background-radius: 5,4,3,5;
    -fx-background-insets: 0,1,2,0;
    -fx-text-fill: white;
    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
    -fx-font-family: "Arial";
    -fx-text-fill: linear-gradient(white, #d0d0d0);
    -fx-font-size: 12px;
    -fx-padding: 10 20 10 20;
}

#button_2 {
    -fx-background-color: 
        #3c7fb1,
        linear-gradient(#fafdfe, #e8f5fc),
        linear-gradient(#eaf6fd 0%, #d9f0fc 49%, #bee6fd 50%, #a7d9f5 100%);
    -fx-background-insets: 0,1,2;
    -fx-background-radius: 3,2,1;
    -fx-padding: 3 30 3 30;
    -fx-text-fill: black;
    -fx-font-size: 14px;
}

#button_3 {
    -fx-background-color: 
        linear-gradient(#ffd65b, #e68400),
        linear-gradient(#ffef84, #f2ba44),
        linear-gradient(#ffea6a, #efaa22),
        linear-gradient(#ffe657 0%, #f8c202 50%, #eea10b 100%),
        linear-gradient(from 0% 0% to 15% 50%, rgba(255,255,255,0.9), rgba(255,255,255,0));
    -fx-background-radius: 30;
    -fx-background-insets: 0,1,2,3,0;
    -fx-text-fill: #654b00;
    -fx-font-weight: bold;
    -fx-font-size: 14px;
    -fx-padding: 10 20 10 20;
}

zzz.jpg

Źródło: http://fxexperience.com/2011/12/styling-fx-buttons-with-css/

0

Czyli musze narysować sb ten obiekt i do tego wykorzystuje funkcje paintComponent().

Chce osiągnąć taki efekt:
user image

Więc w metodzie paintComponent() bd musiał sb "narysować" dwa prostokąty i dodać czcionke. Tak powinno się to robić?

Na razie napisałem tyle:

public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D)g;   
        Rectangle rect = new Rectangle(10, 0, 300, 65);
        Rectangle rect1 = new Rectangle(0, 0, 10, 65);
        g2.setPaint(new Color(34,34,34));
        g2.fill(rect);
        g2.setPaint(new Color(34,34,34));
        g2.fill(rect1);
        g2.setPaint(Color.WHITE);
        g2.drawString(this.getText(), 30, 30);
    }
0

Gdzieś musisz ustawić rozmiar przycisku.

class MyButton extends JButton{
     public MyButton(){
         setPreferredSize(new Dimension(...));
     }
}
...
    public void paintComponent(Graphics g){
        super.paintComponent g); //tego brakowało.
        //własne malowanie
0

a po co super.paint... ?

I przy okazji.

public class menuButton extends JButton{
    public menuButton(){
        setSize(new Dimension(300,65));
        setText("TEKST");
        
    }
    public void paintComponent(Graphics g){
        //super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;   
        Rectangle rect = new Rectangle(10, 0, 300, 65);
        Rectangle rect1 = new Rectangle(0, 0, 10, 65);
        g2.setPaint(new Color(34,34,34));
        g2.fill(rect);
        g2.setPaint(new Color(34,34,34));
        g2.fill(rect1);
        g2.setPaint(Color.WHITE);
        g2.setFont(getFont().deriveFont(25));
        g2.drawString(this.getText(), 10, 38);
    }
}

public class Project1 {
    
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(300,300);
        menuButton btnStart = new menuButton();
        
        frame.add(btnStart);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

Dlaczego przycisk mi się rozciąga na całą dostępną przestrzeń? Czytałem coś o Layout ale nie podoba mi się taka metoda. Czy da się tak zrobić żeby po prostu dodawało mi komponenty w określone współrzędnymi miejsca?

2

Jest zbyteczne jeżeli swoim kodem zamalowujesz cały obszar przycisku.

0
super

Oznacza wywołanie konstruktora klasy nadrzędnej.

1

Da się, ale nie powiem jak bo uważam, że należy używać Layoutów.

public class Project1 {
 
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        
        JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(300,300));
        menuButton btnStart = new menuButton();
 
        p.add(btnStart);
        frame.add(p);  
        
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Jak chcesz ustalić rozmiar przycisku, to użyj innej metody

    public menuButton(){
        setPreferredSize(new Dimension(300,65));
        setText("TEKST");
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1