GUI
Java的图像界面框架。
一.继承(JFrame)
import javax.swing.*;
import java.awt.*;
import java.util.Random;public class GuiDemo extends JFrame {/** public GuiDemo() throws HeadlessException{this.setTitle("我的窗体");this.setBounds(300,250,500,300);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);Random random = new Random(System.currentTimeMillis());// this.getContentPane().setBackground(Color.CYAN);this.getContentPane().setBackground(new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));}*/public static void main(String[] args) throws InterruptedException {GuiDemo gui = new GuiDemo();gui.setTitle("");gui.setBounds(300,250,500,300);gui.setVisible(true);// Color[] colors = {Color.BLACK,Color.BLUE};Random ran = new Random(System.currentTimeMillis());for (;;){gui.getContentPane().setBackground(new Color(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256)));Thread.sleep(100);}}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;public class GuiDemo02 extends JFrame {public GuiDemo02() throws HeadlessException{this.setTitle("W");this.setBounds(300,250,500,300);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.getContentPane().setLayout(null);JButton button = new JButton("check!");JPasswordField pw = new JPasswordField();button.setBounds(300,200,100,50);this.getContentPane().add(button);this.getContentPane().add(pw);//组件(控件)button.addActionListener(new ActionListener() {//事件(动作)@Overridepublic void actionPerformed(ActionEvent e) {//响应// System.out.println("^-^");Random ran = new Random(System.currentTimeMillis());getContentPane().setBackground(new Color(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256)));System.out.println(new String(pw.getPassword()));System.out.println("x:" + pw.getX());int x = pw.getX()+5;pw.setBounds(x,100,200,150);// setVisible(false);}});this.setVisible(true);}public static void main(String[] args) {new GuiDemo02();}
}
二.GUI插件
idea settings plugin
/** Created by JFormDesigner on Sat Sep 28 17:25:21 CST 2024*/package com.ffyc.form;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/*** @author 武鑫鑫*/
public class Test extends JFrame {public Test() {initComponents();}private void pw(ActionEvent e) {// TODO add your code hereSystem.out.println("hhh");String s = new String(pw.getPassword());sp.setText(s);}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:offbutton1 = new JButton();pw = new JPasswordField();sp = new JLabel();//======== this ========Container contentPane = getContentPane();contentPane.setLayout(null);//---- button1 ----button1.setText("check");contentPane.add(button1);button1.setBounds(new Rectangle(new Point(170, 180), button1.getPreferredSize()));//---- pw ----pw.addActionListener(e -> pw(e));contentPane.add(pw);pw.setBounds(410, 110, 200, 90);//---- sp ----sp.setText("text");contentPane.add(sp);sp.setBounds(205, 75, 155, 90);contentPane.setPreferredSize(new Dimension(1235, 840));pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on}public static void main(String[] args) {new Test().setVisible(true);}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:offprivate JButton button1;private JPasswordField pw;private JLabel sp;// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
}