简体   繁体   中英

How to change font size/color of a button in java?

    JFrame f = new JFrame("ia");
    f.setSize(1000, 500);
    f.setLocation(300,100);
     JTextArea textArea = new JTextArea(10, 40);
    f.getContentPane().add(BorderLayout.CENTER, textArea);
     JButton button = new JButton("START");
    //button.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
    //button.setBackgroundColor(Color.BLUE);

    f.getContentPane().add(BorderLayout.CENTER, button);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.append("Button was clicked\n");


The comment areas is what I have tried; the program works otherwise when I run it. I took out all the import/bracket mumbo jumbo to save space.

Did you try this?

        JButton b = new JButton("Test");
        b.setFont(new Font("arial", Font.BOLD, 14));
        b.setForeground(Color.blue);
        b.setBackground(Color.red);

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