窗口
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init():初始化
public void init(){
//顶级窗口
JFrame jframe = new JFrame("这是一个JFrame窗口");
jframe.setVisible(true);
jframe.setBounds(100,100,100,100);
//这里设置颜色是不好使的,因为需要容器实例化进行容器的设置颜色
jframe.setBackground(Color.red);
//设置文字 JLabel
JLabel jLabel = new JLabel("欢迎");
jframe.add(jLabel);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJframe2().init();
}
}
class MyJframe2 extends JFrame{
public void init(){
this.setBounds(10,10,10,10);
this.setVisible(true);
Container container=this.getContentPane();
container.setBackground(Color.red);
JLabel jlabel = new JLabel("你好");
this.add(jlabel);
//让文本标签居中
jlabel.setHorizontalAlignment(SwingConstants.CENTER);
}
}
加图标(画笔画的)
import javax.swing.*;
import java.awt.*;
//图标Icon是接口,需要实现类
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(15,15);
//图标放在标签,也可以放在按钮上
JLabel jLabel = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return 0;
}
@Override
public int getIconHeight() {
return 0;
}
public static void main(String[] args) {
new IconDemo().init();
}
}
加图片
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
JLabel label=new JLabel("ImageIcon");
//获取当前类下所在项目下的第一级资源的文件
URL url = ImageIconDemo.class.getResource("1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container=getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
弹窗
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container container=this.getContentPane();
//绝对布局 自定义坐标布局
container.setLayout(null);
//按钮
JButton button=new JButton("点击弹出一个对话框");
button.setBounds(100,100,200,40);
container.add(button);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
//这个是错误的,因为JDialog退出只能DO_ONTHING,HIDE,DISPOSE三个中的一个
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
JLabel jLabel = new JLabel("一起来学习");
contentPane.add(jLabel);
//自定义布局后不要手动设置,不然看不到
jLabel.setBounds(200,200,300,100);
}
}
把一个图片变成按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container container=this.getContentPane();
//将一个图片变为图标
URL resource=JButtonDemo01.class.getResource("1.jpg");
Icon icon=new ImageIcon(resource);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
单选框
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo02 extends JFrame{
public JButtonDemo02(){
Container container=this.getContentPane();
//将一个图片变为图标
URL resource=JButtonDemo01.class.getResource("1.jpg");
Icon icon=new ImageIcon(resource);
//单选框
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);
container.add(jRadioButton1,BorderLayout.CENTER);
container.add(jRadioButton2,BorderLayout.NORTH);
container.add(jRadioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
多选框
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo03 extends JFrame{
public JButtonDemo03(){
Container container=this.getContentPane();
//将一个图片变为图标
URL resource=JButtonDemo01.class.getResource("1.jpg");
Icon icon=new ImageIcon(resource);
//多选框
JCheckBox jCheckBox01 = new JCheckBox("1");
JCheckBox jCheckBox02 = new JCheckBox("2");
JCheckBox jCheckBox03 = new JCheckBox("3");
container.add(jCheckBox01,BorderLayout.CENTER);
container.add(jCheckBox02,BorderLayout.NORTH);
container.add(jCheckBox03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
面板
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container=this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面参数代表间距
JPanel panel1=new JPanel(new GridLayout(1,3));
panel1.add(new JButton("11"));
panel1.add(new JButton("12"));
panel1.add(new JButton("13"));
container.add(panel1);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
滚动文本框
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container=this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("正在学习JAVA");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,150);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
文本框,密码框
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container container=this.getContentPane();
//文本框
JTextField jTextField1 = new JTextField("Hello");
JTextField jTextField2 = new JTextField("World",20);
container.add(jTextField1,BorderLayout.NORTH);
container.add(jTextField2,BorderLayout.SOUTH);
//密码框
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField,BorderLayout.CENTER);
//文本域 可拉伸的文本框 加到面板上使用的
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
下拉框
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01(){
Container container=this.getContentPane();
JComboBox status=new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
列表
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo02 extends JFrame {
/*
下拉框
*/
public TestComboboxDemo02(){
Container container=this.getContentPane();
//生成列表的内容
//String[] contents={"1","2","3"};
Vector contents=new Vector();
//列表中需要放入内容
JList jList=new JList(contents);
contents.add("123");
contents.add("315");
contents.add("996");
container.add(jList);
this.setVisible(true);
this.setSize(500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}