【问题标题】:Java swing error (eclipse) [duplicate]Java swing错误(eclipse)[重复]
【发布时间】:2015-11-17 11:41:57
【问题描述】:

您好,我是 Java Swing 应用程序窗口的初学者 在这里,我在图形中遇到了这个问题。

  • 为什么会出现这个问题?
  • 如何解决?

Here's the screenshot

这是代码

public class MainWindow {

private JFrame frame;
private JTextField textField;
private JPasswordField passwordField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MainWindow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
        private String txtUsername;
        private String txtPassword;

        public void actionPerformed(ActionEvent arg0) {
            txtPassword = passwordField.getText();
            txtUsername = textField.getText();
        }
    });
    btnLogin.setBounds(183, 181, 89, 23);
    frame.getContentPane().add(btnLogin);

    textField = new JTextField();
    textField.setBounds(183, 110, 150, 23);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    JLabel lblNewLabel = new JLabel("Username");
    lblNewLabel.setBounds(106, 114, 94, 14);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Password");
    lblNewLabel_1.setBounds(106, 153, 67, 14);
    frame.getContentPane().add(lblNewLabel_1);

    passwordField = new JPasswordField();
    passwordField.setBounds(183, 147, 150, 23);
    frame.getContentPane().add(passwordField);

    JLabel lblNewLabel_2 = new JLabel("Bus Station");
    lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
    lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 25));
    lblNewLabel_2.setBounds(62, 11, 321, 51);
    frame.getContentPane().add(lblNewLabel_2);
}
}

【问题讨论】:

  • 真的吗?没有任何代码?我们应该如何帮助您?
  • 有什么问题?你能更精确一点吗?如有必要,粘贴堆栈跟踪或屏幕截图,以帮助他人。
  • 既然 OP 已经添加了请求的信息,那么删除一些反对票怎么样?

标签: java swing graphics window


【解决方案1】:

您不应直接将您的Components 添加到JFrame。 制作一个漂亮的new JPanel(),为其添加按钮和标签,然后仅将这个JPanel 添加到 contentPane。

编辑: 当然,您应该使用正确的LayoutManager 来定位您的Components

【讨论】:

    猜你喜欢
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多