本文目錄一覽:
- 1、java中如何設置按鈕文字的大小、顏色和字體?
- 2、如何改變java按鈕中的顏色?
- 3、JAVA中的JButton的背景顏色設置問題。用的是MacBook pro,用eclipse寫的。
- 4、java改變按鈕顏色
java中如何設置按鈕文字的大小、顏色和字體?
submit=newJButton(“登陸”);\x0d\x0a\x0d\x0asubmit.setFont(newFont(“宋體”,Font.PLAIN,16));\x0d\x0a三個參數分別表示:字體,樣式(粗體,斜體等),字號\x0d\x0a\x0d\x0asubmit.setForeground(Color.RED);\x0d\x0a這個表示給組件上的文字設置顏色Color.RED表示紅色\x0d\x0a當然你也可以自己給RGB的值比如submit.setForeground(newColor(215,215,200));\x0d\x0a\x0d\x0aJLabel組件支持HTML標記代碼\x0d\x0ainfoLab=newJLabel(“用戶登陸系統”,JLabel.CENTER);\x0d\x0a\x0d\x0a*注意:地址要單引號引起來。這個表示給用戶登錄系統幾個字增加超鏈接\x0d\x0ainfoLab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));\x0d\x0a\x0d\x0a這個表示給這個文字添加鼠標樣式,當鼠標移動到文字上,鼠標變成手型
如何改變java按鈕中的顏色?
setForeground() 設置前景/字體顏色
setBackground() 設置背景顏色
具體實現:(假設按鈕名稱為:button)
設置紅字:
button.setForeground(Color.red);
設置黑色背影:
button.setBackground(Color.black);
JAVA中的JButton的背景顏色設置問題。用的是MacBook pro,用eclipse寫的。
mac os 默認的border是aqua border(look and feel設置),這個border是一個灰白色填充外加灰色邊界的矩形,在初始化button的時候默認是顯示border的,所以你會發現這個aqua border layer把你的button覆蓋了。此時button上的圖層從底向上分別是backgroundforegroundborder layer.所以你看到的灰白色其實是你的默認border。
設置button顏色的解決有兩種,你可以設置background顏色,然後設置button foreground透明,border層不顯示。
button.setBackground(Color.RED);//設置background顏色
button.setOpaque(true); //foreground設置透明
button.setBorderPainted(false); //最後顯示紅色
或者設置foreground顏色,這樣會把background蓋住,然後設置border層不顯示。
button.setBackGround(Color.RED); // 隨便設,反正會被蓋
button.setForeGround(Color.BLUE); //蓋住背景色
button.setBorderPainted(false); //最後顯示藍色
另外,你也可以自己設置一個lineBorder代替mac os默認的aqua border。
originBorder=BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1);//這個是windows的默認border
button.setForeGround(Color.BLUE);
button.setBorder(originBorder);//最後會顯示一個藍色,帶淺灰邊框的button
這個問題在window是不存在的,因為window的默認border是swing border,是無填充透明邊界,不會蓋住foreground layer和background layer。所以你在windows情況下只要設置好前景色背景色的覆蓋關係就行了(比如設置button透明並設置背景色,或者直接設置前景色)。
覆蓋關係,覆蓋關係,覆蓋關係。重說三。
另外mac第三種解決方式:自己網上關鍵字查找mac java swing look and feel,下載對應的包加入自己的庫,然後在swing的window或者panel主函數第一句先:
try{
UIManager.setLookAndFeel(#這裡是對應的look and feel包名字#);
}catch(Exception e){e.printStackTrace()}
不過我不推薦這個做法,因為換地方的時候沒有庫會異常,然後使用默認的lookandfeel。
java改變按鈕顏色
為yellow、blue、red3個按鈕添加actionlistener,當按鈕點擊時執行setBackground(backgroundColor),同時執行 按鈕.setBackground(backgroundColor)即可,比如:
JButton btnYellow = null;
JButton btnBlue = null;
JButton btnRed = null;
btnYellow.setActionCommand(“yellow”);
btnBlue.setActionCommand(“blue”);
btnRed.setActionCommand(“red”);
public void actionPerformed(ActionEvent e) {
String command = event.getActionCommand();
if( “yellow”.equals(command) ){
setBackground(Color.yellow);
btnYellow.setBackground(Color.yellow);
}else if( “blue”.equals(command) ){
setBackground(Color.blue);
btnBlue.setBackground(Color.blue);
}else if( “red”.equals(command) ){
setBackground(Color.red);
btnRed.setBackground(Color.red);
}
}
寫出了部分代碼
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/150781.html