GUI编程
告诉大家怎么样去学?
这是什么?
他怎么玩?
该如何在平时中使用?
组件
·窗口
·弹窗
·面板
·文本框
·列表框
·按钮
·图片
·监听事件
·鼠标
·键盘事件
·破解工具
1.简介
GUI的核心技术: Swing AWT
1.因为界面不美观
2.需要jre环境
为什么我们要学习?
1.可以写出自己心中想要的小工具
2.工作时候,也可以维护到swing界面 概率极小!
3.了解MVC框架 了解监听
2.AWT
2.1AWT简介
package com.zhaochen;
import java.awt.*;
//GUI的第一个界面
public class testFrame {
public static void main(String[] args) {
//frame JDK:看源码
Frame frame = new Frame("我的第一个java图形界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400, 400);
//设置背景颜色
frame.setBackground(new Color(236, 16, 16));
//弹出的初始位置
frame.setLocation(200, 200);
//设置大小固定
frame.setResizable(false);
}
}
package com.zhaochen;
import java.awt.*;
public class test {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.yellow);
MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.blue);
MyFrame myFrame3 = new MyFrame(500,100,200,200,Color.red);
MyFrame myFrame4 = new MyFrame(700,100,200,200,Color.pink);
MyFrame myFrame5 = new MyFrame(900,100,200,200,Color.green);
}
}
class MyFrame extends Frame{
static int id = 0 ; //可能存在多个窗口 我们需要一个计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
//setSize和setLocation合体为setBounds
setBounds(x,y,h,w);
setVisible(true);
setBackground(color);
}
}
2.2组件和容器
解决了窗口的关闭问题
package com.zhaochen;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//Panel面板 可以看成一个空间:但是不能单独存在
public class testPanel {
public static void main(String[] args) {
Frame frame = new Frame("我的第二个java界面");
//布局的概念
Panel panel = new Panel();
//设计布局
frame.setLayout(null);
//设置坐标
frame.setBounds(300,300,500,500);
//设置背景
frame.setBackground(Color.green);
//设置可视度
frame.setVisible(true);
//panel设置坐标 相对于frame坐标
panel.setBounds(50,50,400,400);
panel.setBackground(Color.yellow);
//frame.add(panel)
frame.add(panel);
//监听事件,监听窗口关闭事件 System.exit()
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
//窗口关闭时应该干什么
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
2.3布局管理器
·流式布局 flowlayout
package com.zhaochen;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("我的第三个窗口");
frame.setVisible(true);
frame.setBackground(Color.orange);
frame.setBounds(200,200,300,300);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//组件 按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(button1);
frame.add(button2);
frame.add(button3);
}
}
·东南西北中 borderlayout
```java
package com.zhaochen;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("我的第三个窗口");
frame.setVisible(true);
frame.setBackground(Color.orange);
frame.setBounds(200, 200, 300, 300);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setLayout(new BorderLayout());
Button button1 = new Button("south");
Button button2 = new Button("north");
Button button3 = new Button("west");
Button button4 = new Button("east");
Button button5 = new Button("center");
button1.setBackground(Color.yellow);
button2.setBackground(Color.green);
button3.setBackground(Color.blue);
button4.setBackground(Color.red);
button5.setBackground(Color.pink);
frame.add(button1,BorderLayout.SOUTH);
frame.add(button2,BorderLayout.NORTH);
frame.add(button3,BorderLayout.WEST);
frame.add(button4,BorderLayout.EAST);
frame.add(button5,BorderLayout.CENTER);
}
}
·网格布局 GridLayout
package com.zhaochen;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("我的第三个窗口");
frame.setVisible(true);
frame.setBackground(Color.orange);
frame.setBounds(200,200,300,300);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//组件 按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
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(); //包含于java包内
}
}
2.4大小 布局 可见性 背景颜色 监听!
test 嵌套
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class test1 {
public static void main(String[] args) {
Frame frame = new Frame("test");
frame.setVisible(true);
frame.setBackground(Color.BLUE);
frame.setBounds(200,200,300,300);
frame.setLayout(new GridLayout(2,1));
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Panel panel1 = new Panel();
panel1.setLayout(new BorderLayout());
Panel panel2 = new Panel();
panel2.setLayout(new GridLayout(2,1));
Panel panel3 = new Panel();
panel3.setLayout(new BorderLayout());
Panel panel4 = new Panel();
panel4.setLayout(new GridLayout(2,2));
Button button1 = new Button("b1");
Button button2 = new Button("b2");
Button button3 = new Button("b3");
Button button4 = new Button("b4");
Button button5 = new Button("b5");
Button button6 = new Button("b6");
Button button7 = new Button("b7");
Button button8 = new Button("b8");
Button button9 = new Button("b9");
Button button10 = new Button("b10");
panel1.add(button1,BorderLayout.WEST);
panel1.add(button2,BorderLayout.EAST);
panel2.add(button3);
panel2.add(button4);
panel1.add(panel2,BorderLayout.CENTER);
frame.add(panel1);
panel3.add(button5,BorderLayout.WEST);
panel3.add(button6,BorderLayout.EAST);
panel4.add(button7);
panel4.add(button8);
panel4.add(button9);
panel4.add(button10);
panel3.add(panel4,BorderLayout.CENTER);
frame.add(panel3);
}
}
2.5 事件监听
事件监听:当某件事情发生之时,干什么??
按钮监听事件
i
mport jdk.swing.interop.SwingInterOpUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class testActionEven {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame("test");
Button button = new Button("b1");
//关闭窗口的事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
frame.setBounds(200,200,300,300);
frame.setLayout(new BorderLayout());
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
//因为我们需要一个Actionlistener 所以我们需要构建一个ActionListener
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
});
}
}
两个按钮监听同一个事件
package com.zhaochen;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testButtonTwo {
public static void main(String[] args) {
//两个按钮 监听同一件事件
// 开始 停止
Frame frame = new Frame("开始 停止");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setBounds(200,200,300,300);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
Button button1 = new Button("start");
Button button2 = new Button("stop");
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
//可以被触发会返回的的命令,如果不显示定义,则走默认的指令
//多个按钮只写一个监听类
button2.setActionCommand("button2-stop");
Mymonitor mymonitor = new Mymonitor();
button1.addActionListener(mymonitor);
button2.addActionListener(mymonitor);
}
}
class Mymonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//
if(e.getActionCommand().equals("start")) {
System.out.println("按钮被点击了!" + e.getActionCommand());
}
else{
System.out.println("stop");
}
}
}
事件监听 发生选择条件
package com.zhaochen;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testButtonTwo {
public static void main(String[] args) {
//两个按钮 监听同一件事件
// 开始 停止
Frame frame = new Frame("开始 停止");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setBounds(200,200,300,300);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
Button button1 = new Button("start");
Button button2 = new Button("stop");
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
//可以被触发会返回的的命令,如果不显示定义,则走默认的指令
//多个按钮只写一个监听类
button2.setActionCommand("button2-stop");
Mymonitor mymonitor = new Mymonitor();
button1.addActionListener(mymonitor);
button2.addActionListener(mymonitor);
}
}
class Mymonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//
if(e.getActionCommand().equals("start")) {
System.out.println("按钮被点击了!" + e.getActionCommand());
}
else{
System.out.println("stop");
}
}
}
2.6.输入框TextField 监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testTextField {
public static void main(String[] args) {
//只管启动!
myFrame myFrame = new myFrame();
}
}
class myFrame extends Frame {
public myFrame(){
TextField textField = new TextField();
add(textField);
setBounds(200,200,300,300);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//监听这个文本框输入的文字
//按下enter就可以触发这个输入框的事件
textField.setEchoChar('*');
textField.addActionListener(new MyActionListener());
//设置替换编码
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField=(TextField) e.getSource();//获得一些资源,返回一个对象
String str =textField.getText();//获得输入框中的文本
System.out.println(str);
textField.setText("");
}
}
2.7 简易计算器 组合+内部类的复习使用
oop原则:组合大于继承
继承
class A extends B{
}
组合
class A{
public B b;
}
package com.zhaochen;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//简易计算器
public class testCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
//三个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
// 一个按钮
Button button = new Button("=");
//按钮的监听事件
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//一个标签
Label label = new Label("+");
//布局
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);
}
});
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.将这个值加法运算后放入第三个框内
num3.setText(""+ (n1+n2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
2.7 画笔
package com.zhaochen;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class testPaint {
public static void main(String[] args) {
MyPaint myPaint = new MyPaint();
myPaint.loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
//画笔需要有颜色 并且画笔可以画画
g.setColor(Color.yellow);
g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100);//实心的圆
//画笔用完 将他还原到最初的颜色
}
}
2.8 鼠标监听
package com.zhaochen;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
public class testMouseListening {
public static void main(String[] args) {
myFrame myFrame = new myFrame("画图");
}
}
//自己的类
class myFrame extends Frame {
//画画需要画笔 画笔需要监听鼠标的当前位置 需要集合存储这些点
ArrayList points;
public myFrame(String title) {
super(title);
setBounds(200,200,400,300);
//鼠标监听器是针对于这个窗口的
addMouseListener(new MyMouseListening());
//存鼠标点击的点
points = new ArrayList<>();
setVisible(true);
}
@Override
public void print(Graphics g) {
//画画需要监听鼠标的事件
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point= (Point) iterator.next();
g.setColor(Color.red);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到该界面上
public void addPoint(Point point){
}
//适配器模式
private class MyMouseListening extends MouseAdapter{
//鼠标: 按下 按住不放 弹起
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
//这里我们鼠标点击时候 会产生一个点!
//这个点就是鼠标的点
com.zhaochen.myFrame frame = new myFrame("title");
frame.addPoint( new Point(e.getX(),e.getY()));
}
}
}
2.9 窗口监听
package com.zhaochen;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testWindow {
public static void main(String[] args) {
new myWindowFrame("这是个窗口");
}
}
class myWindowFrame extends Frame {
public myWindowFrame(String tittle) {
super(tittle);
setVisible(true);
setBounds(200,200,300,300);
setBackground(Color.green);
//匿名内部类
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false); //隐藏窗口 并不是真正的关闭
//System.exit(0); //正常退出
}
}
class MyWindowListen extends WindowAdapter{
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口打开后");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口正在关闭中");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("窗口已经关闭掉");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗口被激活高亮");
}
}
}
2.10 键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class testkeyListener {
public static void main(String[] args) {
MyKeyListener myKeyListener = new MyKeyListener();
}
}
class MyKeyListener extends Frame{
public MyKeyListener() {
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//键盘按下的键是哪一个? 获取当前键盘的码
int keyCode = e.getKeyCode();
//按下w键 发生事件
if(keyCode==KeyEvent.VK_W){
System.exit(0);
}
}
});
}
}
3.Swing
3.1 窗口 面板
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//Init() 初始化一个窗口
public void init(){
//顶级窗口
JFrame frame = new JFrame("这是个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
frame.setBackground(Color.yellow);
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//获得一个容器
Container container=frame.getContentPane();
container.setBackground(Color.yellow);
//设置文字
JLabel label = new JLabel("欢迎!");
label.setFont(new Font("黑体",Font.BOLD,30));
label.setBackground(Color.BLUE);
label.setHorizontalAlignment(SwingConstants.CENTER);
container.add(label);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
3.2弹窗
JDialog 默认就有关闭键
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo() {
setVisible(true);
setSize(700,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Jframe 放东西 容器
Container container = getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button1 = new JButton("点击弹出一个窗口"); //创建
button1.setBounds(30,30,200,50);
//点击这个按钮弹出一个窗口
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { //监听器
//弹窗
new MyDialogDemo();
}
});
container.add(button1);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
setVisible(true);
setBounds(100,100,500,500);
//setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
Container contenter = getContentPane();
contenter.setLayout(null);
contenter.setBackground(Color.yellow);
JLabel jLabel = new JLabel("你好世界");
jLabel.setFont(new Font("黑体",Font.BOLD,20));
jLabel.setSize(200,200);
contenter.add(jLabel);
}
}
3.3标签 JLabel
Label
1. new JLabel("XXX");
ICON 图片标签
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
//图标:需要实现类
public class testIcon extends JFrame implements Icon {
private int height;
private int width;
public testIcon() { } //无参构造
public testIcon(int height,int width) {
this.height = height;
this.width = width;
}
public void init(){
testIcon testIcon = new testIcon(15, 15);
//放在标签上,也可以放在按钮上
JLabel jLabel = new JLabel("testIcon", testIcon, SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,height,width);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
public static void main(String[] args) {
new testIcon().init();
}
}
插入一个图片
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class testImagine extends JFrame {
public testImagine() {
super("hello world");
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = getContentPane();
URL url = testImagine.class.getResource("hu.png");
ImageIcon imageIcon = new ImageIcon(url);
JLabel jLabel = new JLabel();
jLabel.setLayout(null);
jLabel.setIcon(imageIcon);
jLabel.setLocation(0,0);
JButton jButton = new JButton("hello");
jButton.setBounds(10,10,100,100);
container.add(jButton);
container.add(jLabel);
pack();
}
public static void main(String[] args) {
new testImagine();
}
}
3.4面板 JPanel
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
public class Jpanel extends JFrame {
public Jpanel() {
Container container = getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//行 列 上间距 下间距
Jpanel jpanel = new Jpanel();
jpanel.setLayout(new GridLayout(3,1));
container.add(jpanel);
}
public static void main(String[] args) {
Jpanel jpanel = new Jpanel();
}
}
//滚动条
JScrollPanel
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
public class testJScrollPanel extends JFrame {
public testJScrollPanel(){
Container container = getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("hello world");
//滚动条面板
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
setBounds(100,100,300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new testJScrollPanel();
}
}
3.5按钮 JButton
图片按钮
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class testJButton extends JFrame {
public testJButton() {
//将一个图片变为图标
Container contenter = getContentPane();
URL url = testJButton.class.getResource("hu.png");
ImageIcon imageIcon = new ImageIcon(url);
//把这个图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
jButton.setToolTipText("图片按钮");
//add
contenter.add(jButton);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
new testJButton();
}
}
·单选按钮
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class testJButton extends JFrame {
public testJButton() {
//将一个图片变为图标
Container contenter = getContentPane();
URL url = testJButton.class.getResource("hu.png");
ImageIcon imageIcon = new ImageIcon(url);
//单选框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
//由于单选框只能选择一个 所以我们要把他们分成一个组 一个组中只能选一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
contenter.add(jRadioButton1,BorderLayout.NORTH);
contenter.add(jRadioButton2,BorderLayout.CENTER);
contenter.add(jRadioButton3,BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
new testJButton();
}
}
·复选按钮
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class testJButton extends JFrame {
public testJButton() {
//将一个图片变为图标
Container contenter = getContentPane();
URL url = testJButton.class.getResource("hu.png");
ImageIcon imageIcon = new ImageIcon(url);
//多选框
JCheckBox jCheckBox01 = new JCheckBox("jCheckBox01");
JCheckBox jCheckBox02 = new JCheckBox("jCheckBox02");
contenter.add(jCheckBox01,BorderLayout.NORTH);
contenter.add(jCheckBox02,BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
new testJButton();
}
}
3.6 列表
·下拉框
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class testJButton extends JFrame {
public testJButton() {
//将一个图片变为图标
Container contenter = getContentPane();
URL url = testJButton.class.getResource("hu.png");
ImageIcon imageIcon = new ImageIcon(url);
//多选框
JCheckBox jCheckBox01 = new JCheckBox("jCheckBox01");
JCheckBox jCheckBox02 = new JCheckBox("jCheckBox02");
contenter.add(jCheckBox01,BorderLayout.NORTH);
contenter.add(jCheckBox02,BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
new testJButton();
}
}
·列表框
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Vector;
public class textComboBox extends JFrame {
public textComboBox(){
Container container = this.getContentPane();
//生成列表的内容
Vector list= new Vector();
//列表中需要放内容
JList jList = new JList(list);
list.add("1");
list.add("2");
list.add("3");
container.add(jList);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(350,500);
this.setVisible(true);
}
public static void main(String[] args) {
new textComboBox();
}
}
3.7 文本框
package com.zhaochen;
import javax.swing.*;
import java.awt.*;
public class testJScrollPanel extends JFrame {
public testJScrollPanel(){
Container container = getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("hello world");
//滚动条面板
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
setBounds(100,100,300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new testJScrollPanel();
}
}