我一直遇到一些没有出现JPanel的问题(可能是因为所有东西都在摆动?)。我已经尝试重新订购它添加的位置,添加SwingUtilities.invokeLater(...);
但仍然无效。在Google上搜索,大多数人只是说使用invokeLater函数,这对我不起作用。我假设它不需要布局管理器,因为大小和位置是由.SetBounds()完成的?这是我目前的代码
public class MenuLogin extends JPanel{
private JTextField txtUsername;
private JPasswordField txtPassword;
public MenuLogin(){
setBounds(0, 0, 380, 205);
setBackground(Color.WHITE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JLabel lblLogin = new JLabel("Login");
lblLogin.setBounds(155, 5, 85, 42);
lblLogin.setFont(new Font("Trebuchet MS", Font.PLAIN, 36));
lblLogin.setHorizontalAlignment(SwingConstants.CENTER);
JPanel form = new JPanel(); // The panel that won't show
form.setBorder(null);
form.setBounds(10, 74, 390, 195);
add(form);
form.setLayout(null);
form.setVisible(true);
txtUsername = new JTextField();
txtUsername.setFont(new Font("Trebuchet MS", Font.PLAIN, 17));
txtUsername.setToolTipText("Username");
txtUsername.setBounds(10, 30, 370, 30);
txtUsername.setColumns(16);
form.add(txtUsername);
txtPassword = new JPasswordField();
txtPassword.setFont(new Font("Trebuchet MS", Font.PLAIN, 17));
txtPassword.setToolTipText("Password");
txtPassword.setColumns(16);
txtPassword.setBounds(10, 78, 370, 30);
form.add(txtPassword);
JButton btnProceed = new JButton("Proceed");
btnProceed.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
btnProceed.setBounds(150, 119, 94, 30);
form.add(btnProceed);
JButton btnPlayAsGuest = new JButton("Play As Guest");
btnPlayAsGuest.setFont(new Font("Trebuchet MS", Font.PLAIN, 9));
btnPlayAsGuest.setBounds(291, 161, 89, 23);
form.add(btnPlayAsGuest);
JButton btnSignUp = new JButton("Sign Up");
btnSignUp.setFont(new Font("Trebuchet MS", Font.PLAIN, 13));
btnSignUp.setBounds(155, 160, 83, 23);
form.add(btnSignUp);
add(lblLogin);
}
});
}
}
唯一的是,第一个JLabel被展示......
关于为什么它仍未显示的任何建议都会很好。感谢。
答案 0 :(得分:4)
这应该有效:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MenuLogin extends JPanel {
private JTextField txtUsername;
private JPasswordField txtPassword;
public MenuLogin() {
setLayout(new BorderLayout());
setBackground(Color.WHITE);
JPanel form = new JPanel(); // The panel that won't show
form.setVisible(true);
JLabel lblLogin = new JLabel("Login");
lblLogin.setFont(new Font("Trebuchet MS", Font.PLAIN, 36));
form.add(lblLogin);
txtUsername = new JTextField();
txtUsername.setFont(new Font("Trebuchet MS", Font.PLAIN, 17));
txtUsername.setToolTipText("Username");
txtUsername.setColumns(16);
form.add(txtUsername);
txtPassword = new JPasswordField();
txtPassword.setFont(new Font("Trebuchet MS", Font.PLAIN, 17));
txtPassword.setToolTipText("Password");
txtPassword.setColumns(16);
form.add(txtPassword);
JButton btnProceed = new JButton("Proceed");
btnProceed.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
form.add(btnProceed);
JButton btnPlayAsGuest = new JButton("Play As Guest");
btnPlayAsGuest.setFont(new Font("Trebuchet MS", Font.PLAIN, 9));
form.add(btnPlayAsGuest);
JButton btnSignUp = new JButton("Sign Up");
btnSignUp.setFont(new Font("Trebuchet MS", Font.PLAIN, 13));
form.add(btnSignUp);
add(form, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new MenuLogin());
frame.pack();
frame.setVisible(true);
}
});
}
}
我甚至没有尝试找出哪个setBounds
方法导致问题(因为我从不使用它们,你也不应该这样做)。我刚刚删除了setBounds
方法并实现了合适的布局管理器(FlowLayout
- 非常原始)。
当谈到一些高级布局时,我个人更喜欢MigLayout,因为它简单。 另外,请查看GridBagLayout。
问候。
答案 1 :(得分:0)
您需要在框架中添加面板
JFrame frame = new JFrame();
frame.add(form);