package GUI_experience;
import java.awt.*;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.net.*;
/**class myFrame extends Frame{
myFrame(String s){
super(s);
this.setBackground(Color.blue);
this.setSize(300,200);
this.setVisible(true);
}
public static void main(String []args){
myFrame mfr=new myFrame("First GUI project");
}
}*/ /**class MyFrame extends Frame{
static int id=0;
MyFrame(int x,int y,int w,int h,Color myColor){
super("MyFrame first project "+(++id));
this.setBackground(myColor);
this.setBounds(x,y,w,h);
this.setLayout(null);
this.setVisible(true);
}
public static void main(String[]args){
MyFrame mf=new MyFrame(200,300,200,200,Color.gray);
}
}*/ /**
* 窗体示例JFrame
* 通常继承自JFrame类来使用而不是在类的main函数中创建一个JFrame对象
*
*/
class Demo2 extends JFrame{
Demo2(){ setVisible(true);//Visible setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭按键,直接关闭
//setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//关闭按键等于没用
//setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//关闭但是不停止程序
//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//释放程序资源
setBounds(300,500,300,200);//Location and Size
//setSize(300,200);
//setLocation(300,500);
setTitle("This is a Frame."); Container c = getContentPane();//获取窗体容器
c.setBackground(Color.white);
JLabel jl=new JLabel("Right here is a frame.");
c.add(jl);
// c.remove(jl);
c.validate();//验证容器中的组件,等于刷新一下
// f.setContentPane(c);//重新载入容器,与上式等效,但是上式更好
setResizable(false);
System.out.println(getX());
System.out.println(getY());
} public static void main(String[] args) {
new Demo2(); }
} /**
* JDialog对话框示例
*
*/
class Demo3 extends JDialog{
Demo3(JFrame frame){//在对话框的参数列表中就要包含父窗体
super(frame,"Title of DIALOG",true);//父窗体对象,对话框标题,是否阻塞父窗体
Container c=getContentPane();
JLabel jl=new JLabel("这是一个标签");
jl.setForeground(Color.RED);//设置字体颜色为红色
//jl.setText("");更改标签内容
//jl.getText();获取标签内容
jl.setFont(new Font("宋体",Font.BOLD,15));//设置字体
c.add(jl);//添加一段标签 JLabel pic=new JLabel("这是一个展示图片的标签");
// URL url=Demo3.class.getResource("pycharm_inter.png");//获取图片URL路径,要求图片和此文件在同一包内
// Icon icon = new ImageIcon(url);
Icon icon = new ImageIcon("src/GUI_experience/pycharm_inter.png");//直接写出图片相应路径也能得到图片
// jl.setSize(20,20);//设置标签大小并不能改变图片大小
pic.setIcon(icon);//添加图片
c.add(pic);
setBounds(100,100,500,400); } public static void main(String[] args) {
JFrame f =new JFrame("Father Frame");
f.setBounds(50,50,300,300);
Container c=f.getContentPane();
JButton btn =new JButton("Push out a DIALOG");
c.setLayout(new FlowLayout());
c.add(btn);
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
/**
* 重要部分:给按钮添加监听(使按钮生效)
*
*/
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
Demo3 d =new Demo3(f);
d.setVisible(true);//对话框最好在main中设置窗体可见
}
});
} } class Exp_absoluteLayout extends JFrame{
Exp_absoluteLayout(){
setBounds(300,300,200,150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane(); c.setLayout(null);//布局参数设置为null即为绝对布局,此时组件大小和位置只由设定的坐标决定(与窗体大小无关)
JButton jb1 = new JButton("Button_1"), jb2 = new JButton("Button_2");
jb1.setBounds(10,30,120 ,30);
jb2.setBounds(60,70,100,20);
c.add(jb1);
c.add(jb2); setVisible(true); }
public static void main(String[] args) {
new Exp_absoluteLayout();
}
} class Exp_FlowLayout extends JFrame{
Exp_FlowLayout(){
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(300,300,200,150); Container c = getContentPane();
for(int i=0;i<10;i++){
c.add(new JButton("Button_"+i));
}
// setLayout(new FlowLayout());//默认居中对齐
setLayout(new FlowLayout(FlowLayout.RIGHT,20,20));//设置为右对齐,水平间距20,竖直间距20
} public static void main(String[] args) {
new Exp_FlowLayout();
}
} class Exp_BorderLayout extends JFrame{
Exp_BorderLayout(){
setBounds(100,100,350,200);
setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane();
c.setLayout(new BorderLayout());
JButton jb1=new JButton("中"),
jb2=new JButton("东"),
jb3=new JButton("西"),
jb4=new JButton("南"),
jb5=new JButton("北");
c.add(jb1,BorderLayout.CENTER);
c.add(jb2,BorderLayout.EAST);
c.add(jb3,BorderLayout.WEST);
c.add(jb4,BorderLayout.SOUTH);
c.add(jb5,BorderLayout.NORTH);
c.setVisible(true);
setVisible(true);//tip:只有将设置可见性放在最后才能保证所有组件都可见
} public static void main(String[] args) {
new Exp_BorderLayout();
}
} class Exp_GridLayout extends JFrame{
Exp_GridLayout(){
setBounds(100,100,300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane();
// c.setLayout(new GridLayout(7,3,5,5));//网格布局,7行3列,水平间距5,竖直间距5
c.setLayout(new GridLayout(6,4));//紧密无缝隙排列
for(int i=0;i<23;i++){//如果组件数量超过网格布局,每当不足时则多出一列
c.add(new JButton("Button_"+i));
}
setVisible(true);
} public static void main(String[] args) {
new Exp_GridLayout();
}
} class Exp_GridBagFLayout {
JFrame f = new JFrame();
Container c= f.getContentPane(); void createFrame(){
f.setSize(800,600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(new GridBagLayout());
f.setLocationRelativeTo(null);
}
void init(){
GridBagConstraints g1 = new GridBagConstraints();
c.add(new JButton("Btn1"),g1); GridBagConstraints g2 = new GridBagConstraints();
g2.gridx=1;
g2.gridy=1;
c.add(new JButton("Btn2"),g2); GridBagConstraints g3 = new GridBagConstraints();
g3.gridx=2;
g3.gridy=2;
c.add(new JButton("Btn3"),g3);
}
void createButton(){
for(int i=0;i<9;i++){
GridBagConstraints g1 = new GridBagConstraints();
g1.gridx=0;
g1.gridy=i;//标定位置(左上角)
c.add(new JButton("Btn"),g1); GridBagConstraints g2 = new GridBagConstraints();
g2.gridx=i;
g2.gridy=0;
c.add(new JButton("Btn"),g2);
}
} void init2(){
GridBagConstraints g1 = new GridBagConstraints();
g1.gridx=1;
g1.gridy=1;
c.add(new JButton("Btn1"),g1); GridBagConstraints g2 = new GridBagConstraints();
g2.gridx=2;
g2.gridy=2;
g2.gridwidth=2;
g2.gridheight=1;//占据位置:两行一列
c.add(new JButton("Btn2"),g2); GridBagConstraints g3 = new GridBagConstraints();
g3.gridx=4;
g3.gridy=4;
g3.gridwidth=2;
g3.gridwidth=2;
c.add(new JButton("Btn3"),g3);
}
void init3(){
GridBagConstraints g1 = new GridBagConstraints();
g1.gridx=1;
g1.gridy=1;
g1.gridwidth=2;
g1.gridheight=2;
g1.fill=GridBagConstraints.NONE;
c.add(new JButton("NONE"),g1); GridBagConstraints g2 = new GridBagConstraints();
g2.gridx=3;
g2.gridy=1;
g2.gridheight=2;
g2.gridwidth=2;
g2.fill=GridBagConstraints.HORIZONTAL;//填充方式:水平填充
c.add(new JButton("HORIZONTAL"),g2); GridBagConstraints g3 = new GridBagConstraints();
g3.gridx=5;
g3.gridy=1;
g3.gridwidth=2;
g3.gridheight=2;
g3.fill=GridBagConstraints.VERTICAL;//填充方式:竖直填充
c.add(new JButton("VERTICAL"),g3); GridBagConstraints g4 = new GridBagConstraints();
g4.gridx=7;
g4.gridy=1;
g4.gridwidth=2;
g4.gridheight=2;
g4.fill=GridBagConstraints.BOTH;//填充方式:完全填充
c.add(new JButton("BOTH"),g4);
}
void init4(){
GridBagConstraints g1 = new GridBagConstraints();
g1.gridx=1;
g1.gridy=1;
g1.gridwidth=2;
g1.gridheight=2;
g1.fill=GridBagConstraints.NONE;
g1.anchor=GridBagConstraints.NORTHWEST;//占据方位:所占据区域的左上角(西北角)
c.add(new JButton("."),g1);
}
void init5(){
GridBagConstraints g1 = new GridBagConstraints();
g1.gridx=1;
g1.gridy=1;
g1.gridwidth=2;
g1.gridheight=2;
g1.insets = new Insets(0,0,10,10);//偏移像素值
c.add(new JButton("."),g1);
}
void init6(){
GridBagConstraints g1 = new GridBagConstraints();
g1.gridx=1;
g1.gridy=1;
g1.gridwidth=2;
g1.gridheight=2;
g1.ipadx=10;
g1.ipady=10;//首选位置
c.add(new JButton("NONE"),g1);
}
void init7(){
GridBagConstraints g1 = new GridBagConstraints();
g1.gridx=1;
g1.gridy=1;
g1.gridwidth=2;
g1.gridheight=2;
g1.weightx=10;
g1.weighty=10;//当窗体大小足够大时占据的空间(如果不够大则迁就窗体进行回缩
c.add(new JButton("NONE"),g1);
}
public static void main(String[] args) {
Exp_GridBagFLayout eg = new Exp_GridBagFLayout();
eg.createFrame();
eg.createButton();
eg.init7();
eg.f.setVisible(true);
}
} class Demo4 extends JFrame{
Demo4(){
setBounds(100,100,190,120);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null); JComboBox<String> cb = new JComboBox<String>();//建立下拉列表,可以使用泛型确定条目类型
cb.addItem("身份证");
cb.addItem("学生证");
cb.addItem("工作证"); String [] items = {"1","2","3"};//利用字符串数组创建下拉列表
JComboBox<String> cb2 = new JComboBox<>(items); JComboBox<String> cb3 = new JComboBox<>();
String [] item = {"学生证","驾驶证","毕业证"};
ComboBoxModel cm = new DefaultComboBoxModel<>(item);//建立下拉列表模型并向其中加入元素
cb3.setModel(cm);//利用下拉列表模型创建下拉列表 cb3.setEditable(true);//设定下拉框中值可编辑
cb3.setBounds(10,10,80,21);
c.add(cb3); JButton jb = new JButton("打印");
jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) {
System.out.println("索引:"+cb3.getSelectedIndex());
System.out.println("值:"+cb3.getSelectedItem());
}
});
jb.setBounds(100,10,60,20);
c.add(jb); setVisible(true);
} public static void main(String[] args) {
Demo4 d4 = new Demo4();
} } class Exp_Jlist extends JFrame{
Exp_Jlist(){
setBounds(100,100,217,167);
setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane();
c.setLayout(null); String items [] ={"1","2","3","4","5","6","7","8"};
// JList<String> jl = new JList<>(items); DefaultListModel<String> mod = new DefaultListModel<>();//创建数据模型
for(String tmp:items){
mod.addElement(tmp);//向数据模型中添加元素
}
mod.addElement("AnotherElem");
JList<String> jl = new JList<>();
jl.setModel(mod);//为创建的列表框提供模型,方便更改元素 // jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//选择模式:只能选择1个元素
// jl.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);//只能选择相邻元素
jl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);//选择不受限制(按ctrl) JButton jb = new JButton("确认");
jb.setBounds(120,90,70,25);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
java.util.List<String> str = jl.getSelectedValuesList();//获取列表框中元素
for(String st:str){
System.out.println(st);
}
}
});
c.add(jb); JScrollPane js = new JScrollPane(jl);//添加滚动条
js.setBounds(10,10,100,100);//如果添加了滚动条,列表框的大小由滚动条决定
c.add(js);
setVisible(true);
} public static void main(String[] args) {
new Exp_Jlist();
}
} class Exp_JTextField extends JFrame{
Exp_JTextField(){
setBounds(100,100,250,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c =getContentPane();
c.setLayout(new FlowLayout()); JTextField jt = new JTextField("aaa",20);//创建文本框,同时可以创建默认文本和长度
jt.setColumns(20);//也可以通过此方法设置长度
jt.setText("aaa");//也可以通过此方法设置文本
jt.setFont(new Font("宋体",Font.PLAIN,20));//设置字体
c.add(jt); JButton jb = new JButton("确认");
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(jt.getText());//获取文本框中的文本
jt.setText("");//点击完按钮之后清空文本
jt.requestFocus();//获取光标
}
});
c.add(jb);
setVisible(true);
} public static void main(String[] args) {
new Exp_JTextField();
}
} class Exp_JPasswordField extends JFrame{
Exp_JPasswordField(){
setBounds(100,100,500,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout()); JPasswordField jp = new JPasswordField(20);
jp.setColumns(20);
jp.setFont(new Font("Arial",Font.BOLD,20));
jp.setEchoChar('*');//设置显式字符,默认为圆点
jp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
char ch[] = jp.getPassword();//获取密码的字符数组
String str = new String (ch);
System.out.println(str);
}
});
c.add(jp); setVisible(true);
} public static void main(String[] args) {
new Exp_JPasswordField();
}
} class Exp_JTextArea extends JFrame{
Exp_JTextArea(){
setBounds(100,100,400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JTextArea area = new JTextArea("这是一个文本域");//创建一个带默认文本的文本域,不同于文本框,文本域支持多行输入
area.setText("文本域");
area.setRows(5);//设定行
area.setColumns(20);//设定列数
area.append("添加内容");
area.insert("插入",3);
area.setFont(new Font("楷体",Font.PLAIN,20)); JScrollPane js = new JScrollPane(area);//给文本域提供滚动条
c.add(js); setVisible(true);
} public static void main(String[] args) {
new Exp_JTextArea();
}
} class JustAExampleForActionListener extends JFrame{//动作事件监听器示例
JustAExampleForActionListener(){
setBounds(100,100,500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c =getContentPane(); JTextField jt = new JTextField();
JLabel jl = new JLabel("这是一个标签"); JScrollPane js =new JScrollPane(jt);
js.setBounds(10,10,300,30); jt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Object obj =actionEvent.getSource();//使用ActionEvent对象获取值
JTextField jtTmp = (JTextField)obj;
System.out.println(jtTmp.getText());
jl.setText("文本框单击了回车");
}
}); c.add(js);
c.add(jl);
setVisible(true);
} public static void main(String[] args) {
new JustAExampleForActionListener();
}
} class JustAnExampleForFocusListener extends JFrame{//焦点事件监听器示例
JustAnExampleForFocusListener(){
setBounds(100,100,500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null); JTextField jt1 = new JTextField();
jt1.setBounds(10,10,60,20); JTextField jt2 = new JTextField();
jt2.setBounds(10,60,60,20); JTextField jt3 = new JTextField();
jt3.setBounds(10,110,60,20); jt1.addFocusListener(new MyFocusListener() );
jt2.addFocusListener(new MyFocusListener() );
jt3.addFocusListener(new MyFocusListener() );
c.add(jt1);
c.add(jt2);
c.add(jt3);
setVisible(true);
}
class MyFocusListener implements FocusListener{//自定义的焦点事件监听类
public void focusGained(FocusEvent e){//得到焦点事件
JTextField jtTmp = (JTextField)e.getSource();//获取触发事件的组件
jtTmp.setBorder(BorderFactory.createLineBorder(Color.GREEN));//给得到焦点的文本框提供绿色边框
}
public void focusLost(FocusEvent e){//失去焦点事件
JTextField jtTmp = (JTextField)e.getSource();
jtTmp.setBorder(BorderFactory.createLineBorder(Color.red));
}
} public static void main(String[] args) {
new JustAnExampleForFocusListener();
}
}
05-11 22:50