java swing几种常用容器简单实现
一:分隔面板 JSplitPanel
代码:
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
import javax.swing.JPanel ;
import javax.swing.JSplitPane ;
import java.awt.Container ;
class Tester
{
public static void main(String args[])
{
JFrame frame = new JFrame("JSplitPane测试") ;
Container con = frame.getContentPane() ;
JPanel panel = new JPanel() ;
JSplitPane left = null ; //左右分隔符号
JSplitPane top = null ; //上下分隔符号
left = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JLabel("左标签"),new JLabel("右标签")) ;
top = new JSplitPane(JSplitPane.VERTICAL_SPLIT,left,new JLabel("下标签")) ;
top.setDividerSize(10) ;
top.setOneTouchExpandable(true) ; //设置展开条
con.add(top) ;
frame.setSize(500,500) ;
frame.setLocation(300,300) ;
frame.setVisible(true) ;
}
}
二:JTabbedPanel
代码
import java.io.File ;
import java.awt.* ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
import javax.swing.JTabbedPane ;
import javax.swing.JPanel ;
import javax.swing.ImageIcon ;
import javax.swing.JFrame ;
class Tester
{
public static void main(String args[])
{
JFrame frame = new JFrame("JTabbedPane测试样例") ;
Panel p1 = new Panel() ;
Panel p2 = new Panel() ;
Panel p3 = new Panel() ;
JButton button = new JButton("卡片") ;
JLabel label = new JLabel("标签") ;
JButton open = new JButton("打开文件") ;
p1.add(button) ;
p2.add(label) ;
p3.add(open) ;
String picPath1 = "d:"+File.separator+"ok.gif" ;
String picPath2 = "d:"+File.separator+"exit.gif" ;
String picPath3 = "d:"+File.separator+"play.gif" ;
JTabbedPane tab =null;
tab = new JTabbedPane(JTabbedPane.TOP) ;
tab.addTab("加载",new ImageIcon(picPath1),p1,"图像") ;
tab.addTab("关机",new ImageIcon(picPath2),p2,"文字") ;
tab.addTab("播放",new ImageIcon(picPath3),p3,"影片") ;
frame.add(tab) ;
frame.setSize(500,400) ;
frame.setLocation(300,300) ;
frame.setVisible(true) ;
}
}
效果图
四:JScrollPane 添加可拖动工具条
代码
import java.io.File ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JPanel ;
import javax.swing.JScrollPane ;
import javax.swing.ImageIcon ;
import javax.swing.Icon ;
import java.awt.Container ;
import javax.swing.JLabel ;
class Tester
{
public static void main(String args[])
{
JFrame frame = new JFrame("scrollbar测试样例") ;
String picPath = "d:"+File.separator+"ok.gif" ;
Icon icon = new ImageIcon(picPath) ;
JLabel label = new JLabel(icon) ;
JPanel panel = new JPanel() ;
Container con = frame.getContentPane() ;
JScrollPane sc = null ;
//panel.add(label) ;
sc = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS) ;
panel.add(label) ;
con.add(sc) ;
frame.setSize(600,500) ;
frame.setVisible(true) ;
}
};效果图

浙公网安备 33010602011771号