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图形界面(一)基本组件实例
最新推荐文章于 2022-04-27 11:12:38 发布