swing
swing值JFrame窗体
awt中的frame则不需要,可直接都抽口进行相关操作。
Jframe 中布局格式必须放在container中进行实现。例面板、滚动条 颜色的设置。都是通过container来实现的
package Jframe;
import javax.swing.*;
import java.awt.*;
public class JframeDemo01 {
public static void main(String[] args) {
MyJframeDemo01 myJframeDemo01=new MyJframeDemo01() ;
}
}
class MyJframeDemo01 extends JFrame {
public MyJframeDemo01() {
JFrame jFrame=new JFrame("按下空格开始");
jFrame.setBounds(20,20,400,400);
//设置窗口上面所显示的内容
JLabel jLabel=new JLabel("请按下空格开始游戏");
//将要显示的内容天添加到窗口中
jFrame.add(jLabel);
//Jframe中自定义的Windows的关闭方法
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//在Jframe中,颜色的设置需要通过Container容器来实现
Container container=jFrame.getContentPane();
container.setBackground(Color.CYAN);
//Jframe中的文本居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jFrame.setVisible(true);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X3fxxODb-1618403310198)(E:\csdn image\QQ截图20210411165313.png)]
弹窗事件
package Jframe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JframeDemo02 {
public static void main(String[] args) {
MyJframeDemo02 myJframeDemo02=new MyJframeDemo02();
}
}
class MyJframeDemo02 extends JFrame{
// private final Object ActionListener;
public MyJframeDemo02() {
JFrame jFrame=new JFrame("第一个弹窗");
Container container=jFrame.getContentPane();
container.setBackground(Color.CYAN);
jFrame.setBounds(50,50,400,400);
JButton jButton=new JButton("点击一下跳出弹窗");
jFrame.add(jButton);
jFrame.setVisible(true);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MyPopup myPopup=new MyPopup();//直接调用MyPopup
}
});
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
//设置弹窗,
class MyPopup extends JDialog{
public MyPopup() {
JDialog jDialog=new JDialog();
jDialog.setSize(200,200);
jDialog.setVisible(true);
JLabel jLabel=new JLabel("狂神老师带你学习java");
jDialog.add(jLabel);
// jDialog.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V0RH0LjS-1618403310199)(E:\csdn image\tanchuangshijian2.png)]
标签
图标 ICON
图片icom imageicon可以接受图片
package Jframe;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JframeDemo03 extends JFrame {
public JframeDemo03() {
JFrame jFrame=new JFrame();
JLabel jLabel=new JLabel("");
// System.out.println(JframeDemo03.class.getResource("ju1.jpg"));
// URL url = JframeDemo03.class.getResource("Jframe/ju1.jpg");
ImageIcon imageIcon=new ImageIcon("D:\\IDEA\\Gui1\\src\\Jframe\\ju1.jpg");
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container=jFrame.getContentPane();
container.add(jLabel);
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
// imageIcon.
jFrame.setBounds(50,50,400,400);
jLabel.setSize(11,11);
}
public static void main(String[] args) {
new JframeDemo03();
}
}
面板
Jpanle
package JPanle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JpanleDemo01 {
public static void main(String[] args) {
new MyJpanleDemo01();
}
}
/**
* 在swing Jframe中冲口总存在一个container容器,面板进行布局,之后面板添加到容器container中。
*/
class MyJpanleDemo01 extends JFrame{
public MyJpanleDemo01() {
JFrame jFrame=new JFrame();
jFrame.setSize(500,500);
//通过容器来实现其中的布局
Container container=jFrame.getContentPane();
JPanel jPanel=new JPanel();
jPanel.setLayout(new GridLayout(2,3));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
container.add(jPanel);
jFrame.setVisible(true);
jFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
JScorllPane面板(会根据内容的多少来判断是否有下拉框)
存在下拉条的textArea
普通的panel面板
package JscrollPanel;
import javax.swing.*;
import java.awt.*;
public class JScrollPanelDemo01 {
public static void main(String[] args) {
new MyJScrollPanelDFemo01();
}
}
class MyJScrollPanelDFemo01 extends JFrame {
public MyJScrollPanelDFemo01() {
JFrame jFrame=new JFrame();
Container container=jFrame.getContentPane();
// TextArea textArea=new TextArea(20,30);
TextArea TextArea=new TextArea(400,50);
TextArea.setText("创建textArea中自动显示的文字");
// JScrollPane jScrollPane=new JScrollPane(TextArea);
container.add(TextArea);
jFrame.setVisible(true);
jFrame.setSize(500,400);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
JScrollpane面板
package JscrollPanel;
import javax.swing.*;
import java.awt.*;
public class JScrollPanelDemo01 {
public static void main(String[] args) {
new MyJScrollPanelDFemo01();
}
}
class MyJScrollPanelDFemo01 extends JFrame {
public MyJScrollPanelDFemo01() {
Container container=this.getContentPane();
JTextArea jTextArea=new JTextArea(20,30);
jTextArea.setText("JScrollpane");
JScrollPane jScrollPane=new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setSize(400,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
按钮
单选按钮
package Button;
import javafx.scene.Group;
import javafx.scene.control.RadioButton;
import javax.swing.*;
import java.awt.*;
public class ButtonDemo01 {
public static void main(String[] args) {
new MyButtonDemo01();
}
}
class MyButtonDemo01 extends Frame{
public MyButtonDemo01() {
JFrame jFrame=new JFrame();
JRadioButton jRadioButton=new JRadioButton("男");
JRadioButton jRadioButton1=new JRadioButton("女");
//单选按钮的核心是当其中的一个按钮被选中之后,另一个按钮不能进行选择。
ButtonGroup buttonGroup=new ButtonGroup();
Container container=jFrame.getContentPane();
container.setLayout(new FlowLayout());
//将两个按钮放在同一个组中
buttonGroup.add(jRadioButton);
buttonGroup.add(jRadioButton1);
container.add(jRadioButton);
container.add(jRadioButton1);
jFrame.setVisible(true);
jFrame.setSize(400,300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
复选按钮
package Button;
import javafx.scene.Group;
import javafx.scene.control.RadioButton;
import javax.swing.*;
import java.awt.*;
public class ButtonDemo01 {
public static void main(String[] args) {
new MyButtonDemo01();
}
}
class MyButtonDemo01 extends Frame{
public MyButtonDemo01() {
JFrame jFrame=new JFrame();
Container container=jFrame.getContentPane();
JCheckBox jCheckBox=new JCheckBox("音乐");
JCheckBox jCheckBox1=new JCheckBox("看电影");
JCheckBox jCheckBox2=new JCheckBox("运动");
container.setLayout(new FlowLayout());
container.add(jCheckBox);
container.add(jCheckBox1);
container.add(jCheckBox2);
jFrame.setVisible(true);
jFrame.setSize(400,300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
列表
下拉框(JComboBox)
package List;
import javax.swing.*;
import java.awt.*;
public class ListDemo01 {
public static void main(String[] args) {
new MyListDemo01();
}
}
class MyListDemo01 extends Frame {
public MyListDemo01() {
JFrame jFrame=new JFrame();
Container container=jFrame.getContentPane();
JComboBox jComboBox=new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("即将上映");
jComboBox.addItem("正在热映");
jComboBox.addItem("下映");
container.add(jComboBox);
jFrame.setVisible(true);
jFrame.setSize(400,300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
列表框(一般用来存放一些用户的信息)(JList)
package List;
import javax.swing.*;
import java.awt.*;
public class ListDemo02 {
public static void main(String[] args) {
new MyListDemo02();
}
}
class MyListDemo02 extends Frame {
public MyListDemo02() {
JFrame jFrame=new JFrame();
Container container=jFrame.getContentPane();
String[] contain={"1","2","3"};
JList jList=new JList(contain);
container.add(jList);
jFrame.setVisible(true);
jFrame.setSize(400,300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
文本框
-
JTextArea(文本域可以进行换行)
-
JTextField(文本域但是不能进行换行)
JPasswordField(密码框文本域)
package Text; import javax.swing.*; import java.awt.*; public class TextDemo01 { public static void main(String[] args) { new MyTextDeno01(); } } //JPasswordField默认的显示格式是居中显示。 class MyTextDeno01 extends Frame { public MyTextDeno01() { JFrame jFrame=new JFrame(); Container container=jFrame.getContentPane(); JPasswordField jPasswordField=new JPasswordField(); //表示输入的内容是由*来显示 jPasswordField.setEchoChar('*'); container.add(jPasswordField); jFrame.setVisible(true); jFrame.setSize(400,300); jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }