为什么我的组件不可见?
Why are my components invisible?
这是我使用 swing 创建 window 的代码。
我可以看到 window 的定义大小,但 none 的组件存在于 window 中。
为什么组件不可见?
我有单独的方法用于创建、初始化和添加组件。这些方法是从构造函数中调用的。带有标题和定义大小的 window 在输出中可见。我错过了什么?
package swing_basics;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MySwingDemo extends JFrame {
JLabel lblName, lblPassword; //Declaration of variables
JTextField txtfName;
JPasswordField pwdfPassword;
JButton btnSubmit, btnCancel, btnReset;
public void createComponents(){ //method to initialise the components
lblName = new JLabel();
lblPassword = new JLabel();
txtfName = new JTextField();
pwdfPassword = new JPasswordField();
btnSubmit = new JButton();
btnCancel = new JButton();
btnReset = new JButton();
}
public void setComponents(){ //method to set the components
setVisible(true);
setSize(400, 400);
setTitle("My Swing Demo");
setLayout(new FlowLayout());
lblName.setText("Name");
lblPassword.setText("Password");
txtfName.setText("Name");// try
pwdfPassword.setText("Password");
btnSubmit.setText("Submit");
btnCancel.setText("Cancel");
btnReset.setText("Reset");
}
public void addComponents(JFrame frame){ //method to add the components
frame.add(lblName);
frame.add(txtfName);
frame.add(lblPassword);
frame.add(pwdfPassword);
frame.add(btnSubmit);
frame.add(btnCancel);
frame.add(btnReset);
}
public static void main(String[] args) {
new MySwingDemo();
}
public MySwingDemo() { //Constructor
createComponents();
setComponents();
addComponents(this);
}
}
这是操作顺序,您要在添加(和设置)组件之前将 frame
设置为可见。而是在 设置组件后将 setVisible(true);
移动到 。 和,确保在调用setComponents();
.
之前调用addComponents(this);
喜欢
public MySwingDemo() { // Constructor
createComponents();
addComponents(this);
setComponents();
}
和我还要添加一个默认的frame
关闭操作
public void setComponents() { // method to set the components
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("My Swing Demo");
setLayout(new FlowLayout());
lblName.setText("Name");
lblPassword.setText("Password");
txtfName.setText("Name");// try
pwdfPassword.setText("Password");
btnSubmit.setText("Submit");
btnCancel.setText("Cancel");
btnReset.setText("Reset");
setVisible(true);
}
这是我使用 swing 创建 window 的代码。
我可以看到 window 的定义大小,但 none 的组件存在于 window 中。
为什么组件不可见?
我有单独的方法用于创建、初始化和添加组件。这些方法是从构造函数中调用的。带有标题和定义大小的 window 在输出中可见。我错过了什么?
package swing_basics;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MySwingDemo extends JFrame {
JLabel lblName, lblPassword; //Declaration of variables
JTextField txtfName;
JPasswordField pwdfPassword;
JButton btnSubmit, btnCancel, btnReset;
public void createComponents(){ //method to initialise the components
lblName = new JLabel();
lblPassword = new JLabel();
txtfName = new JTextField();
pwdfPassword = new JPasswordField();
btnSubmit = new JButton();
btnCancel = new JButton();
btnReset = new JButton();
}
public void setComponents(){ //method to set the components
setVisible(true);
setSize(400, 400);
setTitle("My Swing Demo");
setLayout(new FlowLayout());
lblName.setText("Name");
lblPassword.setText("Password");
txtfName.setText("Name");// try
pwdfPassword.setText("Password");
btnSubmit.setText("Submit");
btnCancel.setText("Cancel");
btnReset.setText("Reset");
}
public void addComponents(JFrame frame){ //method to add the components
frame.add(lblName);
frame.add(txtfName);
frame.add(lblPassword);
frame.add(pwdfPassword);
frame.add(btnSubmit);
frame.add(btnCancel);
frame.add(btnReset);
}
public static void main(String[] args) {
new MySwingDemo();
}
public MySwingDemo() { //Constructor
createComponents();
setComponents();
addComponents(this);
}
}
这是操作顺序,您要在添加(和设置)组件之前将 frame
设置为可见。而是在 设置组件后将 setVisible(true);
移动到 。 和,确保在调用setComponents();
.
addComponents(this);
喜欢
public MySwingDemo() { // Constructor
createComponents();
addComponents(this);
setComponents();
}
和我还要添加一个默认的frame
关闭操作
public void setComponents() { // method to set the components
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("My Swing Demo");
setLayout(new FlowLayout());
lblName.setText("Name");
lblPassword.setText("Password");
txtfName.setText("Name");// try
pwdfPassword.setText("Password");
btnSubmit.setText("Submit");
btnCancel.setText("Cancel");
btnReset.setText("Reset");
setVisible(true);
}