码码;
package swing1;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class EmptyJFrame extends JFrame{
public static void main(String[] args) {
//现在创建了一个对象,不过什么都显示不出来
EmptyJFrame f = new EmptyJFrame();
//加上这一句就可以显示一个仅有关闭,最小化,最大化的按钮的Frame了
f.setVisible(true);
//再加上这一句就可以显示一个在左上角,拥有指定大小的Frame了
f.setSize(300,200);
//再加上这一句就可以把Frame放在最中间了
f.setLocationRelativeTo(null);
//如果没有这一句,在点击关闭Frame的时候程序其实还是在执行状态中的,加上这一句才算是真正的把资源释放掉了
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
package swing1;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class EmptyJFrame2 extends JFrame{
EmptyJFrame2(){
initGUI();
}
private void initGUI(){
setVisible(true);
setSize(300,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
EmptyJFrame2 f = new EmptyJFrame2();
}
});
}
}
码码;
package swing1;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class AddComponent extends JFrame{
public AddComponent(){
initGUI();
}
private void initGUI(){
setVisible(true);
setSize(300,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//创建元件
JButton jButton1 = new JButton("jButton1");
//添加元件
add(jButton1);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AddComponent f = new AddComponent();
}
});
}
}
package swing1;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class AddComponent2 extends JFrame{
//定义变量
private JButton jButton1;
public AddComponent2(){
initGUI();
addComp();
}
private void addComp(){
//初始化
jButton1 = new JButton("jButton1");
//添加元件
add(jButton1);
}
private void initGUI(){
setVisible(true);
setSize(300,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AddComponent2 f = new AddComponent2();
}
});
}
}
码码;
package swing1;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
/**
* swing基础实例
* @author HZ20232
*
*/
public class Hello{
public static void main(String args[])throws Exception{
NewFrame frame1 = new NewFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//一定要设置关闭
frame1.setVisible(true);
}
}
class NewFrame extends JFrame{
private JLabel label1;
private JButton button1;
private JTextField text1;
private JComboBox box;
private JMenuBar menuBar;
private JSlider slider;
private JSpinner spinner;
private JToolBar toolBar;
public NewFrame(){
super();
this.setSize(300,500);
this.getContentPane().setLayout(null);//设置布局控制器
// this.getContentPane().setLayout(new FlowLayout());//设置布局控制器
// this.getContentPane().setLayout(new GridLayout(1,2));//设置布局控制器,需要给出设定的行列数目
// this.getContentPane().setLayout(new BorderLayout());//设置布局控制器,以North,South,West,East,来控制控件布局
// this.getContentPane().setLayout(new GridBagLayout());//设置布局控制器
this.add(this.getTextField(),null);//添加文本框
this.add(this.getButton(),null);//添加按钮
this.add(this.getLabel(),null);//添加标签
this.add(this.getBox(),null);//添加下拉列表框
this.setJMenuBar(this.getMenu());//添加菜单
this.add(this.getSlider(),null);
this.add(this.getSpinner(),null);
this.add(this.getToolBar(),null);
this.setTitle("Hello World!");//设置窗口标题
}
private JToolBar getToolBar(){
if(toolBar==null){
toolBar = new JToolBar();
toolBar.setBounds(103,260,71,20);
toolBar.setFloatable(true);
}
return toolBar;
}
private JSpinner getSpinner(){
if(spinner==null){
spinner = new JSpinner();
spinner.setBounds(103,220, 80,20);
spinner.setValue(100);
}
return spinner;
}
private JSlider getSlider(){
if(slider==null){
slider = new JSlider();
slider.setBounds(103,200,100, 20);
slider.setMaximum(100);
slider.setMinimum(0);
slider.setOrientation(0);
slider.setValue(0);
}
return slider;
}
/**
* 菜单的级别JMenuBar->JMenu->JMenuItem
* 三级都是1:n的关系
* 最后添加菜单用的SetJMenuBar方法
* @return 建立好的菜单
*/
private JMenuBar getMenu(){
if(menuBar==null){
menuBar = new JMenuBar();
JMenu m1 = new JMenu();
m1.setText("文件");
JMenu m2 = new JMenu();
m2.setText("编辑");
JMenu m3 = new JMenu();
m3.setText("帮助");
JMenuItem item11 = new JMenuItem();
item11.setText("打开");
JMenuItem item12 = new JMenuItem();
item12.setText("保存");
JMenuItem item13 = new JMenuItem();
item13.setText("退出");
JMenuItem item21 = new JMenuItem();
item21.setText("复制");
JMenuItem item22 = new JMenuItem();
item22.setText("拷贝");
JMenuItem item23 = new JMenuItem();
item23.setText("剪切");
JMenuItem item31 = new JMenuItem();
item31.setText("欢迎");
JMenuItem item32 = new JMenuItem();
item32.setText("搜索");
JMenuItem item33 = new JMenuItem();
item33.setText("版本信息");
m1.add(item11);
m1.add(item12);
m1.add(item13);
m2.add(item21);
m2.add(item22);
m2.add(item23);
m3.add(item31);
m3.add(item32);
m3.add(item33);
menuBar.add(m1);
menuBar.add(m2);
menuBar.add(m3);
}
return menuBar;
}
/**
* 设置下拉列表框
* @return
*/
private JComboBox getBox(){
if(box==null){
box = new JComboBox();
box.setBounds(103,140,71,27);
box.addItem("1");
box.addItem("2");
box.addActionListener(new comboxListener());//为下拉列表框添加监听器类
}
return box;
}
private class comboxListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Object o = e.getSource();
System.out.println(o.toString());
}
}
/**
* 设置标签
* @return 设置好的标签
*/
private JLabel getLabel(){
if(label1==null){
label1 = new JLabel();
label1.setBounds(34,49,53,18);
label1.setText("Name");
label1.setToolTipText("JLabel");
}
return label1;
}
/**
* 设置按钮
* @return 设置好的按钮
*/
private JButton getButton(){
if(button1==null){
button1 = new JButton();
button1.setBounds(103,110,71,27);
button1.setText("OK");
button1.setToolTipText("OK");
button1.addActionListener(new HelloButton());//添加监听器类,其主要的响应都由监听器类的方法实现
}
return button1;
}
/**
* 监听器类实现ActionListener接口,主要实现actionPerformed方法
* @author HZ20232
*
*/
private class HelloButton implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("Hello world!");
}
}
/**
* 设定文本域
* @return
*/
private JTextField getTextField(){
if(text1==null){
text1 = new JTextField();
text1.setBounds(96,49,160,20);
}
return text1;
}
}
码码;
package swing1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoundedRangeModel;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.JTree;
import javax.swing.Timer;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class Test extends JFrame{
public Test(){
MenuTest menuTest=new MenuTest();
LeftPanel leftPanel=new LeftPanel();
RightPanel rightPanel=new RightPanel();
BottomPanel bottomPanel=new BottomPanel();
CenterPanel centerPanel=new CenterPanel();
Container c=this.getContentPane();
this.setJMenuBar(menuTest);
c.add(leftPanel,BorderLayout.WEST);
c.add(rightPanel,BorderLayout.EAST);
c.add(centerPanel,BorderLayout.CENTER);
c.add(bottomPanel,BorderLayout.SOUTH);
this.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
setSize(700,500);
setTitle("Swing 组件大全简体版");
setUndecorated(true);
setLocation(200,150);
show();
}
class MenuTest extends JMenuBar{
private JDialog aboutDialog;
public MenuTest(){
JMenu fileMenu=new JMenu("文件");
JMenuItem exitMenuItem=new JMenuItem("退出",KeyEvent.VK_E);
JMenuItem aboutMenuItem=new JMenuItem("关于..",KeyEvent.VK_A);
fileMenu.add(exitMenuItem);
fileMenu.add(aboutMenuItem);
this.add(fileMenu);
aboutDialog=new JDialog();
initAboutDialog();
exitMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
System.exit(0);
}
});
aboutMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
aboutDialog.show();
}
});
}
public JDialog get(){
return aboutDialog;
}
public void initAboutDialog(){
aboutDialog.setTitle("关于");
Container con=aboutDialog.getContentPane();
Icon icon=new ImageIcon("sdmile.gif");
JLabel aboutLabel=new JLabel(""+"Swing!"+"
",icon,JLabel.CENTER);
con.add(aboutLabel,BorderLayout.CENTER);
aboutDialog.setSize(450,225);
aboutDialog.setLocation(300,300);
aboutDialog.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
dispose();
}
});
}
}
class LeftPanel extends JPanel{
private int i=0;
public LeftPanel(){
DefaultMutableTreeNode root=new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child=new DefaultMutableTreeNode("Child");
DefaultMutableTreeNode select=new DefaultMutableTreeNode("select");
DefaultMutableTreeNode child1=new DefaultMutableTreeNode(""+i);
root.add(child);
root.add(select);
child.add(child1);
JTree tree=new JTree(root);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
tree.setRowHeight(20);
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent e){
JTree tree=(JTree)e.getSource();
DefaultMutableTreeNode selectNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
i++;
selectNode.add(new DefaultMutableTreeNode(""+i));
}
});
tree.setPreferredSize(new Dimension(100,300));
JScrollPane scrollPane=new JScrollPane(tree);
this.add(scrollPane);
}
}
class BottomPanel extends JPanel{
private JProgressBar pb;
public BottomPanel(){
pb=new JProgressBar();
pb.setPreferredSize(new Dimension(680,20));
Timer time=new Timer(1,new ActionListener(){
int counter=0;
public void actionPerformed(ActionEvent e){
counter++;
pb.setValue(counter);
Timer t=(Timer)e.getSource();
if(counter==pb.getMaximum()){
t.stop();
counter=0;
t.start();
}
}
});
time.start();
pb.setStringPainted(true);
pb.setMinimum(0);
pb.setMaximum(1000);
pb.setBackground(Color.white);
pb.setForeground(Color.red);
this.add(pb);
}
public void setProcessBar(BoundedRangeModel rangeModel){
pb.setModel(rangeModel);
}
}
class RightPanel extends JPanel{
public RightPanel(){
this.setLayout(new GridLayout(8,1));
JCheckBox checkBox=new JCheckBox("复选按钮");
JButton button=new JButton("打开文件");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser file=new JFileChooser();
int resule=file.showOpenDialog(new JPanel());
if(resule==file.APPROVE_OPTION){
String fileName=file.getSelectedFile().getName();
String dir=file.getSelectedFile().getName();
JOptionPane.showConfirmDialog(null,dir+"\\"+fileName,"选择的文件",JOptionPane.YES_OPTION);
}
}
});
JToggleButton toggleButton=new JToggleButton("双胎按钮");
ButtonGroup buttonGroup=new ButtonGroup();
JRadioButton radioButton1=new JRadioButton("单选按钮1",false);
JRadioButton radioButton2=new JRadioButton("单选按钮2",false);
JComboBox comboBox=new JComboBox();
comboBox.setToolTipText("点击下拉列表增加选项");
comboBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox comboBox=(JComboBox)e.getSource();
comboBox.addItem("程序员");
comboBox.addItem("分析员");
}
});
DefaultListModel litem=new DefaultListModel();
litem.addElement("香蕉");
litem.addElement("水果");
JList list=new JList(litem);
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
JList l=(JList)e.getSource();
Object s=l.getSelectedValue();
JOptionPane.showMessageDialog(null,s,"消息框",JOptionPane.YES_OPTION);
}
});
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
add(button);
add(toggleButton);
add(checkBox);
add(radioButton1);
add(radioButton2);
add(comboBox);
add(list);
this.setBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.LIGHT_GRAY,Color.blue));
}
}
class CenterPanel extends JPanel{
public CenterPanel(){
JTabbedPane tab=new JTabbedPane(JTabbedPane.TOP);
JTextField textField=new JTextField("文本域,点击打开<文件按钮>可选择文件");
textField.setActionCommand("textField");
JTextPane textPane=new JTextPane();
textPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));
textPane.setText("编辑器,试着点击文本区,试着拉动分隔条。");
textPane.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
JTextPane textPane=(JTextPane)e.getSource();
textPane.setText("编辑器点击命令成功");
}
});
JSplitPane splitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,textField,textPane);
JTable table=new JTable(10,10);
JPanel pane=new JPanel();
pane.add(table.getTableHeader(),BorderLayout.NORTH);
pane.add(table);
tab.addTab("文本演示",splitPane);
tab.addTab("表格演示", pane);
tab.setPreferredSize(new Dimension(500,600));
this.add(tab);
this.setEnabled(true);
}
}
public static void main(String args[]){
new Test();
}
}
图图;