Java >> Java チュートリアル >  >> Java

JButton の背景色を変更する方法

このチュートリアルでは、メソッド setBackground() を使用して、JButton の背景色を変更する方法を見ていきます。 JButtonクラスの.

JButton の背景色を変更する Java プログラム:
import javax.swing.*;
import java.awt.*;

public class JButtonExample
{  
  public static void main(String[] args) 
  {
    //create a frame
    JFrame frame = new JFrame("JButton Example");
    //create button
    JButton btn = new JButton("Click here");
    //set button position
    btn.setBounds(70,80,100,30);
    //change the background color of JButton
    btn.setBackground(Color.RED);
    //add button to frame
    frame.add(btn);
    frame.setSize(250,250);
    frame.setLayout(null);
    frame.setVisible(true);  
  }
}

出力:

参照 :JButton でテキストの周囲の境界線を削除する方法 参照 :Java で JButton の周囲の境界線を削除する方法
Java タグ