swing图形界面(一)基本组件实例
来源:互联网 发布:md5算法java 编辑:程序博客网 时间:2024/06/02 09:01
component基本组件:
(1)frame
package component;
import javax.swing.*;
public class Framework extends JFrame{
public Framework()
{
super("the framework");
setSize(300,200);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainPanel mp=new MainPanel();
add(mp);
setVisible(true);
}
private class MainPanel extends JPanel{
private MainPanel()
{
super();
}
}
public static void main(String[] args)
{
Framework f=new Framework();
}
}
(2)Button
package component;
import javax.swing.*;
public class IconButton extends JFrame {
public IconButton()
{
super("the IconButton");
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
ImageIcon i=new ImageIcon("D://my document//photoshops//Icon//Icon-1.jpg");
JButton load=new JButton("load",i);
JButton save=new JButton("save",i);
JButton delete=new JButton("delete",i);
JButton resert=new JButton("resert",i);
p.add(load);
p.add(save);
p.add(delete);
p.add(resert);
add(p);
pack();
setVisible(true);
}
public static void main(String[] args)
{
IconButton f=new IconButton();
}
}
(3)checkBox
package component;
import javax.swing.*;
public class MycheckBox extends JFrame {
public MycheckBox()
{
super("the framework");
setSize(600,400);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
ImageIcon i=new ImageIcon("D://my document//photoshops//Icon//Icon-1.jpg");
JCheckBox cb1=new JCheckBox("红色",true);
JCheckBox cb2=new JCheckBox("绿色");
JCheckBox cb3=new JCheckBox("黄色");
JCheckBox cb4=new JCheckBox("橙色",false);
JCheckBox cb5=new JCheckBox("棕色",true);
p.add(cb1);
p.add(cb2);
p.add(cb3);
p.add(cb4);
p.add(cb5);
add(p);
setVisible(true);
}
public static void main(String[] args)
{
MycheckBox f=new MycheckBox();
}
}
(4)comboBox
package component;
import javax.swing.*;
public class MycomboBox extends JFrame{
public MycomboBox()
{
super("the framework");
setSize(300,200);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
JComboBox cb=new JComboBox();
String[] str=new String[]{"first","second","third","forth","fifth"};
for(int i=0;i<str.length;i++)
{
cb.addItem(str[i]);
}
p.add(cb);
add(p);
setVisible(true);
}
public static void main(String[] args)
{
MycomboBox f=new MycomboBox();
}
}
(5)Lable
package component;
import javax.swing.*;
public class Mylabel extends JFrame{
public Mylabel()
{
super("the framework");
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
ImageIcon i=new ImageIcon("D://my document//photoshops//Icon//Icon-1.jpg");
JLabel l=new JLabel("URL:",i,SwingConstants.RIGHT);
JTextField tf=new JTextField(40);
p.add(l);
p.add(tf);
add(p);
pack();
setVisible(true);
}
public static void main(String[] args)
{
Mylabel f=new Mylabel();
}
}
(6)List
package component;
import javax.swing.*;
public class Mylist extends JFrame {
public Mylist()
{
super("the framework");
setSize(300,200);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
String[] str=new String[]{"first","second","third","forth","fifth","sixht","seventh"};
JList l=new JList(str);
l.setVisibleRowCount(4);
JScrollPane sp=new JScrollPane(l);
p.add(sp);
add(p);
setVisible(true);
}
public static void main(String[] args)
{
Mylist f=new Mylist();
}
}
(7)RadioButton
package component;
import javax.swing.*;
public class MyRadioButton extends JFrame{
public MyRadioButton()
{
super("the framework");
setSize(400,200);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
JRadioButton[] rb=new JRadioButton[]{new JRadioButton("first"),
new JRadioButton("second"),
new JRadioButton("third"),
new JRadioButton("forth"),
new JRadioButton("fifth")};
ButtonGroup bg=new ButtonGroup();
for(int i=0;i<5;i++)
{
bg.add(rb[i]);
p.add(rb[i]);
}
add(p);
setVisible(true);
}
public static void main(String[] args)
{
MyRadioButton f=new MyRadioButton();
}
}
(8)scrollPane
package component;
import javax.swing.*;
public class Myscoll extends JFrame{
public Myscoll()
{
super("the framework");
setSize(300,400);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
JLabel l=new JLabel("Content:");
JTextArea ta=new JTextArea(5,20);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
JScrollPane sp=new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
p.add(l);
p.add(sp);
add(p);
setVisible(true);
}
public static void main(String[] args)
{
Myscoll f=new Myscoll();
}
}
(9)slider
package component;
import javax.swing.*;
public class Myslider extends JFrame{
public Myslider()
{
super("the framework");
setSize(300,200);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
JSlider s=new JSlider(JSlider.HORIZONTAL,0,40,10);
s.setMajorTickSpacing(10);
s.setMinorTickSpacing(1);
s.setPaintTicks(true);
s.setPaintLabels(true);
p.add(s);
add(p);
setVisible(true);
}
public static void main(String[] args)
{
Myslider f=new Myslider();
}
}
(10)text
package component;
import javax.swing.*;
public class Mytext extends JFrame {
public Mytext()
{
super("the framework");
setSize(350,200);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
JLabel l1=new JLabel("UserName:",SwingConstants.LEFT);
JLabel l2=new JLabel("Password:",SwingConstants.LEFT);
JLabel l3=new JLabel("Content:",SwingConstants.LEFT);
JTextField tf=new JTextField("the first textField:",20);
JPasswordField pf=new JPasswordField(20);
pf.setEchoChar('#');
JTextArea ta=new JTextArea("the first textArea:",10,20);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
p.add(l1);
p.add(tf);
p.add(l2);
p.add(pf);
p.add(l3);
p.add(ta);
add(p);
setVisible(true);
}
public static void main(String[] args)
{
Mytext f=new Mytext();
}
}
(11)消息框
package component;
import javax.swing.*;
public class dialogBox extends JFrame{
public dialogBox()
{
super("the framework");
setSize(300,200);
setLocation(100,100);
int re2=JOptionPane.showConfirmDialog(null,"是否已婚:");
String re1=JOptionPane.showInputDialog(null,"输入你的姓名:");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] str=new String[]{"红色","绿色","蓝色"};
int re3=JOptionPane.showOptionDialog(null,"你最喜欢的颜色:","Option",0,JOptionPane.INFORMATION_MESSAGE,null,str,str[0]);
JOptionPane.showMessageDialog(null,"你还没有保存");
setVisible(true);
}
public static void main(String[] args)
{
dialogBox f=new dialogBox();
}
}
(12)菜单
package swing;
import java.awt.FlowLayout;
import java.awt.MenuBar;
import javax.swing.*;
import component.Framework;
public class Mymenu extends JFrame{
public Mymenu()
{
super("the framework");
setSize(300,200);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar mb=new JMenuBar();
JMenu m1=new JMenu("File");
JMenu m2=new JMenu("Edit");
JMenuItem mi1=new JMenuItem("open");
JMenuItem mi2=new JMenuItem("close");
JMenuItem mi3=new JMenuItem("save");
JMenuItem mi4=new JMenuItem("exit");
JMenuItem mi5=new JMenuItem("Undo typing");
JMenuItem mi6=new JMenuItem("Redo");
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
m1.addSeparator();
m1.add(mi3);
m1.addSeparator();
m1.add(mi4);
m2.add(mi5);
m2.addSeparator();
m2.add(mi6);
mb.add(m1);
mb.add(m2);
setJMenuBar(mb);
setVisible(true);
}
public static void main(String[] args)
{
Mymenu f=new Mymenu();
}
}
(13)进度条
package swing;
import javax.swing.*;
import component.Framework;
public class MyprocessBar extends JFrame{
public MyprocessBar()
{
super("the framework");
setSize(300,200);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
JProgressBar pb=new JProgressBar(0,100);
pb.setValue(30);
pb.setStringPainted(true);
p.add(pb);
add(p);
setVisible(true);
}
public static void main(String[] args)
{
MyprocessBar f=new MyprocessBar();
}
}
(14)tabbedPanel
package swing;
import javax.swing.*;
public class MytabbedPane extends JFrame{
public MytabbedPane()
{
super("the framework");
setSize(400,300);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JTabbedPane tp=new JTabbedPane(JTabbedPane.RIGHT);
tp.add("first",p1);
tp.add("second",p2);
tp.add("third",p3);
tp.add("forth",p4);
tp.add("fifth",p5);
p.add(tp);
add(p);
setVisible(true);
}
public static void main(String[] args)
{
MytabbedPane f=new MytabbedPane();
}
}
(15)toolbar
package swing;
import javax.swing.*;
import java.awt.*;
public class Mytoolbar extends JFrame{
public Mytoolbar()
{
super("the framework");
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon i=new ImageIcon("D://my document//photoshops//Icon//Icon-1.jpg");
JButton load=new JButton("load",i);
JButton save=new JButton("save",i);
JButton delete=new JButton("delete",i);
JButton resert=new JButton("resert",i);
JToolBar tb=new JToolBar();
tb.add(load);
tb.add(save);
tb.add(delete);
tb.add(resert);
JTextArea ta=new JTextArea(5,40);
ta.setLineWrap(true);
JScrollPane sp=new JScrollPane(ta);
add(tb,"North");
add(sp,"Center");
pack();
setLayout(new BorderLayout());
setVisible(true);
}
public static void main(String[] args)
{
Mytoolbar f=new Mytoolbar();
}
}
- swing图形界面(一)基本组件实例
- 核心Swing组件(一)
- Swing基本组件使用4(JTable)
- Swing中的基本组件
- Java Swing 基本组件
- 疯狂JAVA讲义---第十二章:Swing编程(一)基本组件
- Swing代码分析(移动组件一)
- 主要Swing组件小结(一)
- Java图形界面开发:高级Swing容器(一)
- Java笔记(五)——Swing图形界面编程之组件与容器
- Swing中的JFormattedTextField组件实例
- Swing 组件的应用实例
- swing图形界面(二)QQ框架界面(GridBagLayout设计实例)
- Swing基本组件使用3(JMenuBar,JMenu,JMenuItem)
- swing基本组件使用1.0.1(JScrollPane,JSplitPane,JTabbedPane)
- Java Swing 多种组件完美界面实例(代码+截图)
- Android 基本组件(一)
- Swing笔记(一)——JLabel和JButton组件
- C语言动态数组程序
- Boost 1.46.1,vc2005, 编译
- Java学习之InputStream中read()与read(byte[] b)
- Java学习之InputStream中read()与read(byte[] b)
- 第一次面试----华为实习生招聘
- swing图形界面(一)基本组件实例
- 什么是6800总线,8080总线?
- CPU、MCU、MPU、DSP的区别?
- Android Trick 4: Activity的android:launchMode (startard、singleTop、singleTask、singleInstance)
- Air与java通信
- 特别推荐:Web开发常用速查手册大全(100+)
- Eclipse中好用的Porperties文件编辑器
- 天心天字辈ERP全部PDK源代码到了我手上的后果 - 超越天心之WEB天云
- 解决AS3 Socket编程中最令人头疼的问题