如何构建 JFrame child class?
How do I construct a JFrame child class?
我正在尝试从 JFrame
制作 child class。我认为我这样做是正确的,但是当我 运行 这个它打开一个空白 window 没有名称或背景颜色(我的 JPanel
class 做背景。但是,我知道错误不存在,因为我注释掉了 add(Jpanel
) 并且 window 仍然没有名称)而且 eclipse 没有显示任何语法错误。为什么此代码不起作用?:
主要Class:
package ashwin.engine;
import javax.swing.*;
import java.awt.*;
public class Execute {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
int[] bcolor = new int[3];
bcolor[0] = 254;
bcolor[1] = 0;
bcolor[2] = 0;
Window wndw = new Window("Test", 1000, 1000, bcolor, true);
} });
}
}
JFrame Child Class:
package ashwin.engine;
import javax.swing.*;
import java.awt.*;
public class Window extends JFrame {
Window(String name, int width, int length, int[] backgroundColor, boolean visible) {
System.out.println("made it to frame class");
setName(name);
setVisible(visible);
setSize(width, length);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Display display = new Display(backgroundColor);
}
}
编辑:
忘了说,它确实打印出我的调试语句 "made it to frame class",不知道这是否有帮助,但我想我应该指出来。
使 setVisible 行成为最后一行。
您不应使用 setName
,而应使用 setTitle
。这将有效地在屏幕上显示名称。
对于背景,你应该使用 getContentPane().setBackgroundColor(Color color)
chode 应该是这样的:
public class Execute {
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final Color bcolor = new Color(254, 0, 0);
final Window wndw = new Window("Test", 1000, 1000, bcolor, true);
}
});
}
}
public class Window extends JFrame {
Window(final String name, final int width, final int length, final Color backgroundColor,
final boolean visible) {
System.out.println("made it to frame class");
this.setTitle(name);
this.setSize(width, length);
this.getContentPane().setBackground(backgroundColor);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(visible);
}
}
我正在尝试从 JFrame
制作 child class。我认为我这样做是正确的,但是当我 运行 这个它打开一个空白 window 没有名称或背景颜色(我的 JPanel
class 做背景。但是,我知道错误不存在,因为我注释掉了 add(Jpanel
) 并且 window 仍然没有名称)而且 eclipse 没有显示任何语法错误。为什么此代码不起作用?:
主要Class:
package ashwin.engine;
import javax.swing.*;
import java.awt.*;
public class Execute {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
int[] bcolor = new int[3];
bcolor[0] = 254;
bcolor[1] = 0;
bcolor[2] = 0;
Window wndw = new Window("Test", 1000, 1000, bcolor, true);
} });
}
}
JFrame Child Class:
package ashwin.engine;
import javax.swing.*;
import java.awt.*;
public class Window extends JFrame {
Window(String name, int width, int length, int[] backgroundColor, boolean visible) {
System.out.println("made it to frame class");
setName(name);
setVisible(visible);
setSize(width, length);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Display display = new Display(backgroundColor);
}
}
编辑: 忘了说,它确实打印出我的调试语句 "made it to frame class",不知道这是否有帮助,但我想我应该指出来。
使 setVisible 行成为最后一行。
您不应使用 setName
,而应使用 setTitle
。这将有效地在屏幕上显示名称。
对于背景,你应该使用 getContentPane().setBackgroundColor(Color color)
chode 应该是这样的:
public class Execute {
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final Color bcolor = new Color(254, 0, 0);
final Window wndw = new Window("Test", 1000, 1000, bcolor, true);
}
});
}
}
public class Window extends JFrame {
Window(final String name, final int width, final int length, final Color backgroundColor,
final boolean visible) {
System.out.println("made it to frame class");
this.setTitle(name);
this.setSize(width, length);
this.getContentPane().setBackground(backgroundColor);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(visible);
}
}