JAVA GUI学习记录
1.基本界面
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUI
{
public static void main(String[] args)
{
JFrame basic_frame= new JFrame();//frame窗口
Panel basic_panel=new Panel();//panel面板
basic_frame.add(basic_panel);
Color c_background=new Color(255,255,205);//设置颜色
basic_panel.setBackground(c_background);//将颜色设置为面板颜色(背景颜色)
Color c_button0=new Color(0x66ccff);
basic_panel.setLayout(null);//布局类型
basic_frame.setBounds(256,256,512,288);//窗口相关设置
basic_frame.setVisible(true);
basic_frame.validate();
Button button0=new Button("nanijibamono");//button按键
button0.setBounds(208,96,90,25);//设置按键位置以及大小
button0.setBackground(c_button0);//设置按键颜色
button0.addActionListener(button0_behavour);//按键事件监听
basic_panel.add(button0);//在面板中加入按键
Choice choice0=new Choice();//设置下拉选项
choice0.setBounds(208,144,60,20);
choice0.add("wly");
choice0.add("wly~");
basic_panel.add(choice0);//添加按钮
basic_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭
}
}
2.事件监听
public static class Monitor implements ActionListener//按键按下,如果choice为0则打开新窗口
{
public void actionPerformed(ActionEvent e)
{
JFrame frame0=new JFrame();
Panel panel0=new Panel();
frame0.add(panel0);
JLabel label00=new JLabel("什么X东西",JLabel.CENTER);
frame0.add(label00);
frame0.setBounds(256,256,128,64);
if(tempnum==0)
{
frame0.setVisible(true);
}
}
}
choice0.addItemListener(new ItemListener()//选项改变时得到选项的值
{
public void itemStateChanged(ItemEvent e)
{
tempnum=choice0.getSelectedIndex();
}
});
效果: