我正在使用扩展JDialog的类。在构造器中,我添加了两个文本字段,其值从属性文件和登录按钮中读取。但问题是当我运行应用程序时JDialog应该在第一个文本字段上自动设置setFocus(),但直到我手动点击出现的框时才会发生。我怎样才能实现这一点。我已经搜索了很多链接并尝试了很多东西,如&#34 ; setFocus()"," requestFocusInWindow()"等等,但没有任何问题。
public FormDetails(){
container = getContentPane();
layout = new GridBagLayout();
container.setLayout(layout);
cons = new GridBagConstraints();
cons.fill = GridBagConstraints.VERTICAL;
final ImageIcon imageIcon = new ImageIcon("images/logo.png");
JLabel pnLabel = new JLabel(imageIcon);
pnLabel.setPreferredSize(new Dimension(150, 90));
final JPanel logoPanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
Toolkit.getDefaultToolkit().getImage("images/bg_color.jpg");
setBackground(new Color(7, 164, 217));
}
};
final Box horiz = Box.createHorizontalBox();
horiz.add(Box.createHorizontalStrut(60));
horiz.add(pnLabel);
horiz.add(Box.createHorizontalStrut(61));
horiz.add(new JLabel());
logoPanel.add(horiz);
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 5;
cons.gridheight = 1;
logoPanel.setBorder(BorderFactory.createMatteBorder(2, 2, 0, 2,Color.BLACK));
layout.setConstraints(logoPanel, cons);
container.add(logoPanel);
final JPanel hPanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
final Image backgroundImage = Toolkit.getDefaultToolkit().getImage("images/c_top.gif");
_graphics.drawImage(backgroundImage, 0, 0, this.getWidth(), this.getHeight(), this);
}
};
heading = new JLabel(" ");
heading.setFont(new Font("Times New Roman", Font.BOLD, 14));
heading.setForeground(Color.RED);
heading.setPreferredSize(new Dimension(271, 10));
hPanel.add(heading);
cons.gridx = 0;
cons.gridy = 1;
cons.gridwidth = 5;
cons.gridheight = 1;
hPanel.setBorder(BorderFactory.createMatteBorder(0, 2, 0, 2,Color.BLACK));
layout.setConstraints(hPanel, cons);
container.add(hPanel);
final JPanel blPanel = new JPanel() {
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
final Image backgroundImage = Toolkit.getDefaultToolkit().getImage(
"images/below_heading.jpg");
_graphics.drawImage(backgroundImage, 0, 0, this.getWidth(), this.getHeight(), this);
}
};
final Box horiz5 = Box.createHorizontalBox();
horiz5.add(Box.createHorizontalStrut(135));
horiz5.add(new JLabel());
horiz5.add(Box.createHorizontalStrut(136));
horiz5.add(new JLabel());
blPanel.add(horiz5);
cons.gridx = 0;
cons.gridy = 2;
cons.gridwidth = 5;
cons.gridheight = 2;
blPanel.setBorder(BorderFactory.createMatteBorder(0, 2, 0, 2,Color.BLACK));
layout.setConstraints(blPanel, cons);
container.add(blPanel);
final JPanel aPanel = new JPanel() {
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
final Image backgroundImage = Toolkit.getDefaultToolkit().getImage(
"images/c_top1.gif");
_graphics.drawImage(backgroundImage, 0, 0, this.getWidth(), this
.getHeight(), this);
}
};
agentId = new JLabel("<html><span style='font-size:10px'>"+"SIP Extension:"+"</span></html>");
agentId.setFont(new Font("Verdana", Font.PLAIN, 12));
agentId.setLayout(new FlowLayout(FlowLayout.LEFT));
agentId.setForeground(Color.DARK_GRAY);
extText = new JTextField(sipExtension,10);
extText.setCaretPosition(sipExtension.length());
extText.requestFocus();
if(extText.getActionListeners().length < 1){
extText.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent _actionEvent) {
if (_actionEvent.getSource() == extText){
button.doClick();
else{
pwdText.requestFocusInWindow();
}
}
}
});
}
final Box horiz1 = Box.createHorizontalBox();
horiz1.add(Box.createHorizontalStrut(18));
horiz1.add(agentId);
horiz1.add(Box.createHorizontalStrut(7));
horiz1.add(extText);
horiz1.add(Box.createHorizontalStrut(36));
horiz1.add(new JLabel());
aPanel.add(horiz1);
cons.gridx = 0;
cons.gridy = 4;
cons.gridwidth = 5;
cons.gridheight = 1;
aPanel.setBorder(BorderFactory.createMatteBorder(0, 2, 0, 2,
Color.BLACK));
layout.setConstraints(aPanel, cons);
container.add(aPanel);
if(sipEnable){
final JPanel pwdPanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
final Image backgroundImage = Toolkit.getDefaultToolkit().getImage(
"images/c_middle.gif");
_graphics.drawImage(backgroundImage, 0, 0, this.getWidth(), this
.getHeight(), this);
super.repaint();
}
};
agentPassword = new JLabel();
agentPassword.setText("Password:");
agentPassword.setFont(new Font("Verdana", Font.PLAIN, 12));
agentPassword.setLayout(new FlowLayout(FlowLayout.LEFT));
agentPassword.setForeground(Color.DARK_GRAY);
pwdText = new JPasswordField(10);
if(pwdText.getActionListeners().length < 1){
pwdText.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent _actionEvent) {
if (_actionEvent.getSource() == pwdText) {
button.doClick();
}
}
});
}
final Box horiz8 = Box.createHorizontalBox();
horiz8.add(Box.createHorizontalStrut(18));
horiz8.add(agentPassword);
horiz8.add(Box.createHorizontalStrut(35));
horiz8.add(pwdText);
horiz8.add(Box.createHorizontalStrut(37));
horiz8.add(new JLabel());
pwdPanel.add(horiz8);
cons.gridx = 0;
cons.gridy = 5;
cons.gridwidth = 10;
cons.gridheight = 1;
pwdPanel.setBorder(BorderFactory.createMatteBorder(0, 2, 0, 2,
Color.BLACK));
layout.setConstraints(pwdPanel, cons);
container.add(pwdPanel);
super.repaint();
}
JPanel bPanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
final Image backgroundImage = Toolkit.getDefaultToolkit().getImage(
"images/c_middle.gif");
_graphics.drawImage(backgroundImage, 0, 0, this.getWidth(), this
.getHeight(), this);
super.repaint();
}
};
agentName = new JLabel("<html><span style='font-size:10px'>"+"CRM ID:"+"</span></html>");
agentName.setFont(new Font("Verdana", Font.PLAIN, 12));
agentName.setForeground(Color.DARK_GRAY);
idText = new JTextField(10);
idText.setText(crmId);
idText.setEditable(false);
if(idText.getActionListeners().length < 1){
idText.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == idText) {
button.doClick();
}
}
});
}
final Box horiz2 = Box.createHorizontalBox();
horiz2.add(Box.createHorizontalStrut(24));
horiz2.add(agentName);
horiz2.add(Box.createHorizontalStrut(43));
horiz2.add(idText);
horiz2.add(Box.createHorizontalStrut(37));
horiz2.add(new JLabel(""));
horiz2.add(new JLabel(""));
bPanel.add(horiz2);
cons.gridx = 0;
cons.gridy = 6;
cons.gridwidth = 7;
cons.gridheight = 1;
bPanel.setBorder(BorderFactory.createMatteBorder(0, 2, 0, 2,Color.BLACK));
layout.setConstraints(bPanel, cons);
container.add(bPanel);
final JPanel blPanel1 = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
final Image backgroundImage = Toolkit.getDefaultToolkit().getImage(
"images/up_button.jpg");
_graphics.drawImage(backgroundImage, 0, 0, this.getWidth(), this
.getHeight(), this);
}
};
final Box horiz6 = Box.createHorizontalBox();
horiz6.add(Box.createHorizontalStrut(135));
horiz6.add(new JLabel());
horiz6.add(Box.createHorizontalStrut(136));
horiz6.add(new JLabel());
blPanel1.add(horiz6);
cons.gridx = 0;
cons.gridy = 6;
cons.gridwidth = 5;
cons.gridheight = 2;
blPanel1.setBorder(BorderFactory.createMatteBorder(0, 2, 0, 2,Color.BLACK));
layout.setConstraints(blPanel1, cons);
container.add(blPanel1);
final JPanel cPanel = new JPanel() {
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
final Image backgroundImage = Toolkit.getDefaultToolkit().getImage(
"images/c_middle.gif");
LOG.debug("::: Drawing backgound ");
_graphics.drawImage(backgroundImage, 0, 0, this.getWidth(), this
.getHeight(), this);
}
};
button = new JButton();
final ImageIcon buttonLogin = new ImageIcon("images/Login.png");
button.setIcon(buttonLogin);
button.setRolloverIcon(new ImageIcon("images/LoginMouseOver.png"));
button.setToolTipText("Connect");
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setPreferredSize(new Dimension(100, 30));
if(button.getActionListeners().length < 1){
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent _actionEvent) {
String extension = extText.getText();
String sipId = idText.getText();
if(sipEnable){
String agentPwd = String.valueOf(pwdText.getPassword());
LogInForm.agentPwd = agentPwd;
}
LogInForm.crmId=sipId;
sipExtension = extension;
String url = "readAgentname_IP.jsp?agentname=" + sipId
+ "&extension=" + extension + "&action=login";
}
});
}
final Box horiz3 = Box.createHorizontalBox();
horiz3.add(Box.createHorizontalStrut(100));
horiz3.add(button);
horiz3.add(Box.createHorizontalStrut(71));
cPanel.add(horiz3);
cons.gridx = 0;
cons.gridy = 8;
cons.gridwidth = 5;
cons.gridheight = 1;
cPanel.setBorder(BorderFactory.createMatteBorder(0, 2, 0, 2,Color.BLACK)); //changed 31-03-2015
layout.setConstraints(cPanel, cons);
container.add(cPanel);
final JPanel linkPanel = new JPanel() {
@Override
public void paintComponent(final Graphics _graphics) {
super.paintComponent(_graphics);
final Image backgroundImage = Toolkit.getDefaultToolkit().getImage(
"images/c_bott.gif");
_graphics.drawImage(backgroundImage, 0, 0, this.getWidth(), this
.getHeight(), this);
super.repaint();
}
};
tncLink = new JButton();
tncLink.setText("<HTML><FONT size=2><FONT color=\"#666666\">Terms and conditions</FONT></FONT></HTML>");
tncLink.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent _actionEvent) {
//performing some action
}
});
tncLink.setBorderPainted(false);
tncLink.setOpaque(false);
tncLink.setBackground(Color.WHITE);
settingButton = new JButton(new ImageIcon("images/setting_icon.png"));
settingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent _actionEvent) {
//performing some action
}
});
settingButton.setBorderPainted(false);
settingButton.setOpaque(false);
settingButton.setBackground(Color.WHITE);
settingButton.setToolTipText("Settings");
final Box horiz7 = Box.createHorizontalBox();
horiz7.add(Box.createHorizontalStrut(78));
horiz7.add(tncLink);
horiz7.add(settingButton);
linkPanel.add(horiz7);
cons.gridx = 0;
cons.gridy = 9;//changed 31-03-2015
cons.gridwidth = 5;
cons.gridheight = 1;
linkPanel.setBorder(BorderFactory.createMatteBorder(0, 2, 2, 2,Color.BLACK));
layout.setConstraints(linkPanel, cons);
container.add(linkPanel);
super.repaint();
setTitle("Agent Connect");
final ImageIcon icon = new ImageIcon(ApplicationConstants.tEnableIcon);
setIconImage(icon.getImage());
setResizable(false);
final Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screensize.width - 300) / 2, (screensize.height - 300) / 2,
302, 345);
pack();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
extText.requestFocusInWindow();
}
});
setResizable(false);
setVisible(true);
setAlwaysOnTop(true);
getRootPane().setDefaultButton(button);
}
这里我试过了extText..requestFocusInWindow();但它不起作用
答案 0 :(得分:1)
没有任何代码,很难找出确切的问题。但我会试一试。
您需要在pack()和setVisible(true)之间调用requestFocusInWindow,以便在显示右侧元素时获得焦点。在构造函数中执行它可能为时过早。
这是一个简短的例子:
JDialog dialog = new JDialog();
dialog.add(textField1);
dialog.add(textField2);
dialog.pack();
textField1.requestFocusInWindow();
dialog.setVisible(true);
推迟焦点请求的另一种方法是向文本字段添加一个窗口侦听器,该窗口应该获得焦点(这可以在对话框的构造函数中完成):
myDialog.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e){
textField1.requestFocusInWindow();
}
});
当然,如果你在构造函数中使用它,则不需要“myDialog”。
这是一个在pack和setVisible之间调用requestFocusInWindow的示例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JDialogFocusDemo extends JFrame {
JDesktopPane desktop;
InternalFrameAdapter adapter;
int frameCount = 0;
public JDialogFocusDemo() {
super("JDialogFocusDemo");
JButton button = new JButton("Open Dialog");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(JDialogFocusDemo.this, "Dialog", true);
JPanel panel = new JPanel();
JTextField textField1 = new JTextField(20);
JTextField textField2 = new JTextField(20);
panel.add(textField1);
panel.add(textField2);
dialog.add(panel);
dialog.pack();
textField2.requestFocusInWindow();
dialog.setVisible(true);
}
});
add(button);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
private static void createAndShowGUI() {
JDialogFocusDemo frame = new JDialogFocusDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
主框架只包含一个按钮。当您单击它时,将显示一个对话框,其中包含两个文本字段,焦点将在第二个文本字段中设置。
以下是使用Window监听器的示例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JDialogFocusDemo extends JFrame {
JDesktopPane desktop;
InternalFrameAdapter adapter;
int frameCount = 0;
public JDialogFocusDemo() {
super("JDialogFocusDemo");
JButton button = new JButton("Open Dialog");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(JDialogFocusDemo.this, "Dialog", true);
JPanel panel = new JPanel();
final JTextField textField1 = new JTextField(20);
final JTextField textField2 = new JTextField(20);
panel.add(textField1);
panel.add(textField2);
dialog.add(panel);
dialog.pack();
dialog.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e){
textField2.requestFocusInWindow();
}
});
dialog.setVisible(true);
}
});
add(button);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
private static void createAndShowGUI() {
JDialogFocusDemo frame = new JDialogFocusDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我可能宁愿选择Window监听器,即使两者都在我的例子中工作。