第12次抽考(GUI)

1.

package week4;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;

public class Work2 extends JFrame implements ActionListener{
    JButton certain;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Work2();

    }
    public Work2()
    {
        super("交通灯");
        setSize(400,100);
        setBackground(Color.LIGHT_GRAY);
        setLocation(300,240);
        setBounds(100,100,230,230);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Font font1=new Font("宋体 ",Font.PLAIN,16);
        this.setLayout(new FlowLayout());
        certain=new JButton("确认");
        add(certain);
        certain.addActionListener(this);
        this.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        //getActionCommand()函数主要是提取按钮中的文本是啥
        if(e.getActionCommand().equals("确认"))
        {
            certain.setText("确认一次");
        }
    }
}

2.交通灯例

不是很难,主要要对布局有理解,把整个框架当成一个大面板来看待,而中间又夹杂着两个小面板

上面的面板要设置成在上方

下面的面板要设置在下方,在大面板布局中要体现

上面小面板中的几个组件,就流式布局,下面的要用网格布局,因为一个一个的交通灯,都要用一个一个的标签来表示,不过的是背景颜色的更改。然后就是事件处理对象和事件源的关联。

package week4;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI_2 extends JFrame implements ActionListener{
    //按钮
    private JButton b_open,b_close;
            JButton J_h;//横向通行
            JButton J_z;//纵向通行
         JLabel[][] deng=new JLabel[6][6];
    public GUI_2()
    {
        super("交通灯");
        setSize(400,100);
        setBackground(Color.LIGHT_GRAY);
        setLocation(300,240);
        setBounds(100, 100, 230, 230);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Font font1=new Font("宋体 ",Font.PLAIN,16);
        Panel top=new Panel();
        top.setLayout(new FlowLayout());
        J_h=new JButton("横向通行");
        top.add(J_h);
        J_z=new JButton("纵向通行");
        top.add(J_z);
        add(top,BorderLayout.NORTH);
        Panel last=new Panel();
        last.setLayout(new GridLayout(6,6,0,0));
        for(int i=0;i<6;i++)
        {
            for(int j=0;j<6;j++)
            {
                deng[i][j]=new JLabel(" ");
                deng[i][j].setFont(font1);
                deng[i][j].setOpaque(true);//透明
                if(j==2)
                {
                    deng[i][j].setBackground(Color.red);
                }
                last.add(deng[i][j]);
            }
        }
        for(int z=0;z<6;z++) {
            deng[2][z].setBackground(Color.black);
        }
        add(last, BorderLayout.CENTER);
    
        J_z.setEnabled(false);
        this.setVisible(true);

        J_h.addActionListener(this);
        J_z.addActionListener(this);
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new GUI_2();
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==J_h)
        {
            for(int i=0;i<6;i++)
            {
                for(int j=0;j<6;j++)
                {
                    if(j==2)
                    {
                        deng[i][j].setBackground(Color.black);
                    }
                
                }
            }
            for(int z=0;z<6;z++) {
                deng[2][z].setBackground(Color.red);
            }
            J_h.setEnabled(false);
            J_z.setEnabled(true);
        }else if(e.getSource()==J_z)
        {
            for(int i=0;i<6;i++)
            {
                for(int j=0;j<6;j++)
                {
                    if(j==2)
                    {
                        deng[i][j].setBackground(Color.red);
                    }
                
                }
            }
            for(int z=0;z<6;z++) {
                deng[2][z].setBackground(Color.black);
            }
            J_h.setEnabled(true);
            J_z.setEnabled(false);
        }
    }

}
原文地址:https://www.cnblogs.com/YenKoc/p/13125328.html