Swing
标签
Icon标签
将绘制的图形赋给JLabel
package com.bao.lesson10;
import javax.swing.*;
import java.awt.*;
public class MyIcon extends JFrame implements Icon {
//属性
private int width;
private int height;
public MyIcon() {};//无参构造
public MyIcon(int width,int height) {
this.width = width;
this.height = height;
}
public void init(){
Container contentPane = getContentPane();
MyIcon myIcon = new MyIcon(15,15);
JLabel label = new JLabel("icontest", myIcon, SwingConstants.CENTER);
contentPane.add(label);
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 width;
}
@Override
public int getIconHeight() {
return height;
}
public static void main(String[] args) {
new MyIcon().init();
}
}
ImageIcon标签
将图片附在JLabel上
package com.bao.lesson10;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class MyImageIcon extends JFrame {
public MyImageIcon() {
JLabel jLabel = new JLabel("Imageicon");
//获取图片的地址
URL url = MyImageIcon.class.getResource("icon_机票_nor.png");
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new MyImageIcon();
}
}
[!IMPORTANT]
- 可以通过 MyImageIcon.class.getResource(“icon_机票_nor.png”)这个方法来获取本包下的图片地址
- JFrame的背景色,增加组件都需要实例化容器,在容器上进行才可以
面板 JScrollPane及文本域
相当于Frame中的Panel
package com.bao.lesson10;
import javax.swing.*;
import java.awt.*;
public class JScrollPanel extends JFrame {
public JScrollPanel(){
Container contentPane = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("hello everybody");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);
contentPane.add(jScrollPane);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(100, 100, 500, 500);
}
public static void main(String[] args) {
new JScrollPanel();
}
}
文本域在特定情况下会产生滚动条,可以进行滚动
按钮
图片按钮
按钮里面附着着图片:
package com.bao.lesson10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
public class ButtonDemo extends JFrame {
public ButtonDemo(){
Container container = this.getContentPane();//获得容器
JButton press = new JButton("press");
press.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("pressed");
}
});
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
container.setBackground(Color.CYAN);
this.setBounds(100,100,500,500);
container.setBounds(100, 100, 500, 500);
container.add(press);
URL url = ButtonDemo.class.getResource("icon_机票_nor.png");
ImageIcon icon = new ImageIcon(url);
press.setIcon(icon);
press.setHorizontalAlignment(SwingConstants.CENTER);
}
public static void main(String[] args) {
new ButtonDemo();
}
}
实现效果:
单选框
package com.bao.lesson10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
public class ButtonDemo1 extends JFrame {
public ButtonDemo1(){
Container container = this.getContentPane();//获得容器
JRadioButton press1 = new JRadioButton("lalala1");
JRadioButton press2 = new JRadioButton("lalala2");
JRadioButton press3 = new JRadioButton("lalala3");
//单选框,需要分组,不然会导致多个都可以选择 一个组中只能选一个
ButtonGroup group = new ButtonGroup();
group.add(press1);
group.add(press2);
group.add(press3);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
container.setBackground(Color.CYAN);
container.setLayout(new GridLayout(1,3,10,10));
container.add(press1);
container.add(press2);
container.add(press3);
this.pack();
this.setBounds(100,100,500,500);
}
public static void main(String[] args) {
new ButtonDemo1();
}
}
实现效果:
补充:
单选框需要进行分组,因为每个组别里,多个单选框只能选一个
复选框
package com.bao.lesson10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
public class ButtonDemo2 extends JFrame {
public ButtonDemo2(){
Container container = this.getContentPane();//获得容器
JCheckBox box1 = new JCheckBox("la");
JCheckBox box2 = new JCheckBox("la");
JCheckBox box3 = new JCheckBox("la");
//多选框不需要愤分组,因为没有限制选择数量
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
container.setBackground(Color.CYAN);
container.setLayout(new GridLayout(1,3,10,10));
container.add(box1);
container.add(box2);
container.add(box3);
this.pack();
this.setSize(500,500);
}
public static void main(String[] args) {
new ButtonDemo2();
}
}
下拉框 列表框
下拉框
package com.bao.lessonn11;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
public class Select extends JFrame {
public Select() {
Container contentPane = this.getContentPane();
JComboBox jComboBox = new JComboBox();
//加下拉后的选项
jComboBox.addItem("你的名字");
jComboBox.addItem("言叶之庭");
jComboBox.addItem("天气之子");
contentPane.add(jComboBox);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
}
public static void main(String[] args) {
new Select();
}
}
列表框
package com.bao.lessonn11;
import javax.swing.*;
import java.awt.*;
public class ul extends JFrame {
public ul() {
Container pane = this.getContentPane();
//生成列表的内容 数组
String[] content = {"1","2","3"};
//列表中放入内容
JList jList = new JList(content);
pane.add(jList);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
}
public static void main(String[] args) {
new ul();
}
}
文本框 密码框 文本域
文本框
package com.bao.lessonn11;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
public class TextField extends JFrame {
public TextField() {
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(1,2));
JTextField textField = new JTextField("hello", 30);
JTextField textField1 = new JTextField("bye", 30);
contentPane.add(textField);
contentPane.add(textField1);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
}
public static void main(String[] args) {
new TextField();
}
}
密码框
package com.bao.lessonn11;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
public class TextField1 extends JFrame {
public TextField1() {
Container contentPane = this.getContentPane();
//不规范,尽量在面板中设置组件
JPasswordField pass = new JPasswordField("",30);
contentPane.add(pass);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
}
public static void main(String[] args) {
new TextField1();
}
}
文本域
之前写过了