標題:
Color 類別
[打印本頁]
作者:
tonyh
時間:
2019-9-28 14:18
標題:
Color 類別
試使用 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();
}
}
複製代碼
作者:
蔡幸融
時間:
2019-9-28 15:34
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.sql.DriverPropertyInfo;
import java.util.concurrent.BrokenBarrierException;
public class Ch115 {
JFrame fm;
JButton btn[]=new JButton[4];
JPanel pn1, pn2, pn3;
Ch115()
{
for(int i=0; i<=3; 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.MAGENTA);
pn1.setBackground(Color.CYAN);
pn2=new JPanel();
pn2.setBorder(new TitledBorder("第二格"));
pn2.setBackground(new Color(100,100,100));
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<=3; i++)
pn3.add(btn[i]);
fm=new JFrame("BorderLayout");
fm.setBounds(100, 100, 300, 200);
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 Ch115();
}
}
複製代碼
作者:
高睿辰
時間:
2019-10-12 13:18
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
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("botton"+(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.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.setBorder(new TitledBorder("2"));
pn3=new JPanel();
pn3.setBorder(new TitledBorder(null,"3",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("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 Ch117();
}
}
複製代碼
作者:
黃宥鈞
時間:
2019-10-12 13:50
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();
}
}
複製代碼
作者:
譚暐霖
時間:
2019-10-12 13:55
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();
}
}
複製代碼
作者:
蕭澧邦
時間:
2019-10-19 13: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://istak.org.tw/seed/)
Powered by Discuz! 7.2