Java_AWT_Swing 学习过程


Java_AWT_Swing 学习过程

import javax.swing.*;
import java.awt.*;

public class Test {
    public static void main(String[] args) {
        JFrame f1=new JFrame("LHL's First Java");
        f1.setVisible(true);
        f1.setSize(800,600);
        //f1.setBackground(Color.blue);
        f1.setLocation(40,400);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setResizable(true);


    }
}
-------------------------------------
import javax.swing.*;
import java.awt.*;

public class Test {
    public static void main(String[] args) {
        JFrame f1=new JFrame("LHL's First Java");
        f1.setVisible(true);
        
        f1.setSize(800,600);
        Container con=f1.getContentPane();
        con.setBackground(new Color(154,204,255));
        f1.setLocation(40,400);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setResizable(true);


    }
}
----------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

class WindowMenu extends JFrame
{
    JMenuBar menubar;
    JMenu menu,subMenu;
    JMenuItem item1,item2;
    public WindowMenu()
    {

    }
    public WindowMenu(String s,int x,int y,int w,int h)
    {
        init(s);
        setLocation(x,y);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }
    void init(String s)
    {
        setTitle(s);
        menubar=new JMenuBar();
        menu=new JMenu("1");
        subMenu=new JMenu("2");
        item1=new JMenuItem("3");
        item2=new JMenuItem("4");
        item1.setAccelerator(KeyStroke.getKeyStroke('A'));
        item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
        menu.add(item1);
        menu.addSeparator();
        menu.add(item2);
        menu.add(subMenu);
        subMenu.add(new JMenuItem("5"));
        subMenu.add(new JMenuItem("6"));
        menubar.add(menu);
        setJMenuBar(menubar);



    }
}
public class Test {
    public static void main(String[] args) {
        WindowMenu w1=new WindowMenu("菜单",20,30,200,190);
        w1.setSize(800,600);

    }
}
------------------------------------------------
import javax.swing.*;

class window extends JFrame
{
    JMenuBar menber;
    JMenu m1;
    JMenuItem mi1,mi2;
    window(String s,int x,int y,int w,int h)
    {
        setLocation(x,y);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    {
        
    }
    void init(String s)
    {
        setTitle(s);
        menber=new JMenuBar();
        m1=new JMenu();
        mi1=new JMenuItem();
        mi2=new JMenuItem();
        m1.add()
        
    }
}
public class Test {
    public static void main(String[] args) {
       

    }
}
--------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

class WindowMenu extends JFrame
{
    JMenuBar menubar;
    JMenu head;
    JMenu m1,m2;
    JMenuItem t1,t2;
    public WindowMenu()
    {

    }
    public WindowMenu(String s,int x,int y,int w,int h)
    {
        init(s);
        setLocation(x,y);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        Container con= getContentPane();
        con.setBackground(new Color(43,43,43));

    }
    void init(String s)
    {
        setTitle(s);
        menubar =new JMenuBar();
        head=new JMenu("头");
        m1=new JMenu("第一项");
        m2=new JMenu("第二项");
        t1=new JMenuItem("第一项的第一项");
        t2=new JMenuItem("第一项的第二项");
        m1.add(t1);
        m1.add(t2);
        head.add(m1);
        head.add(m2);
        menubar.add(head);
        setJMenuBar(menubar);


    }
}
public class Test {
    public static void main(String[] args) {
        WindowMenu w1=new WindowMenu("菜单",20,30,200,190);
        w1.setSize(800,600);

    }
}
----------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

class WindowMenu extends JFrame
{
    JMenuBar menubar;
    JMenu head;
    JMenu m1,m2;
    JMenuItem t1,t2,t3;
    public WindowMenu()
    {

    }
    public WindowMenu(String s,int x,int y,int w,int h)
    {
        init(s);
        setLocation(x,y);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        Container con= getContentPane();
        con.setBackground(new Color(43,43,43));

    }
    void init(String s)
    {
        setTitle(s);
        menubar =new JMenuBar();
        head=new JMenu("头");
        m1=new JMenu("第一项");
        m2=new JMenu("第二项");
        t1=new JMenuItem("第一项的第一项");
        t2=new JMenuItem("第一项的第二项");
        t3=new JMenuItem("第三项",new ImageIcon("favicon.ico"));
        m1.add(t1);
        m1.add(t2);
        head.add(m1);
        head.add(m2);
        head.add(t3);
        menubar.add(head);
        setJMenuBar(menubar);


    }
}
public class Test {
    public static void main(String[] args) {
        WindowMenu w1=new WindowMenu("菜单",20,30,200,190);
        w1.setSize(800,600);

    }
}
-------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

class windows extends JFrame {
    JCheckBox c1, c2;
    JRadioButton rm, rf;
    ButtonGroup group;
    JComboBox<String> comBox;

    public windows() {
        init();
        setTitle("LHL's Java");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        setSize(300, 100);
        setLocation(40, 100);
    }

    void init() {
        setLayout(new FlowLayout());
        comBox = new JComboBox<String>();
        c1 = new JCheckBox("音乐");
        c2 = new JCheckBox("旅游");
        group = new ButtonGroup();
        rm = new JRadioButton("男");
        rf = new JRadioButton("女");
        group.add(rm);
        group.add(rf);
        add(c1);
        add(c2);
        add(rf);
        add(rm);
        comBox.addItem("音乐天地");
        comBox.addItem("武术天地");
        add(comBox);
    }

}

public class Test {
    public static void main(String[] args) {
        windows w1 = new windows();
    }
}
-------------------------------------------------
import javax.swing.*;
import java.awt.*;

class windows extends JFrame
{
    JComboBox<String> box;
    JCheckBox x1, x2;
    JRadioButton m, w;
    ButtonGroup group;
    windows()
    {
        init();
        setTitle("LHL's Java");
        setVisible(true);
        setSize(400,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(40,200);
    }
    void init()
    {
        setLayout(new FlowLayout());
        box=new JComboBox<String>();
        x1=new JCheckBox("赛博朋克2077");
        x2=new JCheckBox("刺客信条英灵殿");
        m=new JRadioButton("男");
        w=new JRadioButton("女");
        group=new ButtonGroup();
        group.add(m);
        group.add(w);
        add(x1);
        add(x2);
        add(m);
        add(w);
        box.addItem("$39.99");
        box.addItem("$49.99");
        add(box);

    }
}

public class Test
{
    public static void main(String[] args)
    {
        windows w1=new windows();
    }
}
-------------------------
import javax.swing.*;
import java.*;
import java.awt.*;

class PanelNullLayout extends JPanel
{
    JButton button;
    JTextField text;
    PanelNullLayout()
    {
        setLayout(null);
        button =new JButton("确定");
        text=new JTextField();
        add(text);
        add(button);
        text.setBounds(100,30,90,30);
        button.setBounds(190,30,66,30);
    }
}

class PanelGridLayout extends JPanel
{
    PanelGridLayout()
    {
        GridLayout grid = new GridLayout(12, 12);
        setLayout(grid);
        Label label[][] = new Label[12][12];
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                label[i][j]=new Label();
                if ((i + j) % 2 == 0)
                    label[i][j].setBackground(Color.black);
                else
                    label[i][j].setBackground(Color.white);
                add(label[i][j]);
            }
        }
    }
}

class ShowLayout extends JFrame
{
    PanelGridLayout pannelGrid;
    PanelNullLayout panelNull;
    JTabbedPane p;
    ShowLayout()
    {
        pannelGrid=new PanelGridLayout();
        panelNull=new PanelNullLayout();
        p=new JTabbedPane();
        p.add("网格布局的面板",pannelGrid);
        p.add("空布局的面板",panelNull);
        add(p,BorderLayout.CENTER);
        add(new JButton("窗体是BorderLayout布局"),BorderLayout.NORTH);
        add(new JButton("南"),BorderLayout.SOUTH);
        add(new JButton("西"),BorderLayout.WEST);
        add(new JButton("东"),BorderLayout.EAST);
        setBounds(10,10,570,390);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        validate();
    }

}

public class test
{
    public static void main(String[] args)
    {
        new ShowLayout();
    }
}
-------------------------------------------------
import javax.swing.*;
import java.awt.*;

class windowboxLayout extends JFrame
{
    Box boxH;
    Box boxVOne,boxVTwo;
    public windowboxLayout()
    {
        setLayout(new FlowLayout());
        init();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    void init()
    {
        boxH =Box.createHorizontalBox();
        boxVOne=Box.createVerticalBox();
        boxVTwo=Box.createVerticalBox();
        boxVOne.add(new JLabel("姓名:"));
        boxVOne.add(new JLabel("职业:"));
        boxVTwo.add(new JTextField(10));
        boxVTwo.add(new JTextField(10));
        boxH.add(boxVOne);
        boxH.add(Box.createHorizontalStrut(10));
        boxH.add(boxVTwo);
        add(boxH);
        setBounds(10,10,300,110);
    }
}

public class test
{
    public static void main(String[] args)
    {
        windowboxLayout w1=new windowboxLayout();
    }
}
----------------------------------
import javax.swing.*;
import java.awt.*;
class windowboxLayout extends  JFrame
{
    Box wai;
    Box nei1,nei2;
    windowboxLayout()
    {
        setLayout(new FlowLayout());
        init();
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(10,10,300,90);
    }
    void init()
    {
        wai=Box.createHorizontalBox();
        nei1=Box.createVerticalBox();
        nei2=Box.createVerticalBox();
        nei1.add(new JLabel("姓名:"));
        nei1.add(new JLabel("性别:"));
        nei2.add(new JTextField(10));
        nei2.add(new JTextField(10));
        wai.add(nei1);
        wai.add(nei2);
        add(wai);
    }
}
public class test
{
    public static void main(String[] args)
    {
        windowboxLayout w1 = new windowboxLayout();
    }
}
----------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class WindowActionEvent extends JFrame
{
    JTextField text;
    ActionListener listener;
    WindowActionEvent()
    {
        setLayout(new FlowLayout());
        text=new JTextField(10);
        add(text);
        listener=new ReaderListen();
        text.addActionListener(listener);
        setVisible(true);
        setBounds(10,10,200,400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
class ReaderListen implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        String str=e.getActionCommand();
        System.out.println(str+":"+str.length());
    }
}
public class test
{
    public static void main(String[] args)
    {
        WindowActionEvent w1=new WindowActionEvent();
    }
}
--------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class WindowActionEvent extends JFrame
{
    JTextField text;
    ActionListener listener;
    WindowActionEvent()
    {
        setLayout(new FlowLayout());
        Container co=this.getContentPane();
        co.setBackground(new Color(27,40,56));
        text=new JTextField(10);
        listener=new ReaderListen();
        text.addActionListener(listener);
        add(text);
        setBounds(10,10,200,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class ReaderListen implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        String s=e.getActionCommand();
        System.out.println(s+"的字节数是:"+s.length());
    }
}
public class test
{
    public static void main(String[] args)
    {
        WindowActionEvent w1=new WindowActionEvent();
    }
}
--------------------------------------------
import com.sun.javaws.util.JfxHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

interface MycommandListener extends ActionListener
{
    public void setJTextField(JTextField text);

    public void setJTextArea(JTextArea area);
}

class WindowActionEvent extends JFrame
{
    JTextField inputText;
    JTextArea textShow;
    JButton button;
    MycommandListener listener;

    public WindowActionEvent()
    {
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 360, 230);
        setTitle("LHL's Java");
    }

    void init()
    {
        setLayout(new FlowLayout());
        inputText = new JTextField(10);
        button = new JButton("确定");
        textShow = new JTextArea(9, 30);
        add(inputText);
        add(button);
        add(new JScrollPane(textShow));
    }

    void setMyCommandListener(MycommandListener listener)
    {
        this.listener = listener;
        listener.setJTextField(inputText);
        listener.setJTextArea(textShow);
        inputText.addActionListener(listener);
        button.addActionListener(listener);
    }
}

class PoliceListen implements MycommandListener
{
    JTextField textInput;
    JTextArea textShow;

    @Override
    public void setJTextField(JTextField text)
    {
        textInput = text;
    }

    @Override
    public void setJTextArea(JTextArea area)
    {
        textShow = area;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String str = textInput.getText();
        textShow.append(str + "的长度:" + str.length() + '\n');
    }
}

public class test
{
    public static void main(String[] args)
    {
        WindowActionEvent win = new WindowActionEvent();
        PoliceListen police = new PoliceListen();
        win.setMyCommandListener(police);

    }
}
-0------------------------
错误:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
interface Mylisten extends ActionListener
{
    public void setText(JTextField text);
    public void setShow(JTextArea show);
}
class window extends JFrame
{
    JButton button;
    JTextArea show;
    JTextField text;
    Mylisten listen ;
    window()
    {
        init();
        setVisible(true);
        setBounds(10,10,360,230);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void init()
    {
        setLayout(new FlowLayout());
        JButton button=new JButton("计算");
        JTextField text=new JTextField(10);
        JTextArea show=new JTextArea(9,30);
        add(text);
        add(button);
        add(new JScrollPane(show));
    }
    void setListen(Mylisten listener)
    {
        listen=listener;
        listener.setShow(show);
        listener.setText(text);
        text.addActionListener(listener);
        button.addActionListener(listener);
    }
}
class police implements Mylisten
{
    JTextArea area;
    JTextField Text;

    @Override
    public void setText(JTextField text)
    {
        Text=text;
    }

    @Override
    public void setShow(JTextArea show)
    {
        area=show;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String s=Text.getText();
        area.append(s+"长度为:"+s.length());
    }
}
public class test
{
    public static void main(String[] args)
    {
        window w1=new window();
        police p1=new police();
        w1.setListen(p1);

    }
}
----------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

interface listenList extends ActionListener
{
    public void setText(JTextField text);
    public void setShow(JTextArea show);
}
class window extends JFrame
{
    JButton button;
    JTextField text;
    JTextArea show;
    listenList listen;
    window()
    {
        init();
        setVisible(true);
        setBounds(10,10,400,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void init()
    {
        setLayout(new FlowLayout());
        button=new JButton("计算");
        text=new JTextField(10);
        show=new JTextArea(9,30);
        add(text);
        add(button);
        add(new JScrollPane(show));
    }
    void setList(listenList listen)
    {
        this.listen=listen;
        listen.setShow(show);
        listen.setText(text);
        text.addActionListener(listen);
        button.addActionListener(listen);
    }

}
class police implements listenList
{
    JTextArea Show;
    JTextField Text;
    @Override
    public void setText(JTextField text)
    {
        Text=text;
    }

    @Override
    public void setShow(JTextArea show)
    {
        Show=show;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String str=Text.getText();
        Show.append(str+"的长度为:"+str.length()+'\n');
    }
}
public class test
{
    public static void main(String[] args)
    {
        window w1=new window();
        police p1=new police();
        w1.setList(p1);

    }
}
-------------------------------------------------------------
计算器
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
class ComputerListerner implements ActionListener
{
    JTextField inputNumberOne,inputNuberTwo;
    JTextArea textShow;
    String fuhao;
    public void setTextFieldOne(JTextField t)
    {
        inputNumberOne=t;
    }
    public void setJTextFieldTwo(JTextField t)
    {
        inputNuberTwo=t;
    }
    public void setJTextArea(JTextArea area)
    {
        textShow=area;
    }
    public  void setFuhao(String s)
    {
        fuhao=s;
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {
        try
        {
            double number1=Double.parseDouble(inputNumberOne.getText());
            double number2=Double.parseDouble(inputNuberTwo.getText());
            double result=0;
            if(fuhao.equals("+"))
            {
                result =number1+number2;
            }
            else if(fuhao.equals("-"))
            {
                result=number1-number2;
            }
            else if(fuhao.equals("*"))
            {
                result=number1*number2;
            }
            else if(fuhao.equals("/"))
            {
                result=number1/number2;
            }
            textShow.append(number1+" "+fuhao+' '+number2+" = "+result+"\n");
        }
        catch (Exception exp)
        {
            textShow.append("\n请输入数字字符\n");
        }
    }

}

class OperatorListener implements ItemListener
{
    JComboBox choice;
    ComputerListerner workTogether;
    public void setJCombBox(JComboBox box)
    {
        choice=box;
    }
    public void setWorkTogether(ComputerListerner computer)
    {
        workTogether=computer;
    }
    public void itemStateChanged(ItemEvent e)
    {
        String fuhao=choice.getSelectedItem().toString();
        workTogether.setFuhao(fuhao);
    }
}


class WindowOperation extends JFrame
{
    JTextField inputNumberOne,inputNuberTwo;
    JComboBox choiceFuhao;
    JTextArea textShow;
    JButton button;
    OperatorListener operator;
    ComputerListerner computer;
    public WindowOperation()
    {
        init();
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,390,360);
        setTitle("LHL's Java");
    }
    void init()
    {
        setLayout(new FlowLayout());
        inputNumberOne=new JTextField(5);
        inputNuberTwo=new JTextField(5);
        choiceFuhao=new JComboBox();
        button =new JButton("计算");
        choiceFuhao.addItem("选择运算符号:");

        String []a={"+","-","*","/"};
        for(int i=0;i<a.length;i++)
        {
            choiceFuhao.addItem(a[i]);
        }
        textShow=new JTextArea(9,30);
        operator=new OperatorListener();
        computer=new ComputerListerner();
        operator.setJCombBox(choiceFuhao);
        operator.setWorkTogether(computer);
        computer.setTextFieldOne(inputNumberOne);
        computer.setJTextFieldTwo(inputNuberTwo);
        computer.setJTextArea(textShow);
        choiceFuhao.addItemListener(operator);

        button.addActionListener(computer);
        add(inputNumberOne);
        add(choiceFuhao);
        add(inputNuberTwo);
        add(button);
        add(new JScrollPane(textShow));
    }

}
public class test
{
    public static void main(String []args)
    {
        WindowOperation w1=new WindowOperation();
    }
}
-----------------------------
import javax.swing.*;
import java.awt.*;

class create extends JFrame
{
    Box b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;
    JButton t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
    create() {

        setLayout(new FlowLayout());
        t1.setSize(10,14);
        t4.setSize(10,14);
        t5.setSize(10,14);
        t10.setSize(10,14);
        t2.setSize(20,7);
        t3.setSize(20,7);
        t6.setSize(10,7);
        t7.setSize(10,7);
        t8.setSize(10,7);
        t9.setSize(10,7);

        b1=Box.createVerticalBox();
        b1.add(t1);
        b1=Box.createVerticalBox();
        b2.add(t2);
        b2.add(t3);
        b3=Box.createVerticalBox();
        b3.add(t4);
        b4=Box.createVerticalBox();
        b4.add(t5);
        b5.add(t6);
        b5.add(t8);
        b6=Box.createVerticalBox();
        b6.add(t7);
        b6.add(t9);
        b7=Box.createVerticalBox();
        b7.add(t10);
        b8=Box.createHorizontalBox();
        b8.add(b1);
        b8.add(b2);
        b8.add(b3);
        b9=Box.createHorizontalBox();
        b9.add(b4);
        b9.add(b5);
        b9.add(b6);
        b9.add(b7);
        b10=Box.createVerticalBox();
        b10.add(b8);
        b10.add(b9);
        add(b10);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 300, 200);
    }
}
public class test
{
    public static void main(String []args)
    {
        create c1=new create();
    }
}
-------------------------------------
import javax.swing.*;
import java.awt.*;

class create extends JFrame
{
    Box b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;
    JButton t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
    create() {

        t1=new JButton("BUTTON");
        t2=new JButton("BUTTON");
        t3=new JButton("BUTTON");
        t4=new JButton("BUTTON");
        t5=new JButton("BUTTON");
        t6=new JButton("BUTTON");
        t7=new JButton("BUTTON");
        t8=new JButton("BUTTON");
        t9=new JButton("BUTTON");
        t10=new JButton("BUTTON");
        setLayout(new FlowLayout());
        b1=Box.createVerticalBox();
        b1.add(t1);
        b2=Box.createVerticalBox();
        b2.add(t2);
        b2.add(t3);
        b3=Box.createVerticalBox();
        b3.add(t4);
        b4=Box.createVerticalBox();
        b5=Box.createVerticalBox();
        b4.add(t5);
        b5.add(t6);
        b5.add(t8);
        b6=Box.createVerticalBox();
        b6.add(t7);
        b6.add(t9);
        b7=Box.createVerticalBox();
        b7.add(t10);
        b8=Box.createHorizontalBox();
        b8.add(b1);
        b8.add(b2);
        b8.add(b3);
        b9=Box.createHorizontalBox();
        b9.add(b4);
        b9.add(b5);
        b9.add(b6);
        b9.add(b7);
        b10=Box.createVerticalBox();
        b10.add(b8);
        b10.add(b9);
        add(b10);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 300, 200);
    }
}
public class test
{
    public static void main(String []args)
    {
        create c1=new create();
    }
}
-------------------------
import javax.swing.*;
import java.awt.*;

class create extends JFrame
{
    Box b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;
    JButton t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
    create() {
        Dimension preferredSize = new Dimension(10,14);
        t1=new JButton("BUTTON");
        t2=new JButton("BUTTON");
        t3=new JButton("BUTTON");
        t4=new JButton("BUTTON");
        t5=new JButton("BUTTON");
        t6=new JButton("BUTTON");
        t7=new JButton("BUTTON");
        t8=new JButton("BUTTON");
        t9=new JButton("BUTTON");
        t10=new JButton("BUTTON");
        setLayout(new FlowLayout());
        
        b1=Box.createVerticalBox();
        b1.add(t1);
        b2=Box.createVerticalBox();
        b2.add(t2);
        b2.add(t3);
        b3=Box.createVerticalBox();
        b3.add(t4);
        b4=Box.createVerticalBox();
        b5=Box.createVerticalBox();
        b4.add(t5);
        b5.add(t6);
        b5.add(t8);
        b6=Box.createVerticalBox();
        b6.add(t7);
        b6.add(t9);
        b7=Box.createVerticalBox();
        b7.add(t10);
        b8=Box.createHorizontalBox();
        b8.add(b1);
        b8.add(b2);
        b8.add(b3);
        b9=Box.createHorizontalBox();
        b9.add(b4);
        b9.add(b5);
        b9.add(b6);
        b9.add(b7);
        b10=Box.createVerticalBox();
        b10.add(b8);
        b10.add(b9);
        add(b10);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 300, 200);
    }
}
public class test
{
    public static void main(String []args)
    {
        create c1=new create();
    }
}
-----------------------------
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

/**
 * 1:常用面板
 * 面板也是一个Swing容器,他可以看作为容器容纳其他组件,但它也必须被添加到其他容器中
 * Swing中常用的面板包括JPanel面板和JScrollPane面板
 *
 * 2:JPanel面板可以聚集一些组件来布局
 * 读者首先应该明白面板也是一种容器,因为它继承了java.awt.Container类
 * @author biexiansheng
 *
 */
class JPanelTest extends JFrame{

    public JPanelTest(){
        Container container=getContentPane();//设置一个容器

        //将整个容器设置为2行1列的网格布局     网格布局管理器x,y代表行和列
        container.setLayout(new GridLayout(2,3,0,0));

        //初始化一个面板,设置1行3列的网格布局
        JPanel p1=new JPanel(new GridLayout(1,1,0,0));
        JPanel p2=new JPanel(new GridLayout(2,1,0,0));
        JPanel p3=new JPanel(new GridLayout(1,2,0,0));
        JPanel p4=new JPanel(new GridLayout(1,1,0,0));
        JPanel p5=new JPanel(new GridLayout(2,2,0,0));
        JPanel p6=new JPanel(new GridLayout(1,1,0,0));

        //在面板中添加按钮
        p1.add(new JButton("1"));
        p2.add(new JButton("2"));
        p2.add(new JButton("3"));
        p3.add(new JButton("4"));
        p4.add(new JButton("5"));
        p5.add(new JButton("6"));
        p5.add(new JButton("7"));
        p5.add(new JButton("8"));
        p5.add(new JButton("9"));
        p6.add(new JButton("10"));

        //最重要的一步,将面板实例添加到容器中
        container.add(p1);
        container.add(p2);
        container.add(p3);
        container.add(p4);
        container.add(p5);
        container.add(p6);

        //将容器外部特性实例化
        setTitle("JPanel面板的案例");
        setSize(400,250);//设窗体的大小     宽和高
        setVisible(true);//设定窗体的可视化
        //设置窗体的关闭方式
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JPanelTest jt=new JPanelTest();
    }

}
-0----------------------------------
import java.awt.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class JPanelTest extends JFrame{
    public JPanelTest() {
        Container c = getContentPane();
        c.setLayout(new GridLayout(2,3,0,0));

        JPanel p1 = new JPanel(new GridLayout(1,1,0,0));
        JPanel p2 = new JPanel(new GridLayout(2,1,0,0));
        JPanel p3 = new JPanel(new GridLayout(1,1,0,0));
        JPanel p4 = new JPanel(new GridLayout(1,1,0,0));
        JPanel p5 = new JPanel(new GridLayout(2,2,0,0));
        JPanel p6 = new JPanel(new GridLayout(1,1,0,0));
        p1.setPreferredSize(new Dimension(10,14));
        p2.setPreferredSize(new Dimension(20,14));
        p3.setPreferredSize(new Dimension(10,14));
        p4.setPreferredSize(new Dimension(10,14));
        p5.setPreferredSize(new Dimension(20,14));
        p6.setPreferredSize(new Dimension(10,14));
        p1.add(new JButton("BUTTON"));
        p2.add(new JButton("BUTTON"));
        p2.add(new JButton("BUTTON"));
        p3.add(new JButton("BUTTON"));
        p4.add(new JButton("BUTTON"));
        p5.add(new JButton("BUTTON"));
        p5.add(new JButton("BUTTON"));
        p5.add(new JButton("BUTTON"));
        p5.add(new JButton("BUTTON"));
        p6.add(new JButton("BUTTON"));

        
        c.add(p1);
        c.add(p2);
        c.add(p3);
        c.add(p4);
        c.add(p5);
        c.add(p6);
        setSize(600,300);
        setTitle("这是一个JPanel的窗体");
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String args[]) {
        new JPanelTest();
    }
}
-------------------
import javax.swing.*;
import java.awt.*;

public class test
{
    public static void main(String []args)
    {
        Frame f=new JFrame("LHL'S Java");
        ScrollPane s=new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
        s.add(new TextField("adfasdf"));
        s.add(new Button("asdfasdf"));
        f.add(s);

        f.setVisible(true);
        f.setBounds(10,10,500,300);
    }
}
------------------------------------
import javax.swing.*;
import java.awt.*;

public class test
{
    public static void main(String []args)
    {
        Frame f=new JFrame("LHL'S Java");
        Panel p1=new Panel();
        p1.add(new TextField(30));
        f.add(p1,BorderLayout.NORTH);

        Panel p2=new Panel();
        p2.setLayout(new GridLayout(3,4,4,4));
        for(int i=0;i<10;i++)
        {
            p2.add(new Button(i+""));
        }
        p2.add(new Button("+"));
        p2.add(new Button("-"));
        p2.add(new Button("*"));
        p2.add(new Button("/"));
        f.add(p2,BorderLayout.SOUTH);
        f.setVisible(true);
        f.pack();
        //f.setBounds(10,10,500,300);
    }
}
---------------------------------------
import javax.smartcardio.Card;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test
{
    public static void main(String[] args)
    {
        Frame f = new JFrame("LHL'S Java");
        Panel p1 = new Panel();
        CardLayout cardLayout = new CardLayout();
        p1.setLayout(cardLayout);
        String names[] = {"第一张", "第二张", "第三张", "第四张", "第五张"};
        for (int i = 0; i < names.length; i++)
            p1.add(names[i], new Button(names[i]));
        f.add(p1, BorderLayout.CENTER);
        Panel p2 = new Panel();
        Button b1 = new Button("上一张");
        Button b2 = new Button("下一张");
        Button b3 = new Button("第一张");
        Button b4 = new Button("最后一张");
        Button b5 = new Button("第三张");
        ActionListener listener = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String s = e.getActionCommand();
                switch (s)
                {
                    case "上一张":
                        cardLayout.previous(p1);
                        break;
                    case "下一张":
                        cardLayout.next(p1);
                        break;
                    case "第一张":
                        cardLayout.first(p1);
                        break;
                    case "最后一张":
                        cardLayout.last(p1);
                        break;
                    case "第三张":
                        cardLayout.show(p1, "第三张");
                        break;
                }
            }
        };
        b1.addActionListener(listener);
        b2.addActionListener(listener);
        b3.addActionListener(listener);
        b4.addActionListener(listener);
        b5.addActionListener(listener);

        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(b4);
        p2.add(b5);
        f.add(p2, BorderLayout.SOUTH);


        f.setVisible(true);
        f.pack();
        //f.setBounds(10,10,500,300);
    }
}
---------------------------------
import javax.swing.*;
import java.awt.*;

public class  test
{
    public static void main(String []args)
    {
        Frame frame=new Frame("LHL");
        BoxLayout boxLayout=new BoxLayout(frame,BoxLayout.Y_AXIS);
        frame.setLayout(boxLayout);
        frame.add(new Button("按钮1"));
        frame.add(new Button("按钮2"));
        frame.pack();
        frame.setVisible(true);
        

    }
}
------------------------------
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;

import javax.swing.*;
import java.awt.*;
class create
{
    Frame frame=new Frame();
    TextArea ta=new TextArea(5,50);
    Choice colorChooser=new Choice();
    CheckboxGroup cbg=new CheckboxGroup();
    Checkbox male=new Checkbox("男",cbg,true);
    Checkbox female=new Checkbox("女",cbg,false);
    Checkbox isMarried =new Checkbox("是否已婚");
    TextField tf=new TextField(20);
    Button ok=new Button("确认");
    List colorList=new List(6,true);

    create()
    {
        Box bBox=Box.createHorizontalBox();
        bBox.add(tf);
        bBox.add(ok);
        frame.add(bBox,BorderLayout.SOUTH);
        Box cBox=Box.createHorizontalBox();
        colorChooser.add("红色");
        colorChooser.add("蓝色");
        colorChooser.add("绿色");
        cBox.add(colorChooser);
        cBox.add(male);
        cBox.add(female);
        cBox.add(isMarried);
        Box topLeft=Box.createVerticalBox();
        topLeft.add(ta);
        topLeft.add(cBox);
        Box top=Box.createHorizontalBox();
        top.add(topLeft);
        colorList.add("红色");
        colorList.add("蓝色");
        colorList.add("绿色");
        top.add(colorList);
        frame.add(top);
        frame.setVisible(true);
        frame.pack();
        frame.setBounds(10,10,400,600);
    }

}
public class test
{

    public static void main(String []args)
    {
        new create();

    }
}
------------------------------
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test
{

    public static void main(String[] args)
    {
        Frame frame =new Frame();
        Dialog d1=new Dialog(frame,"模式对话框",true);
        Dialog d2=new Dialog(frame,"非模式对话框",false);
        d1.setBounds(20,30,300,200);
        d2.setBounds(20,30,300,200);
        Button b1=new Button("打开模式对话框");
        Button b2=new Button("打开非模式对话框");
        b1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                d1.setVisible(true);
            }
        });
        b2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                d2.setVisible(true);
            }
        });
        frame.add(b1,BorderLayout.NORTH);
        frame.add(b2);
        frame.pack();
        frame.setVisible(true);
    }
}
-----------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test
{

    public static void main(String[] args)
    {
        Frame frame =new Frame();
        Dialog d1=new Dialog(frame,"模式对话框",true);
        Dialog d2=new Dialog(frame,"非模式对话框",false);
        d1.add(new TextField(30),BorderLayout.CENTER);
        d1.add(new Button("确认"),BorderLayout.SOUTH);
        d1.setBounds(20,30,300,200);
        d2.setBounds(20,30,300,200);
        Button b1=new Button("打开模式对话框");
        Button b2=new Button("打开非模式对话框");
        b1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                d1.setVisible(true);
            }
        });
        b2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                d2.setVisible(true);
            }
        });
        frame.add(b1,BorderLayout.NORTH);
        frame.add(b2);
        frame.pack();
        frame.setVisible(true);
    }
}
------------------------------------
点击出现Hello
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test
{

    public static void main(String[] args)
    {
        Frame frame=new Frame("LHL's Java");
        FileDialog f1=new FileDialog(frame,"选择要打开的文件",FileDialog.LOAD);
        FileDialog f2=new FileDialog(frame,"选择要保存的路径",FileDialog.SAVE);
        Button b1=new Button("打开文件");
        Button b2=new Button("保存文件");
        b1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                f1.setVisible(true);
                String directory=f1.getDirectory();
                String file=f1.getFile();
                System.out.println("打开的文件路径为:"+directory);
                System.out.println("打开的文件名为:"+file);
            }
        });
        b2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String directroy=f2.getDirectory();
                String file=f2.getFile();
                System.out.println("保存的文件路径为:"+directroy);
                System.out.println("保存的文件名为:"+file);
            }
        });
        frame.add(b1,BorderLayout.NORTH);
        frame.add(b2);
        frame.setVisible(true);
        frame.pack();
    }
}

文章作者: LHL
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 LHL !
评论
  目录