Java で setBackgroundColor を設定の方法
Javaでは、以下のコードで背景色を設定できます。
import javax.swing.*;
import java.awt.*;
public class SetBackgroundColor {
public static void main(String[] args) {
JFrame frame = new JFrame("Set Background Color");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
// 创建一个面板
JPanel panel = new JPanel();
// 设置面板的背景颜色为红色
panel.setBackground(Color.RED);
frame.add(panel);
frame.setVisible(true);
}
}
上記のコードでは、JFrame ウィンドウを作成し、ウィンドウ内に JPanel パネルを作成し、ウィンドウにパネルを追加しています。次に、setBackground メソッドを使用して、パネルの背景色を赤色に設定します。他の色を設定する場合は、Color クラスで提供されている Color.BLUE、Color.GREEN などの定数を使用できます。最後に、setVisible メソッドを呼び出すことでウィンドウを表示します。