全世界的程序员们,我目前在GUI中遇到了一个问题。我设法将我的messeges的字体更改为itallic,颜色变为灰色,并且我将textfield设置为editable(true)。问题是,当我删除灰色和斜体的字符串并尝试在文本字段中编写有用的东西时,而不是字符串的普通字体和黑色,它总是显示为斜体和灰色。是否有任何方式,文本消息中的字符串可以更改字体和颜色,然后您编写的下一个字符串具有正常的字体和黑色?非常感谢此时的任何建议。这是我的代码:
void makeSecondFrame(){
frame2 = new JFrame("Step one!Vertification!");
Container contentPane2 = frame2.getContentPane();
JLabel label2 = new JLabel("Welcome to step 1 of our algorithm, Verification!" + "To varify yourself of owning an account please enter the following:");
JLabel label3 = new JLabel("First Name:");
JLabel label4 = new JLabel("Middle Name:");
JLabel label5 = new JLabel("Surname:");
JLabel label6 = new JLabel("Account Number:");
JLabel label7 = new JLabel ("Registration Number:");
firstName = new TextField("Enter your name here");
firstName.setForeground(Color.gray);
firstName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
firstName.setEditable(true);
middleName = new TextField("Enter your middle name here");
middleName.setForeground(Color.gray);
middleName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
middleName.setEditable(true);
surname = new TextField("Enter your surname here");
surname.setForeground(Color.gray);
surname.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
surname.setEditable(true);
accountNumber = new TextField("Enter your main account number");
accountNumber.setForeground(Color.gray);
accountNumber.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
accountNumber.setEditable(true);
registrationNumber = new TextField("Enter your registration number");
registrationNumber.setForeground(Color.gray);
registrationNumber.setFont(new java.awt.Font("Arail", Font.ITALIC, 12));
registrationNumber.setEditable(true);
JPanel p2 = new JPanel(new GridLayout(1,1));
p2.add(label2);
contentPane2.add(p2, BorderLayout.NORTH);
JPanel p3 = new JPanel(new GridLayout(10,1));
p3.add(label3);
p3.add(firstName);
p3.add(label4);
p3.add(middleName);
p3.add(label5);
p3.add(surname);
p3.add(label7);
p3.add(registrationNumber);
p3.add(label6);
p3.add(accountNumber);
contentPane2.add(p3, BorderLayout.WEST);
JButton next = new JButton("Next!");
next.addActionListener(this);
JButton cancel = new JButton("Cancel");
cancel.addActionListener(this);
JPanel p4 = new JPanel(new GridLayout(1,1));
p4.add(next);
p4.add(cancel);
contentPane2.add(p4, BorderLayout.SOUTH);
frame2.pack();
frame2.setVisible(true);
}
答案 0 :(得分:1)
您需要对JTextField采取适当的操作。我能想到的最好的是/usr/local/Cellar/portaudio/19.6.0/lib
:
FocusListener.focusGained()
那确实删除了帮助消息 - ХристоПетков
正如我所提到的......
我希望保留它 - ХристоПетков
然后您可能也想在 firstName = new TextField("Enter your name here");
firstName.setForeground(Color.gray);
firstName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
firstName.setEditable(true);
firstName.addFocuListener(new FocusAdapter(){
@Overwrite
public void focusGained(FocusEvent e){
firstName.setText("");// remove help message, will also remove previously entered text!
firstName.setFont(new JTextField().getFont()); // reset to default
}
}
界面中使用其他方法:
FocusListener