例如,如果您將文本欄位的顏色設定為Color.RED,是否有一種方法可以實際回傳該顏色?
我在 Oracle 日志中找到了一個方法,getCaretColor()但它似乎沒有回傳正確的東西......
uj5u.com熱心網友回復:
JTextField 繼承getBackground()自getForeground()Component。如果我理解你的問題,那應該會給你帶來價值觀。
uj5u.com熱心網友回復:
下面的代碼將回傳一個帶有黑色背景和紅色文本的 JTextField。
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.FlowLayout;
public class test
{
public static void main(String[] args)
{
JTextField textField = new JTextField(20);
JFrame frame = new JFrame("Java"); // Just the window title, name doesn't matter
frame.setLayout(new FlowLayout());
Color color = new Color(255,0,0); // Set the text color
textField.setBackground(Color.BLACK); // Set the black background
textField.setForeground(color);
frame.add(textField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,200);
frame.setVisible(true);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/473169.html
上一篇:更改JButton背景顏色
