Java setLocation()事故(Java setLocation() mishap)

编程入门 行业动态 更新时间:2024-10-24 08:19:47
Java setLocation()事故(Java setLocation() mishap)

我正处于创建运行Employee / Customer系统的程序的开始阶段,现在我刚刚创建了Login GUI,但是我遇到了一些问题。

setLocation();

方法。 我将它设置为250,250,但它使我的GUI的高度绝对坚固。 如果有人能解决这个问题,我的代码如下。

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private static final int HEIGHT = 1003; private static final int WIDTH = 400; JTextField _uid = new JTextField(10); JPasswordField _pwd = new JPasswordField(10); JButton _login = new JButton("Login"); JButton _reset = new JButton("Reset"); public Main() { super("Login - Durptech"); Container pane = getContentPane(); setLayout(new FlowLayout()); add(new JLabel("User ID:")); add(_uid); add(new JLabel("Password:")); add(_pwd); add(_login); add(_reset); _reset.addActionListener(new ResetButtonHandler()); setSize(WIDTH, HEIGHT); setVisible(true); setResizable(false); setLocation(250, 250); setDefaultCloseOperation(EXIT_ON_CLOSE); } private class ResetButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { _uid.setText(""); _pwd.setText(""); _uid.requestFocusInWindow(); } } public static void main(String[] args) { new Main(); } }

I'm in the beginning stages of creating a program to operate an Employee/Customer system, right now I have just created the Login GUI, but I am having a little bit of a problem with the

setLocation();

method. I set it to 250, 250, but it makes the height of my GUI go absolutely nuts. My code is below if anyone would be able to fix this.

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private static final int HEIGHT = 1003; private static final int WIDTH = 400; JTextField _uid = new JTextField(10); JPasswordField _pwd = new JPasswordField(10); JButton _login = new JButton("Login"); JButton _reset = new JButton("Reset"); public Main() { super("Login - Durptech"); Container pane = getContentPane(); setLayout(new FlowLayout()); add(new JLabel("User ID:")); add(_uid); add(new JLabel("Password:")); add(_pwd); add(_login); add(_reset); _reset.addActionListener(new ResetButtonHandler()); setSize(WIDTH, HEIGHT); setVisible(true); setResizable(false); setLocation(250, 250); setDefaultCloseOperation(EXIT_ON_CLOSE); } private class ResetButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { _uid.setText(""); _pwd.setText(""); _uid.requestFocusInWindow(); } } public static void main(String[] args) { new Main(); } }

最满意答案

我自己,我使用JOptionPane而不是JFrame来创建这个对话框:

import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import javax.swing.*; @SuppressWarnings("serial") public class UserIdPasswordPanel extends JPanel { JTextField selectionField = new JTextField(10); JTextField userIDField = new JTextField(10); JTextField passwordField = new JTextField(10); public UserIdPasswordPanel(JFrame frame) { add(new JButton(new AbstractAction("Login") { @Override public void actionPerformed(ActionEvent arg0) { LoginPane loginPane = new LoginPane(UserIdPasswordPanel.this, "My Login Panel"); int result = loginPane.show(); System.out.println("" + result); if (result >= 0) { String selectionText = LoginPane.options[result].toString(); if (selectionText.equals(LoginPane.LOGIN)) { System.out.println("do some login action here"); } else if (selectionText.equals(LoginPane.RESET)) { System.out.println("do some reset action here"); } // just to show that this works: selectionField.setText(selectionText); userIDField.setText(loginPane.getUserId()); // !! never do this !!!!! passwordField.setText(new String(loginPane.getPassword())); } } })); selectionField.setEditable(false); userIDField.setEditable(false); passwordField.setEditable(false); add(new JLabel("Selection: ")); add(selectionField); add(new JLabel("ID: ")); add(userIDField); add(new JLabel("Password: ")); add(passwordField); } private static void createAndShowGui() { JFrame frame = new JFrame("UserIdPasswordPanel"); UserIdPasswordPanel mainPanel = new UserIdPasswordPanel(frame); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class LoginPane { public static final String LOGIN = "Login"; public static final String RESET = "Reset"; public static final Object[] options = { LOGIN, RESET }; private JTextField userIdField = new JTextField(10); private JPasswordField passwordField = new JPasswordField(10); private Component parent; private String title; private JPanel message = new JPanel(); public LoginPane(Component parent, String title) { this.parent = parent; this.title = title; message.setLayout(new GridBagLayout()); GridBagConstraints gbc = getGbc(0, 0); message.add(new JLabel("User ID:"), gbc); gbc = getGbc(1, 0); message.add(userIdField, gbc); gbc = getGbc(0, 1); message.add(new JLabel("Password:"), gbc); gbc = getGbc(1, 1); message.add(passwordField, gbc); } private GridBagConstraints getGbc(int x, int y) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(5, 5, 5, 5); gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; return gbc; } public String getUserId() { return userIdField.getText(); } public char[] getPassword() { return passwordField.getPassword(); } public int show() { Object initialValue = "Login"; return JOptionPane.showOptionDialog(parent, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, initialValue); } }

Myself, I'd use a JOptionPane not a JFrame to create this dialog:

import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import javax.swing.*; @SuppressWarnings("serial") public class UserIdPasswordPanel extends JPanel { JTextField selectionField = new JTextField(10); JTextField userIDField = new JTextField(10); JTextField passwordField = new JTextField(10); public UserIdPasswordPanel(JFrame frame) { add(new JButton(new AbstractAction("Login") { @Override public void actionPerformed(ActionEvent arg0) { LoginPane loginPane = new LoginPane(UserIdPasswordPanel.this, "My Login Panel"); int result = loginPane.show(); System.out.println("" + result); if (result >= 0) { String selectionText = LoginPane.options[result].toString(); if (selectionText.equals(LoginPane.LOGIN)) { System.out.println("do some login action here"); } else if (selectionText.equals(LoginPane.RESET)) { System.out.println("do some reset action here"); } // just to show that this works: selectionField.setText(selectionText); userIDField.setText(loginPane.getUserId()); // !! never do this !!!!! passwordField.setText(new String(loginPane.getPassword())); } } })); selectionField.setEditable(false); userIDField.setEditable(false); passwordField.setEditable(false); add(new JLabel("Selection: ")); add(selectionField); add(new JLabel("ID: ")); add(userIDField); add(new JLabel("Password: ")); add(passwordField); } private static void createAndShowGui() { JFrame frame = new JFrame("UserIdPasswordPanel"); UserIdPasswordPanel mainPanel = new UserIdPasswordPanel(frame); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class LoginPane { public static final String LOGIN = "Login"; public static final String RESET = "Reset"; public static final Object[] options = { LOGIN, RESET }; private JTextField userIdField = new JTextField(10); private JPasswordField passwordField = new JPasswordField(10); private Component parent; private String title; private JPanel message = new JPanel(); public LoginPane(Component parent, String title) { this.parent = parent; this.title = title; message.setLayout(new GridBagLayout()); GridBagConstraints gbc = getGbc(0, 0); message.add(new JLabel("User ID:"), gbc); gbc = getGbc(1, 0); message.add(userIdField, gbc); gbc = getGbc(0, 1); message.add(new JLabel("Password:"), gbc); gbc = getGbc(1, 1); message.add(passwordField, gbc); } private GridBagConstraints getGbc(int x, int y) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(5, 5, 5, 5); gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; return gbc; } public String getUserId() { return userIdField.getText(); } public char[] getPassword() { return passwordField.getPassword(); } public int show() { Object initialValue = "Login"; return JOptionPane.showOptionDialog(parent, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, initialValue); } }

更多推荐

本文发布于:2023-07-14 20:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事故   Java   setLocation   mishap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!