標題:
Color 類別
[打印本頁]
作者:
tonyh
時間:
2016-9-24 13:52
標題:
Color 類別
本帖最後由 tonyh 於 2017-9-22 19:58 編輯
試使用 setBackground() 方法為元件(或容器)設定背景顏色,
以及 setForeground() 方法為元件(或容器)設定前景顏色(即文字的顏色).
P.S. 背景色僅在元件是不透明時才可使用, 因此部分元件或容器無法設定背景色, 譬如 JLabel 及 JFrame 就無法 setBackground()
個元件之顏色設定參考:
btn[0].setBackground(Color.BLUE);
btn[0].setForeground(Color.YELLOW);
btn[1].setBackground(Color.RED);
btn[1].setForeground(Color.WHITE);
btn[2].setBackground(Color.DARK_GRAY);
btn[2].setForeground(Color.GREEN);
btn[3].setBackground(Color.PINK);
btn[3].setForeground(Color.BLUE);
pn1.setBackground(Color.CYAN);
pn2.setBackground(new Color(190,255,0));
[attach]1761[/attach]
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
public class Ch118 {
JPanel pn1,pn2,pn3;
JButton btn[]=new JButton[4];
JFrame fm;
Ch118()
{
for(int i=0; i<4; i++)
btn[i]=new JButton("按鈕"+(i+1));
btn[0].setBackground(Color.BLUE);
btn[0].setForeground(Color.YELLOW);
btn[1].setBackground(Color.RED);
btn[1].setForeground(Color.WHITE);
btn[2].setBackground(Color.DARK_GRAY);
btn[2].setForeground(Color.GREEN);
btn[3].setBackground(Color.PINK);
btn[3].setForeground(Color.BLUE);
pn1=new JPanel();
pn1.setBorder(new EtchedBorder());
pn1.setBackground(Color.CYAN);
pn2=new JPanel();
pn2.setBackground(new Color(190,255,0));
pn2.setLayout(new GridLayout(4,1,5,5));
pn2.setBorder(new TitledBorder("第二區"));
pn3=new JPanel();
pn3.setBorder(new TitledBorder(new LineBorder(Color.RED,2),"第三區",TitledBorder.CENTER,TitledBorder.BOTTOM,null,Color.RED));
pn3.setLayout(new GridLayout(4,1,5,5));
for(int i=0; i<4; i++)
pn3.add(btn[i]);
fm=new JFrame("Color 類別");
fm.setBounds(100, 100, 450, 300);
fm.setVisible(true);
fm.setResizable(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(new GridLayout());
fm.add(pn1);
fm.add(pn2);
fm.add(pn3);
}
public static void main(String[] args) {
new Ch118();
}
}
複製代碼
作者:
張峻瑋
時間:
2016-9-24 16:46
本帖最後由 張峻瑋 於 2016-9-24 16:57 編輯
import javax.swing.*;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
public class Ch118 {
JFrame fm;
JButton btn[]=new JButton[4];
JPanel pn1,pn2,pn3;
Ch118()
{
for(int i=0; i<4; i++)
btn[i]=new JButton("按鈕"+(i+1));
btn[0].setBackground(Color.GREEN);
btn[0].setForeground(Color.BLUE);
btn[1].setBackground(Color.CYAN);
btn[1].setForeground(Color.DARK_GRAY);
btn[2].setBackground(Color.MAGENTA);
btn[2].setForeground(Color.ORANGE);
btn[3].setBackground(Color.YELLOW);
btn[3].setForeground(Color.WHITE);
pn1=new JPanel();
pn1.setBorder(new EtchedBorder());
pn1.setBackground(Color.LIGHT_GRAY);
pn2=new JPanel();
pn2.setBorder(new TitledBorder("第二區"));
pn2.setBackground(new Color(45,80,240));
pn3=new JPanel();
pn3.setBorder(new TitledBorder(new LineBorder(Color.RED,25,true),"第三區",TitledBorder.CENTER,TitledBorder.BOTTOM,null,Color.RED));
pn3.setLayout(new GridLayout(4,1,5,5));
for(int i=0; i<4; i++)
pn3.add(btn[i]);
fm=new JFrame("Color 類別");
fm.setBounds(100, 100, 450, 300);
fm.setVisible(true);
fm.setResizable(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(new GridLayout());
fm.add(pn1);
fm.add(pn2);
fm.add(pn3);
}
public static void main(String[] args) {
new Ch118();
}
}
複製代碼
作者:
劉得恩
時間:
2016-9-24 16:56
import javax.swing.*;
import java.awt.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
public class Ch118 {
private JFrame f;
private JButton btn[]=new JButton[4];
private JPanel p1,p2,p3;
Ch118()
{
for(int i=0;i<4;i++)
btn[i]=new JButton("按鈕"+(i+1));
btn[0].setBackground(Color.BLUE);
btn[0].setForeground(Color.YELLOW);
btn[1].setBackground(Color.RED);
btn[1].setForeground(Color.WHITE);
btn[2].setBackground(Color.DARK_GRAY);
btn[2].setForeground(Color.GREEN);
btn[3].setBackground(Color.PINK);
btn[3].setForeground(Color.BLUE);
p1=new JPanel();
p1.setBorder(new EtchedBorder());
p1.setBackground(Color.CYAN);
p2=new JPanel();
p2.setBorder(new TitledBorder("第二區"));
p2.setBackground(new Color(190,255,0));
p3=new JPanel();
p3.setLayout(new GridLayout(4,1,5,5));
p3.setBorder(new TitledBorder(new LineBorder(Color.RED,2),"第三區",TitledBorder.CENTER,TitledBorder.BOTTOM,null,Color.RED));
for(JButton i:btn)
p3.add(i);
f=new JFrame("\u30df\udcef");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(true);
f.setVisible(true);
f.setBounds(100,100,300,200);
f.setLayout(new GridLayout());
f.add(p1);
f.add(p2);
f.add(p3);
}
public static void main(String[] args) {
new Ch118();
}
}
複製代碼
作者:
李允軒
時間:
2016-9-24 16:56
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
public class Ch01 {
JPanel pn1,pn2,pn3;
JButton btn[]=new JButton[4];
JFrame fm;
Ch01()
{
for(int i=0; i<4; i++)
btn[i]=new JButton("按鈕"+(i+1));
btn[0].setBackground(Color.BLUE);
btn[0].setForeground(Color.YELLOW);
btn[1].setBackground(Color.RED);
btn[1].setForeground(Color.WHITE);
btn[2].setBackground(Color.DARK_GRAY);
btn[2].setForeground(Color.GREEN);
btn[3].setBackground(Color.PINK);
btn[3].setForeground(Color.BLUE);
pn1=new JPanel();
pn1.setBorder(new EtchedBorder());
pn1.setBackground(Color.CYAN);
pn2=new JPanel();
pn2.setBorder(new TitledBorder("第二區"));
pn2.setBackground(Color.ORANGE);
pn3=new JPanel();
pn3.setBorder(new TitledBorder(new LineBorder(Color.RED,2),"第三區",TitledBorder.CENTER,TitledBorder.BOTTOM,null,Color.RED));
pn3.setLayout(new GridLayout(4,1,5,5));
for(int i=0; i<4; i++)
pn3.add(btn[i]);
fm=new JFrame("JPanel & setBorder()");
fm.setBounds(100, 100, 450, 300);
fm.setVisible(true);
fm.setResizable(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(new GridLayout());
fm.add(pn1);
fm.add(pn2);
fm.add(pn3);
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
林宇翔
時間:
2016-9-24 16:57
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
public class Ch118 {
JPanel pn1,pn2,pn3;
JButton btn[]=new JButton[4];
JFrame fm;
Ch118()
{
for(int i=0; i<4; i++)
btn[i]=new JButton("按鈕"+(i+1));
btn[0].setBackground(Color.BLUE);
btn[0].setForeground(Color.YELLOW);
btn[1].setBackground(Color.RED);
btn[1].setForeground(Color.WHITE);
btn[2].setBackground(Color.DARK_GRAY);
btn[2].setForeground(Color.GREEN);
btn[3].setBackground(Color.PINK);
btn[3].setForeground(Color.BLUE);
pn1=new JPanel();
pn1.setBorder(new EtchedBorder());
pn1.setBackground(Color.CYAN);
pn2=new JPanel();
pn2.setBackground(new Color(190,255,0));
pn2.setLayout(new GridLayout(4,1,5,5));
pn2.setBorder(new TitledBorder("第二區"));
pn3=new JPanel();
pn3.setBorder(new TitledBorder(new LineBorder(Color.RED,2),"第三區",TitledBorder.CENTER,TitledBorder.BOTTOM,null,Color.RED));
pn3.setLayout(new GridLayout(4,1,5,5));
for(int i=0; i<4; i++)
pn3.add(btn[i]);
fm=new JFrame("JPanel & setBorder()");
fm.setBounds(100, 100, 450, 300);
fm.setVisible(true);
fm.setResizable(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(new GridLayout());
fm.add(pn1);
fm.add(pn2);
fm.add(pn3);
}
public static void main(String[] args) {
new Ch118();
}
}
複製代碼
作者:
張彥承
時間:
2016-9-24 16:59
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
public class Ch100{
JPanel pn1,pn2,pn3;
JButton btn[]=new JButton[4];
JFrame fm;
Ch100()
{
for(int i=0; i<4; i++)
btn[i]=new JButton("按鈕"+(i+1));
btn[0].setBackground(Color.BLUE);
btn[0].setForeground(Color.YELLOW);
btn[1].setBackground(Color.RED);
btn[1].setForeground(Color.WHITE);
btn[2].setBackground(Color.DARK_GRAY);
btn[2].setForeground(Color.GREEN);
btn[3].setBackground(Color.PINK);
btn[3].setForeground(Color.BLUE);
pn1=new JPanel();
pn1.setBorder(new EtchedBorder());
pn1.setBackground(Color.CYAN);
pn2=new JPanel();
pn2.setBackground(new Color(190,255,0));
pn2.setLayout(new GridLayout(4,1,5,5));
pn2.setBorder(new TitledBorder("第二區"));
pn3=new JPanel();
pn3.setBorder(new TitledBorder(new LineBorder(Color.RED,2),"第三區",TitledBorder.CENTER,TitledBorder.BOTTOM,null,Color.RED));
pn3.setLayout(new GridLayout(4,1,5,5));
for(int i=0; i<4; i++)
pn3.add(btn[i]);
fm=new JFrame("Color 類別");
fm.setBounds(100, 100, 450, 300);
fm.setVisible(true);
fm.setResizable(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(new GridLayout());
fm.add(pn1);
fm.add(pn2);
fm.add(pn3);
}
public static void main(String[] args) {
new Ch100();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2