我遇到了一些 JPanel 未显示的问题(可能是因为所有内容都在 swing 中线程化的方式?)。我尝试重新排序添加位置,添加 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...
任何关于为什么它仍然没有显示的建议都会很好。谢谢。
最佳答案
这应该有效:
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 .
问候。
关于java - JPanel 没有显示,尝试执行 invokeLater 之类的操作,仍然没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466076/