本文整理匯總了Java中javax.swing.JButton.requestFocus方法的典型用法代碼示例。如果您正苦於以下問題:Java JButton.requestFocus方法的具體用法?Java JButton.requestFocus怎麽用?Java JButton.requestFocus使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.JButton
的用法示例。
在下文中一共展示了JButton.requestFocus方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setup
import javax.swing.JButton; //導入方法依賴的package包/類
private void setup(String server)
{
okPressed = false;
JLabel usernameLabel = new JLabel("Username");
JLabel passwordLabel = new JLabel("Password");
ok = new JButton("OK");
cancel = new JButton("Cancel");
username = new JTextField();
password = new JPasswordField();
ok.addActionListener(this);
cancel.addActionListener(this);
final int height1 = username.getPreferredSize().height;
final int height2 = ok.getPreferredSize().height;
final int width1 = Math.max(usernameLabel.getPreferredSize().width, passwordLabel.getPreferredSize().width);
final int width2 = cancel.getPreferredSize().width;
final int[] rows = {height1, height1, TableLayout.FILL, height2,};
final int[] cols = {width1, TableLayout.FILL, width2, width2,};
JPanel all = new JPanel(new TableLayout(rows, cols, 5, 5));
all.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
all.add(usernameLabel, new Rectangle(0, 0, 1, 1));
all.add(username, new Rectangle(1, 0, 3, 1));
all.add(passwordLabel, new Rectangle(0, 1, 1, 1));
all.add(password, new Rectangle(1, 1, 3, 1));
all.add(ok, new Rectangle(2, 3, 1, 1));
all.add(cancel, new Rectangle(3, 3, 1, 1));
getContentPane().add(all);
ok.requestFocus();
setSize(310, 150);
setTitle("Login to " + server);
getRootPane().setDefaultButton(ok);
ComponentHelper.centreOnScreen(this);
}
示例2: start
import javax.swing.JButton; //導入方法依賴的package包/類
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
示例3: createAndShowGUI
import javax.swing.JButton; //導入方法依賴的package包/類
private static void createAndShowGUI() {
mainFrame = new JFrame("Bug 8033699 - 8 Tests for Grouped/Non Group Radio Buttons");
btnStart = new JButton("Start");
btnEnd = new JButton("End");
btnMiddle = new JButton("Middle");
JPanel box = new JPanel();
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
box.setBorder(BorderFactory.createTitledBorder("Grouped Radio Buttons"));
radioBtn1 = new JRadioButton("A");
radioBtn2 = new JRadioButton("B");
radioBtn3 = new JRadioButton("C");
ButtonGroup btnGrp = new ButtonGroup();
btnGrp.add(radioBtn1);
btnGrp.add(radioBtn2);
btnGrp.add(radioBtn3);
radioBtn1.setSelected(true);
box.add(radioBtn1);
box.add(radioBtn2);
box.add(btnMiddle);
box.add(radioBtn3);
radioBtnSingle = new JRadioButton("Not Grouped");
radioBtnSingle.setSelected(true);
mainFrame.getContentPane().add(btnStart);
mainFrame.getContentPane().add(box);
mainFrame.getContentPane().add(radioBtnSingle);
mainFrame.getContentPane().add(btnEnd);
mainFrame.getRootPane().setDefaultButton(btnStart);
btnStart.requestFocus();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.Y_AXIS));
mainFrame.setSize(300, 300);
mainFrame.setLocation(200, 200);
mainFrame.setVisible(true);
mainFrame.toFront();
}