java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码租碰,然后自己再根据需要修改,如:
package mainFrame
import java.awt.EventQueue
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.ImageIcon
import javax.swing.JButton
import javax.swing.JCheckBox
import javax.swing.JFrame。
Java是一门面向对象御型消编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复镇知杂的编程。
package imageimport java.awt.BorderLayout
import java.awt.Color
import java.awt.Dimension
import java.awt.GridLayout
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.SwingUtilities
public class PanelRunner extends JFrame
{
private static final long serialVersionUID = 1L
private static void initPanels ( JFrame pr )
{
for ( int i = 0 i < 5 i++ )
{
JPanel panel = new JPanel ()
switch (i)
{
case 0:
panel.setBackground (Color.RED)
panel.setPreferredSize (new Dimension (200, 100))
pr.add (panel, BorderLayout.NORTH)
break
case 1:
panel.setBackground (Color.YELLOW)
panel.setPreferredSize (new Dimension (200, 300))
pr.add (panel, BorderLayout.EAST)
break
case 2:
panel.setBackground (Color.ORANGE)
panel.setPreferredSize (new Dimension (200, 100))
pr.add (panel, BorderLayout.SOUTH)
break
case 3:
panel.setBackground (Color.WHITE)
panel.setPreferredSize (new Dimension (200, 300))
pr.add (panel, BorderLayout.WEST)
break
case 4:
pr.add (panel, BorderLayout.CENTER)
panel.setPreferredSize (new Dimension (200, 100))
panel.setLayout (new GridLayout (1, 2))
for ( 源改猜int j = 0 j < 2 j++ )
{
JPanel subPanel = new JPanel ()
subPanel.setPreferredSize (new Dimension (200, 100))
Color color = j == 0 ? Color.BLUE : Color.GREEN
subPanel.setBackground (color)
panel.add (subPanel)
}
break
default:
break
}
}
}
private static void initFrame ( JFrame pr )
{
pr.setLayout (new BorderLayout ())
pr.setSize (600, 300)
pr.setLocationRelativeTo (null)
pr.setResizable (false)
pr.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void 雹型run ()
{
PanelRunner pr = 歼绝new PanelRunner ()
initFrame (pr)
initPanels (pr)
pr.setVisible (true)
}
})
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)