java课题–day5学习JFRAME
第一个弹窗
import javax.swing.*;
public class JfDemo {
public static void main(String[] args) {
JFrame jf=new JFrame("可爱的弹窗");
jf.setBounds(500,300,900,700);
jf.setIconImage(new ImageIcon("src/W0.png").getImage());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
弹窗居中–去除任务栏
import javax.swing.*;
import java.awt.*;
public class JfDemo3 {
private static final int WIDTH=500;
private static final int HEIGHT=400;
public static void main(String[] args) {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
带任务栏居中
import javax.swing.*;
import java.awt.*;
public class JfDemo2 {
private static final int WIDTH=500;
private static final int HEIGHT=400;
public static void main(String[] args) {
JFrame jFrame=new JFrame("窗口居中");
Toolkit toolkit=Toolkit.getDefaultToolkit();
Dimension screenSize=toolkit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
按钮–流式布局
import javax.swing.*;
import java.awt.*;
public class JfButtonDemo {
private static final int WIDTH=500;
private static final int HEIGHT=400;
public static void main(String[] args) {
JFrame jframe = JfButtonDemo.init();
jframe.setLayout(new FlowLayout(FlowLayout.LEFT));
for (int i = 0; i <100 ; i++) {
JButton jButton1 = new JButton("button"+i);
jframe.add(jButton1);
}
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}
按钮–边界布局
import javax.swing.*;
import java.awt.*;
public class JfBouttonDemo1 {
private static final int WIDTH=1000;
private static final int HEIGHT=800;
public static void main(String[] args) {
JFrame jframe = JfButtonDemo.init();
jframe.setLayout(new BorderLayout(10,10));
JButton jButton1 = new JButton("button1");
JButton jButton2 = new JButton("button2");
JButton jButton3 = new JButton("button3");
JButton jButton4 = new JButton("button4");
JButton jButton5 = new JButton("button5");
jframe.add(jButton1,BorderLayout.NORTH);
jframe.add(jButton2,BorderLayout.SOUTH);
jframe.add(jButton3,BorderLayout.EAST);
jframe.add(jButton4,BorderLayout.WEST);
jframe.add(jButton5,BorderLayout.CENTER);
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}
按钮–网格布局
import javax.swing.*;
import java.awt.*;
public class JfButtonDemo2 {
private static final int WIDTH=1000;
private static final int HEIGHT=800;
public static void main(String[] args) {
JFrame jframe = JfButtonDemo.init();
jframe.setLayout(new GridLayout(5,5));
for (int i = 0; i <25 ; i++) {
JButton jButton1 = new JButton("button"+i);
jframe.add(jButton1);
}
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}
面板
import javax.swing.*;
import java.awt.*;
public class JPanelDemo {
private static final int WIDTH=500;
private static final int HEIGHT=400;
public static void main(String[] args) {
JPanel jPanel=new JPanel(new GridLayout(5,1));
JPanel jPane2=new JPanel(new BorderLayout());
JFrame jframe = JfButtonDemo.init();
jframe.setLayout(new BorderLayout(10,10));
jframe.add(jPanel,BorderLayout.WEST);
JButton jb=new JButton("center");
jPane2.add(jb);
jframe.add(jPane2,BorderLayout.CENTER);
for (int i = 0; i <5 ; i++) {
JButton jButton1 = new JButton("button"+i);
jPanel.add(jButton1);
}
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}
文本输入框
import javax.swing.*;
import java.awt.*;
public class JLableDemo {
private static final int WIDTH=1000;
private static final int HEIGHT=800;
public static void main(String[] args) {
JFrame jframe = init();
jframe.setLayout(new GridLayout(2,1));
JPanel jPanel=new JPanel();
JLabel lable=new JLabel("请输入用户名:",new ImageIcon("src/W0.png"),JLabel.CENTER);
JTextField jtf=new JTextField(40);
jPanel.add(lable);
jPanel.add(jtf);
JPanel jPane2=new JPanel();
JLabel jlable2=new JLabel("请输入密码:",new ImageIcon("src/W30.png"),JLabel.CENTER);
JTextField jtf2=new JTextField(40);
jPanel.add(jlable2);
jPanel.add(jtf2);
jframe.add(jPanel);
jframe.add(jPane2);
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}
选择框
import javax.swing.*;
import java.awt.*;
public class JComboBoxDemo {
private static final int WIDTH=1000;
private static final int HEIGHT=800;
public static void main(String[] args) {
JFrame jframe = init();
jframe.setLayout(new GridLayout(5,1));
JPanel jPanel=new JPanel();
JLabel jlb1=new JLabel("请输入您的学习语言:");
JComboBox box=new JComboBox();
JCheckBox jcb1=new JCheckBox("足球");
JCheckBox jcb2=new JCheckBox("篮球");
JCheckBox jcb3=new JCheckBox("乒乓球");
box.addItem("Java");
box.addItem("c");
box.addItem("C++");
box.addItem("sql");
jPanel.add(jlb1);
jPanel.add(box);
jframe.add(jPanel);
JPanel jPanel2 = new JPanel();
JLabel jlb2=new JLabel("请输入你的爱好:");
jframe.add(jPanel2);
jPanel2.add(jlb2);
jPanel2.add(jcb1);
jPanel2.add(jcb2);
jPanel2.add(jcb3);
JPanel jpanel3 =new JPanel();
JLabel jlb3=new JLabel("您的性别:");
JRadioButton jrb1=new JRadioButton("男");
JRadioButton jrb2=new JRadioButton("女");
JRadioButton jrb3=new JRadioButton("中性");
JRadioButton jrb4=new JRadioButton("保密");
ButtonGroup bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
bg.add(jrb3);
bg.add(jrb4);
jpanel3.add(jlb3);
jpanel3.add(jrb1);
jpanel3.add(jrb2);
jpanel3.add(jrb3);
jpanel3.add(jrb4);
jframe.add(jpanel3);
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}
文件选择
import com.sun.org.apache.bcel.internal.generic.NEW;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JFileChooserDemo {
private static final int WIDTH=1000;
private static final int HEIGHT=800;
public static void main(String[] args) {
JFrame jframe = init();
JPanel jPanel=new JPanel();
JLabel jlb1=new JLabel("请选择头像:");
JButton jb=new JButton("选择头像");
JTextField jtf=new JTextField(30);
jPanel.add(jlb1);
jPanel.add(jtf);
jPanel.add(jb);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jfilehelpers = new JFileChooser();
int returnVal = jfilehelpers.showOpenDialog(null);
if (returnVal==JFileChooser.APPROVE_OPTION){
jtf.setText(jfilehelpers.getSelectedFile().getAbsolutePath());
}
}
});
jframe.add(jPanel);
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}
滚动条
import javax.swing.*;
import java.awt.*;
public class JScrollPaneDemo {
private static final int WIDTH=1000;
private static final int HEIGHT=800;
public static void main(String[] args) {
JFrame jframe = init();
JPanel jPanel=new JPanel();
jPanel.setLayout(new GridLayout(500,1));
for (int i = 0; i <500 ; i++) {
JButton jButton1 = new JButton("button"+i);
jPanel.add(jButton1);
}
JScrollPane jsp = new JScrollPane(jPanel);
jframe.add(jsp);
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}
消息弹窗
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JComboBoxDemo {
private static final int WIDTH=1000;
private static final int HEIGHT=800;
public static void main(String[] args) {
JFrame jframe = init();
jframe.setLayout(new GridLayout(5,1));
JPanel jPanel=new JPanel();
JLabel jlb1=new JLabel("请输入您的学习语言:");
JComboBox box=new JComboBox();
JCheckBox jcb1=new JCheckBox("足球");
JCheckBox jcb2=new JCheckBox("篮球");
JCheckBox jcb3=new JCheckBox("乒乓球");
box.addItem("Java");
box.addItem("c");
box.addItem("C++");
box.addItem("sql");
jPanel.add(jlb1);
jPanel.add(box);
jframe.add(jPanel);
JPanel jPanel2 = new JPanel();
JLabel jlb2=new JLabel("请输入你的爱好:");
jframe.add(jPanel2);
jPanel2.add(jlb2);
jPanel2.add(jcb1);
jPanel2.add(jcb2);
jPanel2.add(jcb3);
JPanel jpanel3 =new JPanel();
JLabel jlb3=new JLabel("您的性别:");
JRadioButton jrb1=new JRadioButton("男");
JRadioButton jrb2=new JRadioButton("女");
JRadioButton jrb3=new JRadioButton("中性");
JRadioButton jrb4=new JRadioButton("保密");
ButtonGroup bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
bg.add(jrb3);
bg.add(jrb4);
jpanel3.add(jlb3);
jpanel3.add(jrb1);
jpanel3.add(jrb2);
jpanel3.add(jrb3);
jpanel3.add(jrb4);
jframe.add(jpanel3);
JPanel jPanel4=new JPanel();
JButton jButton4=new JButton("---注册---");
jButton4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"注册成功");
int i=JOptionPane.showConfirmDialog(null,"确认删除吗");
System.out.println(i);
JOptionPane.showInputDialog(null);
}
});
jPanel4.add(jButton4);
jframe.add(jPanel4);
jframe.setVisible(true);
}
public static JFrame init() {
JFrame jFrame=new JFrame("窗口居中");
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle=ge.getMaximumWindowBounds();
int width=rectangle.width;
int height=rectangle.height;
jFrame.setBounds((width-WIDTH)/2,(height-HEIGHT)/2,WIDTH,HEIGHT);
jFrame.setIconImage(new ImageIcon("src/W0.png").getImage());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return jFrame;
}
}