您的位置:首页 > 编程语言 > Java开发

java框架swing基础组件详解

2013-03-20 18:34 821 查看
Jpannel 内容面板

--------------------------------------------------------------------------------------------

JButton 按钮

but.setEnabled(false);//禁用按钮

--------------------------------------------------------------------------------------------

JLabel 文本内容

--------------------------------------------------------------------------------------------

JTextField 文本框

txt.getText(); 用来获取文本框的内容

txt.setText(""); 设置文本框的内容

txt.setBorder(BorderFactory.createLineBorder(

new Color(255,0,0), 2)); //颜色和边框粗细px

txt.setBackground(Color.GREEN);//设置背景色

txt.setForeground(Color.RED); //设置前景色

txt.setFont(new Font("隶书",1,22)); //字体,粗斜体,字号

--------------------------------------------------------------------------------------------

JPasswordField 密码框

--------------------------------------------------------------------------------------------

JComboBox 下拉列表

boxcountry.getSelectedItem();//获取下拉列表选中内容

boxprovince.addItem(list.get(i));//下拉列表添加内容

boxprovince.removeAllItems();//下拉列表清空内容

--------------------------------------------------------------------------------------------

JRadioButton 单选按钮

radio.isSelected();//是否被选

--------------------------------------------------------------------------------------------

JCheckBox 复选框

box.isSelected();//是否被选

--------------------------------------------------------------------------------------------

JTextArea 文本域

area.setLineWrap(true); //设置文本域自动换行。

--------------------------------------------------------------------------------------------

JScrollPane 滚动面板

span.setViewportView(area); //注意:滚动面板添加元素

--------------------------------------------------------------------------------------------

DefaultTableModel 表格模板;

model=new DefaultTableModel(dao.selectAllInfo(),vc);//设置模板内容和模板表头

model.addRow(vv);//添加行

DefaultTableModel model=

(DefaultTableModel)table.getModel();

//获取表格的模板

--------------------------------------------------------------------------------------------

JTable 表格

table.setModel(model);//给表格添加模板

table.setForeground(Color.red);//前景色

table.setBackground(Color.orange);//背景色

--------------------------------------------------------------------------------------------

JTabbedPane 选项卡

tab.addTab("主题",new ImageIcon("key_search.gif"),pan1,"你好");

//添加选项卡,(标题,图标,显示的组件,工具提示)

--------------------------------------------------------------------------------------------

JMenuBar 菜单栏(里面包含菜单)

--------------------------------------------------------------------------------------------

JMenu 菜单(可包含下一级菜单)

menu1.setMnemonic('v'); //快捷键使用的时候要使用alt组合键

menu1.addSeparator();//加分割线

--------------------------------------------------------------------------------------------

JMenuItem 菜单项(最子级)

--------------------------------------------------------------------------------------------

JPopupMenu 弹出菜单

pan.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e) {

if(3==e.getButton()){//是否点击右键

pop.show(pan, e.getX(),e.getY());//显示菜单并设置坐标

}

}

});

--------------------------------------------------------------------------------------------

Timer 定时器

time =new Timer(1000,new ActionListener(){

public void actionPerformed(ActionEvent e) {

txt.setText(new Date().toLocaleString());

}

});//设置定时器的频率,和要运行的内容

time.start();//启动定时器

time.stop();//停止定时器

--------------------------------------------------------------------------------------------

JList //可选列表

DefaultListModel model =new DefaultListModel();//模版

model.addElement("zhaoxin");//模板添加元素

list.getSelectedIndex();//获取选中元素位置

list.getSelectedValue();//获取选中元素值

--------------------------------------------------------------------------------------------

JProgressBar 加载条

bar.setValue(bar.getValue()+5);//设置加载条值

--------------------------------------------------------------------------------------------

JTree 树

DefaultMutableTreeNode root =new DefaultMutableTreeNode("Root");

DefaultTreeModel model=new DefaultTreeModel(root);//建立模板并设置根节点

model.insertNodeInto(subroot, root, 0);//在root下面添加节点subroot

model.insertNodeInto(leaf2, root, 1);

subroot.add(leaf1);

--------------------------------------------------------------------------------------------

pan = (JPanel)this.getContentPane(); //获取窗体自身的内容面板

// pan.setBackground(Color.RED);

// pan.setBackground(new Color(0,255,0)); //rgb 设置背景颜色

//流式布局

pan.setLayout(new FlowLayout()); //设置面板的布局方式。

pan.add(but1);pan.add(but2);pan.add(but3); //把控件添加到面板上

this.setSize(400, 400); //设置窗体宽和高 (头)

this.setTitle("this is my first window"); //设置窗体的标题栏

Image image =new ImageIcon("img/a.jpg").getImage();

but.setBounds(40,110, 120, 30);//设置坐标和大小

//注意:java程序中目录一个杠要么用\\ 要么/

this.setIconImage(image); //设置窗体标题栏图标

this.setResizable(false); //不能改变大小

// this.setUndecorated(true); //设置窗体无边框。

this.setLocationRelativeTo(null);//让窗体相对于屏幕居中对齐

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //当窗体关闭或退出的时候,控制台也被关闭。

this.setVisible(true); //让窗体可视化 (尾)

--------------------------------------------------------------------------------------------

pan.setLayout(new BorderLayout());

//面板不设置布局,默认为边界布局

pan.add(BorderLayout.EAST,but1);

pan.add(BorderLayout.WEST,but2);

pan.add(BorderLayout.SOUTH,but3);

pan.add(BorderLayout.NORTH,but4);

pan.add(BorderLayout.CENTER,but5);

pan.setLayout(new GridLayout(3,3)); //网格布局

pan.setLayout(null); //空布局 需要坐标定位

--------------------------------------------------------------------------------------------

弹出式对话框:

//弹出式对话框两种:

//消息对话框,没有返回值

// JOptionPane.showMessageDialog(null,"你确定要走?"

// ,"想走?"

// ,JOptionPane.ERROR_MESSAGE,new ImageIcon("跑.gif"));

//有返回值的对话框。

int num =JOptionPane.showConfirmDialog(null,"你确定要走?",

"想走?",JOptionPane.OK_CANCEL_OPTION,

JOptionPane.INFORMATION_MESSAGE, null);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: