为什么我的背景颜色没有显示?

时间:2012-10-26 04:09:40

标签: swing scala user-interface background

不确定为什么背景颜色没有出现? 我在每个面板中设置了不透明度,我是否必须在MainFrame内容部分中执行其他操作?

// See http://www.artima.com/pins1ed/gui-programming.html
import scala.swing._
import java.awt.Color

object SecondSwingApp extends SimpleGUIApplication {
  def top = new MainFrame {
    title = "Second Swing App"

    val dealerPanel = new BoxPanel(Orientation.Horizontal) {
      background = new java.awt.Color(35, 125, 35)
      preferredSize = new java.awt.Dimension(800, 200)
      opaque = true
    }

    val botPlayerPanel = new BoxPanel(Orientation.Horizontal) {
      background = new java.awt.Color(35, 125, 125)
      preferredSize = new java.awt.Dimension(800, 200)
      opaque = true
    }

    val humanPlayerPanel = new BoxPanel(Orientation.Horizontal) {
      background = new java.awt.Color(125, 125, 35)
      preferredSize = new java.awt.Dimension(800, 200)
      opaque = true
    }

    contents = new BoxPanel(Orientation.Vertical) {
        contents += dealerPanel
        contents += botPlayerPanel
        contents += humanPlayerPanel
    }

  }
}

2 个答案:

答案 0 :(得分:0)

我认为这是因为在dealerPanel,botPlayerPanel和humanPlayerPanel中没有组件,因此不显示背景颜色。如果您向每个面板添加内容,则会显示颜色。例如,我添加了标签:

var label = new Label("dealer panel")
label.foreground = new java.awt.Color(255,255,255)
contents += label

enter image description here

尺寸和布局也存在问题,可能是问题的一部分。

import scala.swing._
import java.awt.Color

object SecondSwingApp extends SimpleGUIApplication {
  def top = new MainFrame {
    title = "Second Swing App"

    val dealerPanel = new BoxPanel(Orientation.Horizontal) {
      background = new Color(35, 125, 35)
      preferredSize = new java.awt.Dimension(800, 255)
      var label = new Label("dealer panel")
      label.foreground = new java.awt.Color(255,255,255)
      contents += label
      opaque = true
    }

    val botPlayerPanel = new BoxPanel(Orientation.Horizontal) {
      background = new java.awt.Color(35, 125, 125)
      preferredSize = new java.awt.Dimension(800, 255)
      var label = new Label("bot player panel")
      label.foreground = new java.awt.Color(255,255,255)
      contents += label
      opaque = true
    }

    val humanPlayerPanel = new BoxPanel(Orientation.Horizontal) {
      background = new java.awt.Color(125, 125, 35)
      preferredSize = new java.awt.Dimension(800, 255)
      var label = new Label("human player panel")
      label.foreground = new java.awt.Color(255,255,255)
      contents += label
      opaque = true
    }

    contents = new BoxPanel(Orientation.Vertical) {
        contents += dealerPanel
        contents += botPlayerPanel
        contents += humanPlayerPanel
    }

  }
}

答案 1 :(得分:0)

我也不是Swing专家,但如果他们没有任何内容(如另一个答案所示),它看起来好像没有空间。如果内容会强制小组超过preferredSize,则preferredSize可能仅相关。您可以将minimumSizemaximumSize设置为相同的值,从而迫使BoxPanel占用那么多空间,但它当然会使布局非常静态。

enter image description here

val dealerPanel = new BoxPanel(Orientation.Horizontal) {
  background = new java.awt.Color(35, 125, 35)
  // preferredSize = new java.awt.Dimension(800, 200)
  minimumSize = new Dimension(100, 50)
  maximumSize = minimumSize
  opaque = true
}

val botPlayerPanel = new BoxPanel(Orientation.Horizontal) {
  background = new java.awt.Color(35, 125, 125)
  preferredSize = new java.awt.Dimension(800, 200)
  minimumSize = new Dimension(50, 100)
  maximumSize = minimumSize
  opaque = true
}

val humanPlayerPanel = new BoxPanel(Orientation.Horizontal) {
  background = new java.awt.Color(125, 125, 35)
  // preferredSize = new java.awt.Dimension(800, 200)
  minimumSize = new Dimension(200, 20)
  maximumSize = minimumSize
  opaque = true
}

奇怪的是,你仍然需要指定一个preferredSize属性,因为窗口不会跨越任何东西,你看到的只是应用程序的标题栏。