0

我尝试用按钮创建网格表

package midterm



import java.awt.BorderLayout
import java.awt.Color
import java.awt.GridLayout
import javax.swing.JButton
import javax.swing.JPanel;
import scala.swing.Button
import scala.swing.MainFrame
import scala.swing.SimpleSwingApplication



object Main extends SimpleSwingApplication {
def top = new MainFrame {
title = "Hello, World!"
val panel = new JPanel()
var btn = Array.ofDim[JButton](8,8)
//contents =new JButton()
 for(i<-0 to 7){
    for(j<-0 to 7){
      btn(i)(j)= new JButton("vsdf")
      btn(i)(j).setBounds(i*80,j*80,80,80)
      btn(i)(j).setBackground(Color.BLUE)
      panel.add(btn(i)(j))
    }
 }
}
}

编译很好,但在输出中只有没有按钮的框架,仅此而已。请告诉我我的错误!

4

1 回答 1

4

您已将按钮添加到 JPanel,但尚未在框架上放置面板(因此框架以空内容结束)。相反,您必须编写如下内容:

object Main extends SimpleSwingApplication {
  def top = new MainFrame {
  //...
    contents = new JPanel()
  //...
  contents+= btn(i)(j)
  }
}

请参阅 Scala Swing 教程

于 2013-03-10T20:15:25.313 回答