簡體   English   中英

Put a Jbutton On a Jbutton on java Swing

[英]Put a Jbutton On a Jbutton on java Swing

import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;



public class ButtonOnButton extends JFrame {

    private JPanel contentPane;
    private JButton firstButton; // first Button
    private JButton secondButton; // second Button
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ButtonOnButton frame = new ButtonOnButton();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ButtonOnButton() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
         GridLayout gl = new GridLayout(1,0); // the button is in entire screen now and i want to put the "secondButton" on this red button.
        contentPane.setLayout(gl);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        firstButton = new JButton(""); // 
        secondButton = new JButton("");
        firstButton.setDisabledIcon(new ImageIcon(getClass().getResource("meeple.jpg"))); // disabled icon
        firstButton.setEnabled(false);
        secondButton.setBackground(Color.blue);
        
        contentPane.add(firstButton);
        firstButton.add(secondButton);
        setContentPane(contentPane);
// it work with button enabled, but diswork with button disabled and setDisabledIcon 
//sorry for my english bad i hope sincerly you understand( here decipher ).
    }

}

https://i.stack.imgur.com/f02B9.png

我想在 Jbutton 上放置一個 Jbutton 即 JButton 是 setEnabledFalse 並且有一個 DisabledIcon 並且在頂部有另一個按鈕(在鏈接中它是藍色的)

// I want to put a Jbutton on a Jbutton 
//  i.e The JButton is setEnabledFalse and have a DisabledIcon and has another button on top (in //the link it is blue)

這似乎是按鈕的一個怪癖。 為了顯示禁用圖標,您還需要為按鈕提供常規圖標。

在您的示例中,您只關心禁用的 state,因此您可以共享圖標:

    ImageIcon icon = new ImageIcon(getClass().getResource("meeple.jpg"));
    firstButton.setIcon(icon); 
    firstButton.setDisabledIcon(icon); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM