GUI学习
作者:互联网
GUI编程
包含以下组件
窗口
弹窗
面板
文本框
列表框
按钮
图片
监听事件
鼠标
键盘事件
破解工具
AWT
存在于 java.awt 包
(图源自狂神)
Frame
Frame是顶级窗口
Frame frame = new Frame("第一个Java窗口");
frame.setVisible(true);
frame.setSize(400, 400);
frame.setBackground(new Color(222, 167, 167));
frame.setLocation(200, 200);
frame.setResizable(false);
Panel
Panel必须添加到某个容器中
frame.setLayout(null);
Panel panel = new Panel();
panel.setBounds(50, 60, 300, 300);
panel.setBackground(new Color(56, 91, 177));
frame.add(panel);
// 适配器模式,利用实现WindowListener接口的WindowAdapter类,只重写需要重写的函数
// 成功通过单击右上角的X完成窗口的关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
布局管理器
// 首先新建3个按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
-
流式布局 FlowLayout
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(button1);
frame.add(button2);
frame.add(button3);
-
东西南北中 BorderLayout
frame.setLayout(new BorderLayout());
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.WEST);
frame.add(button3, BorderLayout.EAST);
-
表格布局 GridLayout
frame.setLayout(new GridLayout(3, 1));
frame.add(button1);
frame.add(button2);
frame.add(button3);
// 自动布局
frame.pack();
事件监听——多种监听
实现 ActionListener 接口,可以多个按钮通过一个事件进行监听,如下为文本输入框监听:
class MyFrame2 extends Frame{
public MyFrame2(){
TextField textField = new TextField();
this.add(textField);
// 监听文本框
MyActionListener myActionListener = new MyActionListener();
textField.addActionListener(myActionListener);
// 设置文本框显示的字符
textField.setEchoChar('*');
this.setVisible(true);
this.pack();
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField text = (TextField) e.getSource();
System.out.println(text.getText());
text.setText("");
}
}
还有 WindowListener、MouseListener、KeyListener,由编写内部类改为匿名内部类
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("您已退出!");
System.exit(0);
}
});
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
points.add(new Point(e.getX(), e.getY()));
repaint();
}
});
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
System.out.println("你按下了上键!其keyCode为" + keyCode + "。");
}
}
});
画笔
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200, 200, 600, 500);
setVisible(true);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.green);
g.drawOval(100, 100, 100, 100);
g.setColor(Color.pink);
g.fillOval(100, 100, 100, 100);
}
}
Swing
JFrame
通过 getContentPane() 获取 frame 的 container ,并在其中添加 各种组件
class MyJframe extends JFrame{
public void init() {
this.setBounds(100,100,300,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 标签居中显示
JLabel jLabel = new JLabel("你好,第一个文本框", SwingConstants.CENTER);
this.add(jLabel);
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.orange);
}
}
JDialog 弹窗
class MyDialog extends JDialog {
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,500,500);
// 不需要设置关闭方式,dialog默认可以关闭
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Icon 标签
普通标签、图片标签、图像标签
URL url = ImageIconDemo.class.getResource("tx.gpg");
ImageIcon imageIcon = new ImageIcon(url);
JPanel 面板、JTextArea 文本域、JScrollPane 滚动面板
按钮
图片按钮
public class JButtonDemo extends JFrame {
public JButtonDemo() {
Container contentPane = this.getContentPane();
URL url = JButtonDemo.class.getResource("jt.png");
ImageIcon icon = new ImageIcon(url);
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
contentPane.add(button);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500, 300);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
单选按钮
// 单选框,要分组,分组后一个组中只能选择一个
JRadioButton radio1 = new JRadioButton("Radio");
JRadioButton radio2 = new JRadioButton("Radio");
JRadioButton radio3 = new JRadioButton("Radio");
ButtonGroup group = new ButtonGroup();
group.add(radio1);
group.add(radio2);
group.add(radio3);
contentPane.add(radio1, BorderLayout.NORTH);
contentPane.add(radio2, BorderLayout.CENTER);
contentPane.add(radio3, BorderLayout.SOUTH);
复选按钮
// 复选框
JCheckBox checkBox1 = new JCheckBox("CheckBox01");
JCheckBox checkBox2 = new JCheckBox("CheckBox02");
contentPane.add(checkBox1, BorderLayout.NORTH);
contentPane.add(checkBox2, BorderLayout.SOUTH);
列表
下拉框
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
contentPane.add(status);
列表框
// 展示信息,动态扩容
String[] contents = {"1", "33", "666"};
JList jList = new JList(contents);
contentPane.add(jList);
文本框
文本框
JTextField jTextField1 = new JTextField("hello");
JTextField jTextField2 = new JTextField("world", 20);
contentPane.add(jTextField1, BorderLayout.NORTH);
contentPane.add(jTextField2, BorderLayout.SOUTH);
密码框
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
contentPane.add(passwordField);
文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("第一个文本域");
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(scrollPane);
标签:GUI,add,public,学习,contentPane,new,100,frame 来源: https://www.cnblogs.com/hellowzf/p/15942906.html