farme.add(BorderLayout.NORTH,g1)
frame.add(BorderLayout.CENTER,g2)
frame.add(BorderLayout.SOUTH,g3)
若这样不能达到你的效果,还可以这样
JFrame frame = new JFrame(new FlowLayout())
frame.add(g1)
frame.add(g2)
frame.add(g3)
希望对你有帮助
多半是因为你是在frame显示之后才把组件加上去,当改变窗口大小的时候窗口要重绘,这时组件才显示出来
把你加组件的代码放到frame.setVisible(true)后面就行了
import java.awt.Colorimport javax.swing.JFrame
import javax.swing.JLabel
public class MyFrame extends JFrame {
public static void main(String[] args) {
new MyFrame()// 创建实例,初始化界面
}
public MyFrame() {// 初始化界面
JLabel jl = new JLabel("文字标签")//标签
jl.setOpaque(true)//设置背景不透明
jl.setBackground(Color.RED)//背景色
jl.setForeground(Color.BLUE)//文字色
this.add(jl)
this.setLocation(280, 320)
this.setSize(250, 120)
this.setTitle("窗口")
this.setDefaultCloseOperation(EXIT_ON_CLOSE)
this.setVisible(true)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)