Hey guys, see this snipped of code:
JFrame loginFrame = new JFrame("Login");
loginFrame.setSize(500,400);
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new GridLayout(0,2));
JLabel header = new JLabel("Login");
JLabel header2 = new JLabel("blahvlah");
JLabel loginLabel = new JLabel("Login");
JTextField loginField = new JTextField(24);
JLabel passLabel = new JLabel("Password");
JTextField passField = new JTextField(24);
loginPanel.add(header);
loginPanel.add(header2);
loginPanel.add(loginLabel);
loginPanel.add(loginField);
loginPanel.add(passLabel);
loginPanel.add(passField);
loginFrame.add(loginPanel);
loginFrame.setVisible(true);
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
the JTextFields are present on the app, but i cannot enter anything i开发者_运维问答nto them? There is no cursor either, what is the problem? Also my exit on close does not seem to work?
thanks
The code you posted does not compile (on the last line you typed loginframe
instead of loginFrame
). Having fixed that, the following program:
public class JFrameTest {
public static void main(String[] args) {
JFrame loginFrame = new JFrame("Login");
loginFrame.setSize(500,400);
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new GridLayout(0,2));
JLabel header = new JLabel("Login");
JLabel header2 = new JLabel("blahvlah");
JLabel loginLabel = new JLabel("Login");
JTextField loginField = new JTextField(24);
JLabel passLabel = new JLabel("Password");
JTextField passField = new JTextField(24);
loginPanel.add(header);
loginPanel.add(header2);
loginPanel.add(loginLabel);
loginPanel.add(loginField);
loginPanel.add(passLabel);
loginPanel.add(passField);
loginFrame.add(loginPanel);
loginFrame.setVisible(true);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
runs just fine. I can enter text in both JTextFields and can also close the JFrame:
精彩评论