我的登录界面有问题。不知怎的,这是不正确的......
所有对象(JLabel,JButton,JTextfield等)未正确显示。 这应该是它的样子:
只有在拉动窗口边框调整屏幕大小后才会出现。
JFrame包含2个JPanels作为选项卡。这是代码:
Login.java
package de.immozukunft.windows;
import javax.swing.*;
import de.immozukunft.tabs.LoginTab;
import de.immozukunft.tabs.MySQLConnectionTab;
import java.awt.BorderLayout;
import java.util.Locale;
import java.util.ResourceBundle;
public class Login {
/**The Login class implements the LoginTab and the MySQLConnectionTab
* to verify the user access*/
//String management
static Locale locale = new Locale("de");
static ResourceBundle r = ResourceBundle.getBundle("Strings", locale);
JFrame window = new JFrame();
//Constructor
public Login() {
window.setTitle(r.getString("userlogin"));
frame();
}
//Frame method
private void frame() {
window.setSize(400,250);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
//Tab-panel
JTabbedPane tabbedPane = new JTabbedPane();
//Tabs
tabbedPane.addTab("Login", new LoginTab(window));
tabbedPane.addTab("MySQL Verbindung", new MySQLConnectionTab()); //TODO Strings einfügen
//Add tab-panel to frame
window.add(tabbedPane, BorderLayout.CENTER);
}
}
LoginTab.java
package de.immozukunft.tabs;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import de.immozukunft.overwrittenClasses.JTextFieldImmo;
import de.immozukunft.persons.UserDetails;
import de.immozukunft.programs.MySQL;
import de.immozukunft.windows.ButtonPanel;
public class LoginTab extends JPanel {
/** LoginTab does the userverfication by
* comparing the entered data with the MySQL-database
* There is no encryption implemented*/
private static final long serialVersionUID = 1L;
//Multilingual String-Management
static Locale locale = new Locale("de");
static ResourceBundle r = ResourceBundle.getBundle("Strings", locale);
// static Connection con = null;
static Statement stnt = null;
static ResultSet rs = null;
//Constructor
public LoginTab(final JFrame window) {
panelMethod(window);
}
// LOGIN ITEMS
JLabel usernameLbl = new JLabel(r.getString("username"));
JLabel passwordLbl = new JLabel(r.getString("password"));
JTextFieldImmo usernameFld = new JTextFieldImmo(15);
JPasswordField passwordFld = new JPasswordField(15);
JButton loginBtn = new JButton(r.getString("login"));
JButton registerUserBtn = new JButton(r.getString("newUser"));
public void panelMethod(final JFrame window) {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Insets
c.insets = new Insets(4, 5, 0, 0);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
this.add(usernameLbl, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
this.add(usernameFld, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
this.add(passwordLbl, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
this.add(passwordFld, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
this.add(loginBtn, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
this.add(registerUserBtn, c);
// Actions Listener
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
MySQL.connect();
String username = usernameFld.getText().trim();
String password = String.valueOf(passwordFld.getPassword()).trim();
String sql = "SELECT username,password from per_user where username = '"
+ username + "'and password = '" + password + "'";
stnt = MySQL.getCon().createStatement();
rs = stnt.executeQuery(sql);
int count = 0;
while (rs.next()) {
count = count + 1;
}
if (count == 1) {
//JOptionPane.showMessageDialog(null, "User Found, Access Granded!"); //TODO String
window.setVisible(false);
window.dispose();
new ButtonPanel();
} else if (count > 1) {
JOptionPane.showMessageDialog(null,
"Duplicate User, Access Denied!"); // TODO String einfügen
} else {
JOptionPane.showMessageDialog(null, r.getString("userNotFound"));
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, r.getString("unableToConnectMySQL"));
e1.printStackTrace();
}
}
});
registerUserBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new UserDetails();
}
});
passwordFld.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
loginBtn.doClick();
}
});
}
}
MySQLConnectionTab.java
package de.immozukunft.tabs;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.*;
import de.immozukunft.overwrittenClasses.JTextFieldImmo;
import de.immozukunft.programs.MySQL;
public class MySQLConnectionTab extends JPanel { //Subclass of Jframe, with ActionListener as interface
/** MySQLConnectionTab is a JPanel which can be implemented
* in other JTabbedPanes in order to change the MySQL connection settings
*/
private static final long serialVersionUID = 1L;
private JButton connectBtn = new JButton(r.getString("connect")); //Connect-Button
private JButton cancelBtn = new JButton(r.getString("cancel")); //Cancel-Button
private JTextFieldImmo hostFld = new JTextFieldImmo(MySQL.getHost(), 20); // Host text field
private JTextFieldImmo usernameFld = new JTextFieldImmo(MySQL.getUsername(), 20); // Username text field
private JTextFieldImmo databaseFld = new JTextFieldImmo(MySQL.getDatabase(), 20); // Database text field
private JTextFieldImmo portFld = new JTextFieldImmo(String.valueOf(MySQL.getPort()), 20); // Port text field
private JPasswordField passwordFld = new JPasswordField(MySQL.getPassword(), 20); // Password text field
private JLabel hostLbl = new JLabel(r.getString("host")); // Host text label
private JLabel usernameLbl = new JLabel(r.getString("username")); // Username text label
private JLabel databaseLbl = new JLabel(r.getString("database")); // Database text label
private JLabel portLbl = new JLabel(r.getString("port")); // Port text label
private JLabel passwordLbl = new JLabel(r.getString("password")); // Password text label
static Locale locale = new Locale("de");
static ResourceBundle r = ResourceBundle.getBundle("Strings", locale);
//Constructor
public MySQLConnectionTab() {
panelMethod();
}
public void panelMethod(){
//Create GridBagConstraints
GridBagConstraints c2 = new GridBagConstraints();
//SetLayout to GridBagLayout
this.setLayout(new GridBagLayout());
//Insets
c2.insets = new Insets(4, 5, 0, 0);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 0;
c2.gridy = 0;
this.add(hostLbl, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 1;
c2.gridy = 0;
this.add(hostFld, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 0;
c2.gridy = 1;
this.add(usernameLbl, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 1;
c2.gridy = 1;
this.add(usernameFld, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 0;
c2.gridy = 2;
this.add(databaseLbl, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 1;
c2.gridy = 2;
this.add(databaseFld, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 0;
c2.gridy = 3;
this.add(portLbl, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 1;
c2.gridy = 3;
this.add(portFld, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 0;
c2.gridy = 4;
this.add(passwordLbl, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 1;
c2.gridy = 4;
this.add(passwordFld, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 1;
c2.gridy = 5;
this.add(connectBtn, c2);
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 0;
c2.gridy = 5;
this.add(cancelBtn, c2);
connectBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//All the field data is being set for the MySQL-connection
MySQL.setHost(hostFld.getText());
MySQL.setUsername(usernameFld.getText());
MySQL.setDatabase(databaseFld.getText());
MySQL.setPort(Integer.parseInt(portFld.getText()));
MySQL.setPassword(String.valueOf(passwordFld.getPassword()));
JOptionPane.showMessageDialog(null, String.format("%s", MySQL.getStatus()));
}
}); // Add ActionListener for connect Button
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hostFld.setText(MySQL.getHost());
usernameFld.setText(MySQL.getUsername());
databaseFld.setText(MySQL.getDatabase());
portFld.setText(String.valueOf(MySQL.getPort()));
passwordFld.setText(MySQL.getPassword());
}
}); // Add ActionListener for cancel Button
}
}
我希望有人可以给我一个提示。关于我的代码的任何其他建议也赞赏...仍然是一个初学者;)
答案 0 :(得分:6)
这是因为您在添加组件之前正在调用setVisible
。在setVisible
上添加组件后,请致电JFrame
。
基本上:
//Frame method
private void frame() {
//Tab-panel
JTabbedPane tabbedPane = new JTabbedPane();
//Tabs
tabbedPane.addTab("Login", new LoginTab(window));
tabbedPane.addTab("MySQL Verbindung", new MySQLConnectionTab()); //TODO Strings einfügen
//Add tab-panel to frame
window.add(tabbedPane, BorderLayout.CENTER);
//window.setSize(400,250);
window.pack();
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
另外,请致电pack
而不是setSize
。 JFrame
的大小将基于JFrame
内组件的首选大小以及这些组件之间的差距。