Chr*_*and -3 java swing jlabel jframe
我有2节课,
我的主类创建了一个框架,我希望另一个类向其添加内容.一些阅读arroudn告诉我,我应该使用组件来执行此操作,但是当我运行我的代码时,框架是空的.
public static void main(String[] args)
{
// create frame
JFrame frame = new JFrame();
final int FRAME_WIDTH = 800;
final int FRAME_HEIGHT = 600;
// set frame attributes
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("My Frame");
frame.setVisible(true);
Component1 Com = new Component1();
Component add = frame.add(Com);
}
Run Code Online (Sandbox Code Playgroud)
我的Component类创建了一个JLabel
public class Component1 extends JComponent {
public void paintComponent()
{
JLabel label = new JLabel("<html>Some Text</html>");
}
}
Run Code Online (Sandbox Code Playgroud)
我没有得到任何编译错误,但是我在JFrame中没有得到任何文本.
谁能解释我做错了什么?
克里斯
您需要添加的JLabel
.也可以更好地扩展JPanel
而不是JComponent
使用默认布局管理器,并且可以在不需要设置组件大小的情况下显示任何添加的组件.paintComponent
用于自定义绘画BTW.
public class Component1 extends JPanel {
Component1() {
JLabel label = new JLabel("<html>Some Text</html>");
add(label);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4426 次 |
最近记录: |