gpt4 book ai didi

java - 如何更改 gui 中字符串的字体,然后将其恢复正常

转载 作者:行者123 更新时间:2023-12-02 13:32:15 25 4
gpt4 key购买 nike

世界各地的程序员 friend 们大家好,我目前的 GUI 中的一个框架出现了问题。我已设法将消息的字体更改为斜体,颜色更改为灰色,并将文本字段设置为可编辑(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);
}

最佳答案

您需要对 JTextField 执行适当的操作。我能想到的最好的是 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
}
}
<小时/>

Well that did remove the helper message – Христо Петков

正如我提到的...

and I want to keep it – Христо Петков

那么您可能还想使用 FocusListener 接口(interface)中的其他方法:

      @Overwrite
public void focusLost(FocusEvent e){
if(userDidNotEnterTextIn(firstName){ // hopefully you know how to find out...
firstName.setText("Enter your name here");
// do formatting again
}
}

关于java - 如何更改 gui 中字符串的字体,然后将其恢复正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com