標題:
Color 類別
[打印本頁]
作者:
tonyh
時間:
2017-9-22 20:47
標題:
Color 類別
本帖最後由 tonyh 於 2019-9-7 14:56 編輯
試使用 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));
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();
}
}
複製代碼
作者:
李知易
時間:
2017-9-22 21:34
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 Ch109 {
JPanel pn1,pn2,pn3;
JButton btn[]=new JButton[4];
JFrame fm;
Ch109()
{
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 Ch109();
}
}
複製代碼
作者:
洪振庭
時間:
2017-9-22 22:03
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 Ch117 {
JPanel pn1,pn2,pn3;
JButton btn[]=new JButton[4];
JFrame fm;
Ch117()
{
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());
pn2=new JPanel();
pn2.setBorder(new TitledBorder("第二區"));
pn3=new JPanel();
pn3.setBorder(new TitledBorder(null,"第三區",TitledBorder.CENTER,TitledBorder.BOTTOM));
pn3.setLayout(new GridLayout(4,1,5,5));
for(int i=0; i<4; i++)
pn3.add(btn[i]);
fm=new JFrame("cococ");
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 Ch117();
}
}
複製代碼
作者:
陳思惟
時間:
2017-9-29 19: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("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();
}
}
複製代碼
作者:
黃璽安
時間:
2017-9-29 20:09
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 Ch110 {
JPanel pn1,pn2,pn3;
JButton btn[]=new JButton[4];
JFrame fm;
Ch110()
{
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 Ch110();
}
}
複製代碼
作者:
曾挺桂
時間:
2017-9-29 21:06
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();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2