Frame
package gui.lesson01;
import java.awt.Color;
import java.awt.Frame;
public class FrameTest {
public static void main(String[] args) {
Frame frame=new Frame("Java窗口");
frame.setVisible(true);
frame.setSize(400, 400);
frame.setBackground(new Color(200,200,200));
frame.setLocation(100,100);
frame.setResizable(false);
}
}
封装
package gui.lesson01;
import java.awt.Color;
import java.awt.Frame;
public class FrameTset1 {
public static void main(String[] args) {
MyFrame myFrame= new MyFrame(220, 220, 100, 100,Color.BLUE);
MyFrame myFrame1= new MyFrame(250, 250, 100, 100,Color.CYAN);
MyFrame myFrame2= new MyFrame(280, 280, 100, 100,Color.GRAY);
myFrame.test();
myFrame1.test();
myFrame2.test();
}
}
class MyFrame extends Frame{
private static final long serialVersionUID = 1L;
static int id=0;
public MyFrame() {
}
public MyFrame(int x,int y,int w,int h,Color color) {
super("MyFrame"+(++id));
setVisible(true);
setSize(h,w);
setBackground(color);
setLocation(x, y);
setResizable(false);
}
public void test(){
System.out.println(id);
}
}
Panel
package gui.lesson01;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class PanelTest {
public static void main(String[] args) {
Frame frame=new Frame();
Panel panel=new Panel();
frame.setLayout(null);
frame.setBounds(200, 200, 300, 300);
frame.setBackground(Color.BLUE);
panel.setBounds(50, 50, 200, 200);
panel.setBackground(Color.lightGray);
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
常用布局
流式布局
package gui.layout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FlowLayoutTest {
public static void main(String[] args) {
Frame frame=new Frame();
Button button1=new Button("one");
Button button2=new Button("two");
Button button3=new Button("three");
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(300, 300);
frame.setVisible(true);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
东西南北中
package gui.layout;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BorderLayoutTest {
public static void main(String[] args) {
Frame frame=new Frame("BorderLayoutTest");
Button button1=new Button("east");
Button button2=new Button("west");
Button button3=new Button("south");
Button button4=new Button("north");
Button button5=new Button("center");
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.SOUTH);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.CENTER);
frame.setSize(200, 200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
表格布局
package gui.layout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GirdLayoutTest {
public static void main(String[] args) {
Frame frame=new Frame("GirdrLayoutTest");
Button button1=new Button("btn1");
Button button2=new Button("btn2");
Button button3=new Button("btn3");
Button button4=new Button("btn4");
Button button5=new Button("btn5");
Button button6=new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
嵌套
package gui.layout;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class LayoutTest {
public static void main(String[] args) {
Frame frame=new Frame();
frame.setSize(500,500);
frame.setLocation(300, 300);
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true);
Panel panel1=new Panel(new BorderLayout());
Panel panel2=new Panel(new GridLayout(2,1));
Panel panel3=new Panel(new BorderLayout());
Panel panel4=new Panel(new GridLayout(2,2));
panel1.add(new Button("aaa"),BorderLayout.EAST);
panel1.add(new Button("bbb"),BorderLayout.WEST);
panel2.add(new Button());
panel2.add(new Button());
panel1.add(panel2,BorderLayout.CENTER);
panel3.add(new Button("ccc"),BorderLayout.EAST);
panel3.add(new Button("ddd"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
panel4.add(new Button());
}
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
监听器
事件监听器
package gui.listener;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ActionTest {
public static void main(String[] args) {
Frame frame=new Frame();
Button button=new Button("hello");
frame.add(button);
frame.pack();
frame.setVisible(true);
MyActionListener myActionListener=new MyActionListener();
button.addActionListener(myActionListener);
windowsClose(frame);
}
private static void windowsClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
文字输入
package gui.listener;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TextTest {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
private static final long serialVersionUID = 1L;
public MyFrame() {
TextField textField=new TextField();
add(textField);
MyActionListenerTwo myActionListener=new MyActionListenerTwo();
textField.addActionListener(myActionListener);
textField.setEchoChar('*');
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListenerTwo implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field=(TextField)e.getSource();
System.out.println(field.getText());
field.setText("");
}
}
计算器
package gui.listener;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CalaTest {
public static void main(String[] args) {
new Calulator().loadFrame();
}
}
class Calulator extends Frame{
private static final long serialVersionUID = 1L;
TextField num1,num2,num3;
public void loadFrame() {
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(20);
Button button=new Button("=");
Label label=new Label("+");
button.addActionListener(new MyCalulatorListener());
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public Calulator() {
}
}
class MyCalulatorListener implements ActionListener{
Calulator calulator=null;
public MyCalulatorListener() {
}
public MyCalulatorListener(Calulator calulator) {
this.calulator=calulator;
}
@Override
public void actionPerformed(ActionEvent e) {
int num1=Integer.parseInt(calulator.num1.getText());
int num2=Integer.parseInt(calulator.num2.getText());
calulator.num3.setText(""+(num1+num2));
calulator.num1.setText("");
calulator.num2.setText("");
}
}
鼠标监听
package gui.listener;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class MouseListener {
public static void main(String[] args) {
new MyMouseFrame("画图");
}
}
class MyMouseFrame extends Frame{
ArrayList<Object> points;
private static final long serialVersionUID = 1L;
public MyMouseFrame() {
}
public MyMouseFrame(String title) {
super(title);
setBounds(200,200,400,300);
points=new ArrayList<Object>();
this.addMouseListener(new MyMouseListener());
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
Iterator<Object> iterator=points.iterator();
while (iterator.hasNext()) {
Point point = (Point) iterator.next();
g.setColor(Color.blue);
g.fillOval(point.x, point.y, 10, 10);
}
}
public void addPaint(Point point) {
points.add(point);
}
private class MyMouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
MyMouseFrame frame=(MyMouseFrame)e.getSource();
frame.addPaint(new Point(e.getX(),e.getY()));
frame.repaint();
}
}
}
窗口监听
package gui.listener;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WindowTest {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
private static final long serialVersionUID = 1L;
public WindowFrame() {
pack();
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
键盘监听
package gui.listener;
import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class KeyTest {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
private static final long serialVersionUID = 1L;
public KeyFrame() {
setBounds(100,200,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP) {
System.out.println("上");
}
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
JFrame
JFrame
package gui.swing;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class JFrameDemo {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
private static final long serialVersionUID = 1L;
public void init() {
this.setBounds(200, 200, 200, 200);
this.setVisible(true);
Container container=this.getContentPane();
container.setBackground(Color.cyan);
JLabel jLabel=new JLabel("hi");
this.add(jLabel);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public MyJFrame() {
}
}
弹窗
package gui.swing;
import java.awt.Container;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class DialogTest extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new DialogTest();
}
public DialogTest() {
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container=this.getContentPane();
container.setLayout(null);
JButton jButton=new JButton("this");
jButton.setBounds(30,30,200,50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
container.add(jButton);
}
}
class MyDialog extends JDialog{
private static final long serialVersionUID = 1L;
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,300,300);
Container container=this.getContentPane();
container.add(new Label("hhh"));
}
}
图片标签
package gui.swing;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class IconDemo extends JFrame implements Icon{
private static final long serialVersionUID = 1L;
private int width;
private int heigth;
public static void main(String[] args) {
new IconDemo();
}
public void init() {
IconDemo iconDemo= new IconDemo(5,15);
JLabel jLabel= new JLabel("icontest",iconDemo,SwingConstants.CENTER);
Container container=getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public IconDemo() {
JLabel jLabel=new JLabel("ImageLcon");
URL url=IconDemo.class.getResource("aa.jpeg");
ImageIcon imageIcon= new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container=getContentPane();
container.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 1000, 500);
}
public IconDemo(int witdh,int heigth) {
this.width=witdh;
this.heigth=heigth;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, heigth);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.heigth;
}
}
JScroll
package gui.swing;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class JScrollDemo extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo() {
Container container=this.getContentPane();
JTextArea jTextArea=new JTextArea(20,50);
jTextArea.setText("hi");
JScrollPane jScrollPane=new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,300,300);
}
}
图片按钮
package gui.swing;
import java.awt.Container;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class JButtonDemo01 extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new JButtonDemo01();
}
public JButtonDemo01() {
Container container=this.getContentPane();
URL url=JButtonDemo01.class.getResource("aa.jpeg");
Icon icon=new ImageIcon(url);
JButton button=new JButton();
button.setIcon(icon);
button.setToolTipText("image");
container.add(button);
this.setVisible(true);
this.setBounds(100, 100, 200, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
单选
package gui.swing;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
public class JButtonDemo02 extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new JButtonDemo02();
}
public JButtonDemo02() {
Container container=this.getContentPane();
JRadioButton button1=new JRadioButton("1");
JRadioButton button2=new JRadioButton("2");
JRadioButton button3=new JRadioButton("3");
ButtonGroup buttonGroup=new ButtonGroup();
buttonGroup.add(button1);
buttonGroup.add(button2);
buttonGroup.add(button3);
container.add(button1,BorderLayout.NORTH);
container.add(button2,BorderLayout.CENTER);
container.add(button3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100, 100, 200, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
多选
package gui.swing;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class JButtonDemo03 extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new JButtonDemo03();
}
public JButtonDemo03() {
Container container=this.getContentPane();
JCheckBox jCheckBox1=new JCheckBox("1");
JCheckBox jCheckBox2=new JCheckBox("2");
JCheckBox jCheckBox3=new JCheckBox("3");
container.add(jCheckBox1,BorderLayout.NORTH);
container.add(jCheckBox2,BorderLayout.CENTER);
container.add(jCheckBox3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100, 100, 200, 200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
下拉框
package gui.lesson06;
import java.awt.Container;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class ConboxDemo01 extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new ConboxDemo01();
}
public ConboxDemo01() {
Container container=this.getContentPane();
JComboBox<Object> status=new JComboBox<Object>();
status.addItem(null);
status.addItem("aa");
status.addItem("bb");
status.addItem("cc");
container.add(status);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100, 100, 300, 300);
}
}
密码框+列表
package gui.lesson06;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPasswordField;
import javax.swing.WindowConstants;
public class ConboxDemo02 extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new ConboxDemo02();
}
public ConboxDemo02() {
Container container=this.getContentPane();
String[] content= {"1","2","3"};
JList<String> jList=new JList<String>(content);
JPasswordField passwordField=new JPasswordField();
passwordField.setEchoChar('*');
container.add(jList,BorderLayout.CENTER);
container.add(passwordField,BorderLayout.NORTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100, 100, 300, 300);
}
}