简体   繁体   中英

setBackground won't work not even after doing setOpaque(true) on java

My code goes like this

class ActionPanel extends JPanel{
    private JButton calculate;
    private JLabel sup;
    private Border border;
    public ActionPanel(){
        calculate = new JButton("Calculate Bill");
        calculate.setOpaque(true);
        calculate.setBackground(Color.BLUE);
        sup = new JLabel("Total Cost", SwingConstants.CENTER);
        setLayout(new GridLayout(0,1));
        add(sup);
        add(calculate);
        }    
}

this goes to form part of a JFrame but when it is displayed after running the program the JButton won't change color unless I do setBorderPainted(false); but then the button looks weird because it has no border, what can I do so that the JButton changes color but I don't lose the Border

Do you add the Action panel to the frame AFTER the frame is set visible?

If so, you must call repaint and (re)validate on the frame to make changes visible. Hope this helps, providing more code would help answering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM