JAVA实现颜色分格
package com.company;
import javax.swing.*;
import java.awt.*;
public class Window {
Window(){
JFrame window=new JFrame("这是一个分层的窗口");//设置一个JFrame容器
window.setSize(500,300); //设置大小
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,1)); //使用网格布局
JPanel p1=new JPanel(); //添加面板
p1.setBackground(Color.red);
JPanel p2=new JPanel();
p2.setBackground(Color.blue);
JPanel p3=new JPanel();
p3.setBackground(Color.green); //设置背景颜色
window.add(p1);
window.add(p2);
window.add(p3);
window.setVisible(true);
}
private void setVisible(boolean b){
}
public static void main(String[] args){
Window window=new Window();
}
}
JAVA图形化界面实现按钮的单击事件
package com.company;
import javax.accessibility.AccessibleValue;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Onclick {
public static void main(String[] args) {
MyFrame frame=new MyFrame();
}
}
class MyFrame extends JFrame{
public MyFrame(){
setBounds(100,200,300,300);
Container contentPane=this.getContentPane();
contentPane.setLayout(new BorderLayout());
//北部
JLabel b1=new JLabel("现在还没有按钮被按下",JLabel.CENTER);//设置文字居中
b1.setFont(new Font("微软雅黑",Font.BOLD,24));
contentPane.add(b1,BorderLayout.NORTH);
//南部
JPanel p1=new JPanel();
contentPane.add(p1,BorderLayout.SOUTH);
JButton BT_RED=new JButton("红色");
BT_RED.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.setBackground(Color.red);
p1.setBackground(Color.red);
b1.setText("红色按钮被按下");
}
});
p1.add(BT_RED);
JButton BT_GREEN=new JButton("绿色");
BT_GREEN.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.setBackground(Color.green);
p1.setBackground(Color.green);
b1.setText("绿色按钮被按下");
}
});
p1.add(BT_GREEN);
JButton BT_BLUE=new JButton("蓝色");
BT_BLUE.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.setBackground(Color.blue);
p1.setBackground(Color.blue);
b1.setText("蓝色按钮被按下");
}
});
p1.add(BT_BLUE);
setVisible(true);
}
}
JAVA实现鼠标单击随机切换背景颜色
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
public class Test {
public static void main(String[] args) {
new TestFrame();
}
}
class TestFrame extends JFrame {
public TestFrame() {
setBounds(100, 200, 300, 300);
Container contentPane = this.getContentPane();
contentPane.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
Random rand=new Random();
int r=rand.nextInt(256);
int w=rand.nextInt(256);
int g=rand.nextInt(256);
contentPane.setBackground(new Color(r,w,g));
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
setVisible(true);
}
}