`
yongyezhandui
  • 浏览: 21431 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Java Swing 组件大全1

阅读更多


申明这是借鉴来的

自动水平和垂直方向都会出现滚动条
JTextArea ta=new JTextArea("http://hi.baidu.com/jenny��/");
JScrollPane sp=new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS ,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
窗口界面出现在屏幕中间位置
static final int WIDTH=300;
static final int HEIGHT=150;
JFrame loginframe=new JFrame("计算机爱好者");
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setDefaultCloseOperation这是一个使得窗口上面的关闭控件有效的类库方法
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int x=(screenSize.width-WIDTH)/2;
int y=(screenSize.height-HEIGHT)/2;
loginframe.setLocation(x,y);
按照GridBagConstraints网格组布局方式排列组件的方法
//x指控件位于第几列。
//y指控件位于第几行。
//w指控件需要占几列。
//h指控件需要占几行。
public void add(Component c,GridBagConstraints constraints,int x,int y,int w,int h)
{
        constraints.gridx=x;
        constraints.gridy=y;
        constraints.gridwidth=w;
        constraints.gridheight=h;
        add(c,constraints);
}
分割内容面板JSplitPane
JButton b1= new JButton ("确定");//创建两个普通按钮组件
JButton b2 = new JButton ("取消");
JSplitPane splitPane = new JSplitPane ();//创建一个分隔容器类
splitPane.setOneTouchExpandable (true); //让分割线显示出箭头
splitPane.setContinuousLayout (true); //当用户操作分割线箭头时,系统会重绘图形
splitPane.setPreferredSize (new Dimension (100,200));
splitPane.setOrientation (JSplitPane.HORIZONTAL_SPLIT); //设置分割线为水平分割线
splitPane.setLeftComponent (b1); //将b1放到分割线左边,将b2放到分割线右边
splitPane.setRightComponent (b2);
splitPane.setDividerSize (3); //设置分割线大小为3个单位
splitPane.setDividerLocation(50); //设置分割线的位置位于中间
选项卡容器JTabbedPane
JFrame frame = new JFrame ("计算机爱好者");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);//默认为false
JTabbedPane tp=new JTabbedPane();//创建一个选项卡容器,将之添加到顶层容器内
frame.setContentPane(tp);
JPanel panel1 = new JPanel ();
tp.addTab("panel1", panel1); //添加选项卡容器,并且设置其中每个选项卡的标签以及其是否可启用
tp.setEnabledAt(0,true);
tp.setTitleAt(0,"Tab1");
JInternalFrame窗口、虚拟桌面容器JDesktopPane
JFrame jf=new JFrame("计算机爱好者");
        jf.setSize(WIDTH,HEIGHT);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        JPanel contentPane=new JPanel();//创建一个中间容器,并且将之添加到顶层容器内,将之设置为流布局。
        jf.setContentPane(contentPane);
        contentPane.setLayout(new FlowLayout());
        JDesktopPane dp=new JDesktopPane();//创建一个虚拟桌面容器,将dp添加到以上创建的中间容器中
        dp.setLayout(new FlowLayout());
        contentPane.add(dp);
        JInternalFrame jif=new JInternalFrame("第一个窗口",true,true,true); //创建两个JIntenaFrame容器,并且创建三个标签组件,分别将它们添加到两个JInternaFrame容器内
        JInternalFrame jif1=new JInternalFrame("第二个窗口",true,true,true);
        JLabel l1=new JLabel("这是我第一个窗口");
        JLabel l4=new JLabel("这是我第二个窗口");
        JLabel l5=new JLabel("这是你第二个窗口");
        jif.setLayout(new FlowLayout());
        jif1.setLayout(new FlowLayout());
        jif.add(l1);
        jif1.add(l4);
        jif1.add(l5);
        dp.add(jif);
        dp.add(jif1);
        jif.setVisible(true);
        jif1.setVisible(true);
JLayeredPane容器
是将两个普通按钮组件,放置到JLayeredPane容器中,
再为这两个按钮组件分成两个不同的层次,这样的话,
就当单击下面一个层的按钮组件,它就会被显示到上面一个层上来
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
static final int WIDTH=300;
    static final int HEIGHT=150;
    JLayeredPane lp=new JLayeredPane();
    JButton b1=new JButton("确定");
    JButton b2=new JButton("取消");
    public test()
{
///设置顶层容器的标题
      super("测试窗口");
      ///将新建的JLayeredPane放到顶层容器内
      super.setContentPane(lp);
      b1.addActionListener(this); // 按钮事件
      b2.addActionListener(this);
lp.add(b1, new Integer(200)); // 将组件添加到JLayeredPane中,指定所在的层
lp.add(b2, new Integer(300));
b1.setBounds(new Rectangle(100, 100, 100, 100)); // Button出现位置
b1.setVisible(true); // 显示
b2.setBounds(new Rectangle(50, 50, 100, 100));
b2.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(360, 260);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("确定"))
{ // 判断是哪个按钮的动作
   lp.setLayer(b1, 300); // 重新设置组件层数
   lp.setLayer(b2, 200);
}
else if (e.getActionCommand().equals("取消"))
{
   lp.setLayer(b1, 200);
   lp.setLayer(b2, 300);
}
}
public static void main(String args[]) {
new test();
}
}

用panel做GroupBox
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.setBorder(BorderFactory.createTitledBorder( "Edit ")); //设置边框
buttonPanel.add(new JButton( "button1 "));
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics