因此,我有一个问候程序,其中旁边有3个JTextfields和3个JLabel,我想添加助记符,我相信这是JLabel中JLabel中的字母之一下方的小划线。当用户按下Alt +下划线键时,光标将转到其旁边的JTextfield。这是我的代码,因此只显示不带助记符的简单问候
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GreetingApp extends JFrame {
private static final long serialVersionUID = 1L;
private final JTextField firstNameField,middleNameField,lastNameField;
private final JButton greetingButton;
public GreetingApp() {
super("Greetings");
this.firstNameField = new JTextField(8);
this.middleNameField = new JTextField(8);
this.lastNameField = new JTextField(8);
this.greetingButton = new JButton("Get Greeting");
greetingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent ae){
showGreeting();
}
});
final Container mainPanel = getContentPane();
mainPanel.setLayout(new BorderLayout());
final JPanel inputPanel = new JPanel();
final JPanel buttonPanel = new JPanel();
inputPanel.setLayout(new GridLayout(3,3,3,5));
buttonPanel.setLayout(new FlowLayout());
JSeparator sep = new JSeparator();
inputPanel.add(new JLabel("First Name: "),JLabel.LEFT_ALIGNMENT);
inputPanel.add(firstNameField);
inputPanel.add(new JLabel("MI: "),JLabel.LEFT_ALIGNMENT);
inputPanel.add(middleNameField);
inputPanel.add(new JLabel("Last Name: "),JLabel.LEFT_ALIGNMENT);
inputPanel.add(lastNameField);
mainPanel.add(inputPanel,BorderLayout.PAGE_START);
//buttonPanel.add(sep,BorderLayout.PAGE_START);
mainPanel.add(sep,BorderLayout.CENTER);
buttonPanel.add(greetingButton);
mainPanel.add(buttonPanel,BorderLayout.PAGE_END);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private String getFullName() throws IllegalStateException{
if(firstNameField.getText().trim().length() == 0){
throw new IllegalArgumentException("First name cannot be blank");
}
if(middleNameField.getText().trim().length() > 1){
throw new IllegalArgumentException("Middle intial cannot be greater than 1 letter");
}
if(lastNameField.getText().trim().length() == 0){
throw new IllegalArgumentException("Last name cannot be blank");
}
if(middleNameField.getText().trim().length() ==0){
return "Greetings, "+this.firstNameField.getText()+" "+ this.middleNameField.getText() +this.lastNameField.getText()+"!";
}
return "Greetings, "+this.firstNameField.getText()+" "+ this.middleNameField.getText()+"."+this.lastNameField.getText()+"!";
}
private void showGreeting(){
try{
String message = getFullName();
JOptionPane.showMessageDialog(this, message);
}catch(final IllegalArgumentException iae){
JOptionPane.showMessageDialog(this,
iae.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
try{
for(LookAndFeelInfo info:UIManager.getInstalledLookAndFeels()){
if("Nimbus".equals(info.getName())){
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch(Exception e){
e.getStackTrace();
}
}
}
最佳答案
如果您的JLabel被称为label
而您的JTextField被称为textField
:
label.setDisplayedMnemonic(KeyEvent.VK_N); //replace .VK_N with the appropriate key
将助记符放在标签上,并且:
label.setLabelFor(textField);
会将标签(及其助记符)与适当的文本字段相关联