码迷,mamicode.com
首页 > 其他好文 > 详细

GUI

时间:2015-08-26 21:59:43      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

package cn.donghua.gui;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.Border;

public class HelloSwing {

public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("hello...Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
Border brd = BorderFactory.createMatteBorder(1, 1, 2, 2, Color.black);

//JScrollBar滚动条
JScrollBar jsb = new JScrollBar();
frame.add(jsb);

JPanel jp= new JPanel();
jp.setBorder(brd);
frame.add(jp);

final JTextField jtf = new JTextField(10);
frame.add(jtf);
//button
JButton jb = new JButton("kkk");
jp.add(jb);

//menu
JMenu menu = new JMenu("文件");
JMenu menu1 = new JMenu("编辑");
JMenu menu2 = new JMenu("打开");
JMenuItem item1 = new JMenuItem("item1");
JMenuItem item2 = new JMenuItem("item2");
JMenuItem item3 = new JMenuItem("item3");

JMenuBar jmb = new JMenuBar();
menu.add(item1);
menu.add(item2);
menu.add(menu2);
menu2.add(item3);
jmb.add(menu);
jmb.add(menu1);
frame.setJMenuBar(jmb);

//label
JLabel label = new JLabel("label");
frame.add(label);

//JTextPane
JTextPane jtp = new JTextPane();
frame.add(jtp);

//JCheckBox
JCheckBox jcb1 = new JCheckBox("checkbox1");
JCheckBox jcb2 = new JCheckBox("checkbox2");
JCheckBox jcb3 = new JCheckBox("checkbox3");
frame.add(jcb1);
frame.add(jcb2);
frame.add(jcb3);

//JRadioButton必须放在ButtonGroup中
ButtonGroup bg = new ButtonGroup();
JRadioButton jrb = new JRadioButton("性别",false);
JRadioButton jrb2 = new JRadioButton("职业",false);
JRadioButton jrb3 = new JRadioButton("年龄",false);
bg.add(jrb);
bg.add(jrb2);
bg.add(jrb3);
Container com = frame.getContentPane();
com.add(jrb);
com.add(jrb2);
com.add(jrb3);

//组合框JComboBox会出现下拉菜单(选一个)
final JComboBox<String> jcb = new JComboBox<String>();
jcb.addItem("item1");
jcb.addItem("item2");
jcb.addItem("item3");
frame.add(jcb);
jcb.addActionListener(new ActionListener() {

@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
jtf.setText("index:"+jcb.getSelectedIndex()+"content:"+((JComboBox<String>)e.getSource()).getSelectedItem());

}
});

//JList列表框按住ctrl键可以多选
String[] flavors = { "A", "B", "C", "D", "E", "F" };
DefaultListModel<String> items = new DefaultListModel<String>() ;
for(int i=0;i<flavors.length;i++){
items.addElement(flavors[i]);
}

JList<String> jlist = new JList<String>(items);
frame.add(jlist);


}

}

GUI

标签:

原文地址:http://www.cnblogs.com/daxiong225/p/4761715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!