2

不知道为什么我的背景颜色没有出现?我在每个面板中设置了不透明度,我是否必须在 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
    }

  }
}
4

2 回答 2

0

我认为这是因为dealerPanel、botPlayerPanel 和humanPlayerPanel 中没有组件,所以背景颜色不显示。如果您向每个面板添加一些东西,那么颜色就会显示出来。例如,我添加了标签:

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

在此处输入图像描述

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

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
    }

  }
}
于 2012-10-26T07:55:17.093 回答
0

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

在此处输入图像描述

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属性,因为否则窗口不会跨越任何东西,您所看到的只是应用程序的标题栏。

于 2012-10-26T08:08:19.417 回答