【发布时间】:2011-12-23 12:14:53
【问题描述】:
我已经进行了实验和搜索,但我似乎无法弄清楚我认为什么是简单的事情,即当我的小 GUI 应用程序启动时让我的 START 按钮具有焦点,所以用户所要做的就是按他们的 Enter/Return 键,其效果与他们用鼠标单击 START 按钮的效果相同。这是我的代码。感谢您的帮助:)
private void initialize() {
// Launch the frame:
frame = new JFrame();
frame.setTitle("Welcome!");
frame.setSize(520, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add the image:
ImageIcon heroShotImage = new ImageIcon("heroShot.jpg");
JPanel heroShotPanel = new JPanel();
JLabel heroShot = new JLabel(heroShotImage);
heroShotPanel.add(heroShot);
// Create a panel to hold the "Start" button:
JPanel submitPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
// Create the "Start" button, which launches business logic and dialogs:
JButton start = new JButton("Start");
start.setToolTipText("Click to use library");
start.setFocusable(true); // How do I get focus on button on App launch?
start.requestFocus(true); // Tried a few things and can't get it to work.
// Listen for user actions and do some basic validation:
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// THE APP's LOGIC GOES HERE...
}
// Finish setting up the GUI and its components, listeners, and actions:
submitPanel.add(start);
frame.getContentPane().add(heroShotPanel, BorderLayout.NORTH);
frame.getContentPane().add(submitPanel, BorderLayout.SOUTH);
}
【问题讨论】:
-
+1,因为他足够亲切地向所有回答的人表示感谢。祝你工作顺利。问候
标签: java swing user-interface