作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正处于创建运算符(operator)工/客户系统的程序的开始阶段,现在我刚刚创建了登录 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();
}
}
最佳答案
我自己,我会使用 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);
}
}
关于Java setLocation() 事故,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388459/
我正处于创建运算符(operator)工/客户系统的程序的开始阶段,现在我刚刚创建了登录 GUI,但我在使用 setLocation(); 方法。我将它设置为 250、250,但它使我的 GUI 的高
前言 🍊缘由 在一个月黑风高的夜晚,正准备休息的我突然接到之前 外包老总的亲切问候 。一顿输出才知道 三年前 为了搭建流程化部署,将甲方的 测试代码 放到github上后
所以我跟着这个教程 http://codeigniter.com/user_guide/general/cli.html 我试过 php index.php tools message 但我在终
我的页面上有社交媒体分享和点赞按钮。 Facebook 按钮获得的点击数存在问题。如果我在 Facebook 上“分享”页面,下次我刷新页面时,它会被计入“喜欢”。 stLight.opt
我是一名优秀的程序员,十分优秀!