GUI
2、Swing
2.1JFrame窗体
public class MyJFrame {
//定义init()方法,进行初始化
public void init(){
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setBounds(100,100,400,400);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/*关闭的四种实现
* EXIT_ON_CLOSE 正常关闭
* DISPOSE_ON_CLOSE 直到最后一个窗口被关闭时才能关闭
* HIDE_ON_CLOSE 点击关闭时,隐藏窗口
* DO_NOTHING_ON_CLOSE 点击关闭时,不做任何反应
* */
JLabel jLabel = new JLabel("JFrame窗口");
jf.add(jLabel);
//设置水平对齐
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器,容器来设置背景颜色
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.yellow);
}
public static void main(String[] args) {
new MyJFrame().init();
}
}
JFrame里有容器的一个概念,所有的东西都是放在一个又一个容器中的,所以没有办法直接对对象进行背景颜色设置,需要实现一个容器对象,再来对该容器进行背景颜色设置。
效果图:
2.2、JDiaLog弹窗
JDiaLog:默认就有关闭事件
public class DialogDemo extends JFrame{
public DialogDemo() {
this.setBounds(100,100,400,400);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton jButton = new JButton("按钮");
//容器
Container container = this.getContentPane();
container.setLayout(null); //绝对布局
container.add(jButton);
//绝对布局需要设置按钮相对窗口坐标
jButton.setBounds(30,30,100,50);
//为按钮添加监听事件
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyJDialog();
}
});
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗窗口
class MyJDialog extends JDialog{
public MyJDialog() {
this.setBounds(150,150,200,200);
this.setVisible(true);
//弹窗默认有关闭事件,所以不需要再加关闭事件
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//弹窗容器
Container container = this.getContentPane();
//如果设置布局为null,则默认为绝对布局,需要再设置bounds坐标,才能使元素显示出来
//container.setLayout(null);
container.add(new JLabel("这是一个弹窗"));
}
}
效果图:
2.3Icon标签
- 标签
new JLable(); //最常规标签写法
-
图标
public class IconDemo02 extends JFrame implements Icon { private int width; private int height; public IconDemo02(){} public IconDemo02(int width,int height) { this.width = width; this.height = height; } public void init(){ /* * 重写的画的那些方法,他们都是系统默认调用的 * Graphics定义了给画笔是在虚拟界面上已经把东西画好了 * 所以可以直接在标签里把他添加上去 * */ IconDemo02 iconDemo02 = new IconDemo02(10,10); //图标放在标签上,也可以放在按钮上 //标签标题,图标,标签的定位 JLabel label = new JLabel("icontext",iconDemo02,SwingConstants.CENTER); Container container = iconDemo02.getContentPane(); container.add(label); iconDemo02.setVisible(true); iconDemo02.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); iconDemo02.setBounds(200,200,400,400); } public static void main(String[] args) { new IconDemo02().init(); } @Override public void paintIcon(Component c, Graphics g, int x, int y) { //画图标,参数x相对图标左上角的 X 坐标,y相对图标左上角的 Y 坐标 g.fillOval(x,y,width,height); } @Override public int getIconWidth() { //图标的宽 return this.width; } @Override public int getIconHeight() { //图标的高 return this.width; } }
- 图片
//添加图片
public class ICONDemo extends JFrame {
public static void main(String[] args) {
new ICONDemo();
}
public ICONDemo() {
JLabel label = new JLabel();
//引入同级目录下的图片,获取图片地址
URL url = ICONDemo.class.getResource("BG.jpg");
//ImageIcon把图片的地址转为了对象,,方便label添加
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon); //将图片加到图标上
label.setHorizontalAlignment(SwingConstants.CENTER); //设置居中
Container container = getContentPane();
container.add(label);
setBounds(100,100,200,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
2.4面板
JPanel
//JPanel面板
public class JPanelDemo extends JFrame {
public static void main(String[] args) {
new JPanelDemo();
}
public JPanelDemo() {
Container container =this.getContentPane();
container.setLayout(new GridLayout(1,3,10,10));
JPanel panel1 = new JPanel(new GridLayout(1,2));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(1,2));
InterJPanel(panel1);
InterJPanel(panel2);
InterJPanel(panel3);
container.add(panel1);
container.add(panel2);
container.add(panel3);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,400,400);
}
//for循环添加按钮
public void InterJPanel(JPanel jPanel){
for (int i=1;i<=2;i++){
jPanel.add(new JButton(String.valueOf(i)));
}
}
}
JScrollPane面板
可以添加下拉框
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container container = this.getContentPane();
//文本域,设置每行字节数和总行数
JTextArea textArea = new JTextArea(10,20);
textArea.setText("这是JTextArea文本域");
//JScroll面板,带有滚动条,将文本域添加到里面
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(300,300,400,400);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
2.5按钮
//图片按钮
public class JButtonDemo extends JFrame {
public JButtonDemo() {
Container container = this.getContentPane();
container.setLayout(null);
//将图片变为图标
URL url = JButtonDemo.class.getResource("BG.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JButton jButton = new JButton("这是按钮",imageIcon);
jButton.setToolTipText("按钮点击"); //设置按钮提示信息
jButton.setBounds(10,10,100,50); //绝对布局下定义按钮位置
container.add(jButton);
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
-
单选按钮
public class JButtonDemo02 extends JFrame { public JButtonDemo02() { Container container = getContentPane(); //单选框 JRadioButton radioButton1 = new JRadioButton("radioButton1"); JRadioButton radioButton2 = new JRadioButton("radioButton2"); JRadioButton radioButton3 = new JRadioButton("radioButton3"); //把单选框放入同一个组中,每个组只能选择一个,达到单选效果 ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(radioButton1); buttonGroup.add(radioButton2); buttonGroup.add(radioButton3); container.add(radioButton1,BorderLayout.NORTH); container.add(radioButton2,BorderLayout.CENTER); container.add(radioButton3,BorderLayout.SOUTH); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,100,400,400); } public static void main(String[] args) { new JButtonDemo02(); } }
如果不放在一个组里,则会:
都能选择
-
复选按钮
public class JButtonDemo03 extends JFrame{ public JButtonDemo03() { Container container = getContentPane(); //多选框 JCheckBox checkBox01 = new JCheckBox("checkBox01"); JCheckBox checkBox02 = new JCheckBox("checkBox02"); container.add(checkBox01,BorderLayout.NORTH); container.add(checkBox02,BorderLayout.SOUTH); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,100,200,200); } public static void main(String[] args) { new JButtonDemo03(); } }
2.6列表
- 下拉框JComoBox
public class ComboBoxDemo extends JFrame {
public ComboBoxDemo() {
Container container = this.getContentPane();
JPanel jPanel = new JPanel();
//下拉框
JComboBox comboBox = new JComboBox();
//下拉框内的内容
comboBox.addItem("已下架");
comboBox.addItem("正在热播");
comboBox.addItem("已上映");
//将下拉框放在面板上,再把面板放在容器上
jPanel.setBounds(20,20,50,50);
jPanel.add(comboBox);
comboBox.setBounds(10,10,20,5);
container.add(jPanel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ComboBoxDemo();
}
}
-
列表框JList
public class ComboBoxDemo02 extends JFrame{ public ComboBoxDemo02() { Container container = this.getContentPane(); JPanel jPanel = new JPanel(); //定义数组,来添加到列表中 String[] strings ={"123","zhangsan","abc123"}; //列表中放入需要内容 JList jList = new JList(strings); container.add(jList); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setBounds(100,100,200,200); } public static void main(String[] args) { new ComboBoxDemo02(); } }
2.7文本框
包括文本框,密码框,文本域三种
public class TextTestDemo extends JFrame{
public TextTestDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(3,1));
//文本框
JTextField textField = new JTextField("这是文本框");
//密码框,密码框默认会隐藏输入字符,不需要再设置替代字符
JPasswordField passwordField = new JPasswordField();
//文本域
JTextArea textArea = new JTextArea();
container.add(textField);
container.add(passwordField);
container.add(textArea);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TextTestDemo();
}
}