java一个窗口里怎么添加两个面板,上下各一个,颜色不同
创建如下窗口,一个窗口中包含两个面板p1和p2。p1中的颜色在黑白间切换;p2中的底色在红绿间切换。不是用点击按钮来切换,是模拟线程,自动的切换,不停的那种...
创建如下窗口,一个窗口中包含两个面板p1和p2。
p1中的颜色在黑白间切换;p2中的底色在红绿间切换。
不是用点击按钮来切换,是模拟线程,自动的切换,不停的那种 展开
p1中的颜色在黑白间切换;p2中的底色在红绿间切换。
不是用点击按钮来切换,是模拟线程,自动的切换,不停的那种 展开
2个回答
展开全部
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ColorFrame extends JFrame {
private JPanel panel1, panel2;
public ColorFrame() {
setLayout(new GridLayout(-1, 1));
panel1 = new JPanel();
panel2 = new JPanel();
getContentPane().add(panel1);
getContentPane().add(panel2);
panel1.setBackground(Color.red);
JButton btn1 = new JButton("White");
JButton btn2 = new JButton("Black");
panel1.add(btn1);
panel1.add(btn2);
new ToogleGroup(panel1, btn1, Color.white, btn2, Color.black);
JButton btn3 = new JButton("Red");
JButton btn4 = new JButton("Green");
panel2.add(btn3);
panel2.add(btn4);
new ToogleGroup(panel2, btn3, Color.red, btn4, Color.green);
}
private static class ToogleGroup implements ActionListener {
private JButton btn1;
private JButton btn2;
private Color color1;
private Color color2;
private Component target;
public ToogleGroup(Component target, JButton btn1, Color color1,
JButton btn2, Color color2) {
this.btn1 = btn1;
this.btn2 = btn2;
btn1.addActionListener(this);
btn2.addActionListener(this);
this.target = target;
this.color1 = color1;
this.color2 = color2;
toggleButton(btn1);
}
private void toggleButton(Object src){
if (src == btn1) {
btn1.setVisible(false);
btn2.setVisible(true);
target.setBackground(color1);
} else {
btn1.setVisible(true);
btn2.setVisible(false);
target.setBackground(color2);
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
toggleButton(src);
}
}
public static void main(String[] args) {
ColorFrame colorFrame = new ColorFrame();
colorFrame.setSize(300, 300);
colorFrame.setLocationRelativeTo(null);
colorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
colorFrame.setVisible(true);
}
}
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ColorFrame extends JFrame {
private JPanel panel1, panel2;
public ColorFrame() {
setLayout(new GridLayout(-1, 1));
panel1 = new JPanel();
panel2 = new JPanel();
getContentPane().add(panel1);
getContentPane().add(panel2);
panel1.setBackground(Color.red);
JButton btn1 = new JButton("White");
JButton btn2 = new JButton("Black");
panel1.add(btn1);
panel1.add(btn2);
new ToogleGroup(panel1, btn1, Color.white, btn2, Color.black);
JButton btn3 = new JButton("Red");
JButton btn4 = new JButton("Green");
panel2.add(btn3);
panel2.add(btn4);
new ToogleGroup(panel2, btn3, Color.red, btn4, Color.green);
}
private static class ToogleGroup implements ActionListener {
private JButton btn1;
private JButton btn2;
private Color color1;
private Color color2;
private Component target;
public ToogleGroup(Component target, JButton btn1, Color color1,
JButton btn2, Color color2) {
this.btn1 = btn1;
this.btn2 = btn2;
btn1.addActionListener(this);
btn2.addActionListener(this);
this.target = target;
this.color1 = color1;
this.color2 = color2;
toggleButton(btn1);
}
private void toggleButton(Object src){
if (src == btn1) {
btn1.setVisible(false);
btn2.setVisible(true);
target.setBackground(color1);
} else {
btn1.setVisible(true);
btn2.setVisible(false);
target.setBackground(color2);
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
toggleButton(src);
}
}
public static void main(String[] args) {
ColorFrame colorFrame = new ColorFrame();
colorFrame.setSize(300, 300);
colorFrame.setLocationRelativeTo(null);
colorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
colorFrame.setVisible(true);
}
}
TableDI
2024-07-18 广告
2024-07-18 广告
在Excel中引用另一个工作表的数据,可以通过以下步骤操作:1. 打开包含要引用数据的源工作表和目标工作表。2. 在目标工作表中,选中要放置引用数据的单元格。3. 输入等号(=)开始公式。4. 接着输入源工作表的名称和要引用的单元格的坐标,...
点击进入详情页
本回答由TableDI提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询