Swing
标签
- 文字标签
new JLable("xxx")
- 图标
package com.yuecheng.guistudy.Demo01;
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){}
public IconDemo(int width,int height){
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(20,20);
//图标放在标签上,也可以放在按钮上
JLabel icontest = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
//获取容器
Container contentPane = getContentPane();
//将标签放入容器
contentPane.add(icontest);
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
- 图片
package com.yuecheng.guistudy.Demo01;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Demo02 extends JFrame {
public Demo02(){
JLabel jLabel = new JLabel("imageIcon");
//获取图片地址
URL url = Demo02.class.getResource("手机2.png");
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new Demo02();
}
}
消息提示对话框
package com.yuecheng.guistudy.Demo01;
import javax.swing.*;
public class Demo03 {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setVisible(true);
jFrame.setBounds(0,0,500,500);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//showMassageDialog的用法
//JOptionPane.showMessageDialog(null,"友情提示");
//JOptionPane.showMessageDialog(null,"提示消息","标题",JOptionPane.WARNING_MESSAGE);
//JOptionPane.showMessageDialog(null,"提示消息","标题",JOptionPane.ERROR_MESSAGE);
String n = JOptionPane.showInputDialog("请输入的名字","彭于晏");
JOptionPane.showConfirmDialog(null,"你真的是"+n+"?");
}
}
面板
JPanel
package com.yuecheng.guistudy.Demo01;
import javax.swing.*;
import java.awt.*;
public class JpanelDemo01 extends JFrame{
public JpanelDemo01(){
//设置容器
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10));//后面两个是上下间距
//new出面板,并设置布局
JPanel jPanel = new JPanel(new GridLayout(1,3));
JPanel jPanel1 = new JPanel(new GridLayout(1,2));
JPanel jPanel2= new JPanel(new GridLayout(2,1));
JPanel jPanel3 = new JPanel(new GridLayout(1,3));
//将按钮放入吗,面板
jPanel.add(new JButton("11"));
jPanel.add(new JButton("11"));
jPanel.add(new JButton("11"));
jPanel1.add(new JButton("22"));
jPanel1.add(new JButton("22"));
jPanel2.add(new JButton("33"));
jPanel2.add(new JButton("33"));
jPanel3.add(new JButton("44"));
jPanel3.add(new JButton("44"));
jPanel3.add(new JButton("44"));
//设置可见
this.setSize(500,500);
this.setVisible(true);
//关闭事件
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//将面板添加入容器
contentPane.add(jPanel);
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
}
public static void main(String[] args) {
//new出该类
new JpanelDemo01();
}
}
滚动框JScroll
package com.yuecheng.guistudy.lesson03;
import javax.swing.*;
import java.awt.*;
public class Demo01 extends JFrame {
public Demo01(){
Container contentPane = this.getContentPane();
TextArea textArea = new TextArea(20,50);
//设置文本框默认文本
textArea.setText("欢迎学习java");
//contentPane.add(textArea);
//new一个面板jscroll
JScrollPane jScrollPane = new JScrollPane(textArea);
contentPane.add(jScrollPane);
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Demo01 demo01 = new Demo01();
}
}
按钮
- 图片按钮
package com.yuecheng.guistudy.lesson03;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Demo03 extends JFrame {
public Demo03(){
Container contentPane = this.getContentPane();
URL resource = Demo03.class.getResource("手机3.png");
//图片变为一个图标
Icon icon =new ImageIcon(resource);
//吧2图标放到按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
contentPane.add(button);
this.setSize(600,600);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo03();
}
}
- 单选按钮
package com.yuecheng.guistudy.lesson03;
import com.yuecheng.oop.Demo05.B;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.security.KeyStore;
public class Demo04 extends JFrame {
public Demo04(){
Container contentPane = this.getContentPane();
URL resource = Demo03.class.getResource("手机3.png");
//图片变为一个图标
Icon icon =new ImageIcon(resource);
JRadioButton jRadioButton01 = new JRadioButton("我是一个单选框");
JRadioButton jRadioButton02 = new JRadioButton("我是一个");
JRadioButton jRadioButton03 = new JRadioButton("我是");
//由于单选框只能选择一个,所以经常会进行分组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton01);
buttonGroup.add(jRadioButton02);
buttonGroup.add(jRadioButton03);
contentPane.add(jRadioButton01,BorderLayout.CENTER);
contentPane.add(jRadioButton02,BorderLayout.NORTH);
contentPane.add(jRadioButton03,BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo04();
}
}
- 复选按钮
package com.yuecheng.guistudy.lesson03;
import com.yuecheng.oop.Demo05.B;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.security.KeyStore;
public class Demo04 extends JFrame {
public Demo04(){
Container contentPane = this.getContentPane();
URL resource = Demo03.class.getResource("手机3.png");
//图片变为一个图标
Icon icon =new ImageIcon(resource);
//多选框的操作
Checkbox checkbox01 = new Checkbox("checkbox01");
Checkbox checkbox02 = new Checkbox("checkbox02");
contentPane.add(checkbox01,BorderLayout.NORTH);
contentPane.add(checkbox02,BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args) {
new Demo04();
}
}