问题
I am trying to change the text colour in JTextfield using the JButton but somehow i am not able to find the specific function to do so. Any help would be appreciated. Thanks. Also it would be a real help if i could get the source from where i could search different functions with their use, used in java.
回答1:
I am trying to change the text colour in JTextfield using the JButton but somehow i am not able to find the specific function to do so.
That's because there is no one "function" that changes text color from a JButton. You have to create code to do it. Consider using:
- ActionListener to respond to button press
setForeground(Color newColor)
call on the JTextField to set its foreground/text color.- The specifics on how to make these calls will all depend on your current code, something that we have not seen yet.
- Check the Swing tutorials and do a little more research in the future and show your attempt when asking similar questions. Else we'll have no idea what you may be doing wrong. Please look at the help section as well as how to ask good questions to find out how you can improve this question and increase your odds of getting decent answers.
回答2:
Quick example of what you could do
JButton btn = new JButton("Button");
public void changeColor(Color background, Color foreground){
btn.setBackground(background);
btn.setForeground(foreground);
}
来源:https://stackoverflow.com/questions/32028972/jbutton-changes-text-colour-of-textfield