Java: Creating a new instance of the UI class
I have two classes: class A and class B. class A has a main method. Class B is the User Interface (JFrame). I want to have an instance of B in A, but when I write any of the following the program runs but no window shows:
public class A{
static B d= new B();
public static void main(String args[]){
d.setVisible(true);} }
public class A{
public static void main(String args[]){
开发者_开发百科 java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new B().setVisible(true);
}
});
}}
public class A{
public static void main(String args[]){
B d= new B()
d.setVisible(true);
}}
How can I show the window?
Unfortunately, you didn't show us the code for class B. Perhaps the following example can give you a hint:
public class B extends JFrame {
public B() {
super("Hello, JFrame!");
}
}
public class A {
public static void main(String[] args) {
B b = new B();
b.setSize(300, 400);
b.setVisible(true);
}
}
It's a very minimal example, but as you can see B
inherits from JFrame. In A
's main method, you first create a new instance of B
, then set its size and make it visible.
It turns out that the problem was in class A. I had an instruction that was waiting for input from a stream before the new B() but it wasn't getting the input.
精彩评论