Java基础
GUI编程
核心技术:Swing、AWT
现在GUI并不流行 因为其界面不美观、需要依赖jre环境
Swing
public class Demo1 {
//init();初始化
public void init() {
//最大窗口
JFrame jf = new JFrame("jframe窗口");
jf.setVisible(true);
jf.setSize(500,600);
//设置文字jlable
JLabel jLabel = new JLabel("欢迎来到java世界");
jf.add(jLabel);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//获取一个容器
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.cyan);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo1().init();
}
}
弹窗
public class Demo2 {
public void init(){
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setSize(600,800);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container cp = jf.getContentPane();
cp.setLayout(null);//绝对布局
JButton jBut = new JButton("点我");
jBut.setBounds(20,20,200,100);
jBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
cp.add(jBut);//将按钮放进容器中
}
public static void main(String[] args) {
new Demo2().init();
}
}
//弹窗窗口
class MyDialog extends JDialog{
public MyDialog() {
setVisible(true);
setBounds(100,100,200,200);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel label = new JLabel("我来带你飞");
label.setBounds(50,50,100,100);
contentPane.add(label);
}
}
Icon 图标
public class Demo3 extends JFrame implements Icon {
private int width;
private int height;
public Demo3() {
}
public Demo3(int width, int height) {
this.width = width;
this.height = height;
}
public void init() {
Demo3 d = new Demo3(15,15);
JLabel icon = new JLabel("icon", d, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(icon);
setVisible(true);
setSize(200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void setIcon() {
JLabel icon2 = new JLabel("image",SwingConstants.CENTER);
URL url = this.getClass().getResource("ico.png");
ImageIcon imageIcon = new ImageIcon(url);
icon2.setIcon(imageIcon);
Container contentPane = getContentPane();
contentPane.add(icon2);
setVisible(true);
setSize(200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// new Demo3().init();
new Demo3().setIcon();
}
@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 class Demo4 extends JFrame {
public Demo4() {
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10));
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,3));
contentPane.add(panel);
panel.add(new JButton("1"));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
contentPane.add(panel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300,400);
}
public static void main(String[] args) {
new Demo4();
}
}
public class Demo5 extends JFrame {
public Demo5(){
Container contentPane = getContentPane();
JTextArea jTextArea = new JTextArea(20, 50);
Scanner scanner = new Scanner(System.in);
String str= scanner.toString();
jTextArea.setText(str);
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500,400);
}
public static void main(String[] args) {
new Demo5();
}
}
单选、多选
public class Demo6 extends JFrame {
public Demo6(){
// Container container = getContentPane();
//
// URL url = Demo6.class.getResource("ico.png");
// ImageIcon icon = new ImageIcon(url);
//
// JButton jButton = new JButton();
// jButton.setSize(55,55);
// jButton.setIcon(icon);
// jButton.setToolTipText("我是提示");
//
// container.add(jButton);
//
// setVisible(true);
// setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// setSize(400,500);
}
public void init(){
Container container = getContentPane();
URL url = Demo6.class.getResource("ico.png");
ImageIcon icon = new ImageIcon(url);
//单选框
JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
//分组 将按钮放入组内 一个组只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton1);
group.add(jRadioButton2);
group.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.SOUTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.NORTH);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,500);
}
public void init2(){
Container container = getContentPane();
URL url = Demo6.class.getResource("ico.png");
ImageIcon icon = new ImageIcon(url);
//多选
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2 = new JCheckBox("checkBox1");
JCheckBox checkBox3 = new JCheckBox("checkBox1");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox2,BorderLayout.CENTER);
container.add(checkBox3,BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,500);
}
public static void main(String[] args) {
new Demo6().init2();
}
}
下拉、列表
public class Demo7 extends JFrame {
public Demo7() {
}
public void init() {
Container container = getContentPane();
//下拉框
JComboBox<Object> box = new JComboBox<>();
box.addItem("黑");
box.addItem("白");
box.addItem("黄");
box.addItem("红");
container.add(box);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,500);
}
public void init2() {
Container container = getContentPane();
String[] strings = {"1","2","3","4"}; //固定添加
//列表框
Vector objs = new Vector();
objs.add(1);
objs.add("2");
objs.add("DNF");
objs.add(234);
JList jList = new JList(objs);
container.add(jList);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,500);
}
public static void main(String[] args) {
new Demo7().init2();
}
}
文本框
public class Demo8 extends JFrame {
public void init1(){
Container container = getContentPane();
//文本框
JTextField textField1 = new JTextField("nihao");
TextField tf = new TextField();
JTextField textField2 = new JTextField("java");
tf.setEchoChar('*');//伪密码框效果
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');//密码显示样式更改
container.add(textField1,BorderLayout.SOUTH);
container.add(textField2,BorderLayout.NORTH);
container.add(passwordField,BorderLayout.CENTER);
container.add(tf,BorderLayout.WEST);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,500);
}
public static void main(String[] args) {
new Demo8().init1();
}
}
贪吃蛇小游戏
/**
* 游戏面板
*/
public class GamePanel extends JPanel implements KeyListener, ActionListener {
//定义
标签:24,java,int,void,day23,add,container,new,public
From: https://www.cnblogs.com/onlyxue/p/16848139.html