JAVA关于多个按钮多个标签,点击不同的按钮让不同的标签发生变化

yjyddwj yjyddwj 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
写了五个java文件,就是想让按钮被按一次,对应的面板发生一次变化,并且指示灯由红色变成绿色。
希望各位友人帮帮忙解决一下。困扰了好几天了。
第一个类:
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;


public class TestA {

private JLabel label;
private Light light;
private JPanel panel;
private String msg;

public TestA(String msg){

this.msg = msg;
light = new Light();
label = new JLabel(msg);
panel = new JPanel();

panel.setLayout(null);
light.setBounds(0,0,15,20);
panel.add(light);
label.setBounds(20,0,80,20);
panel.add(label);
}
public JPanel getP1(){
return panel;
}
}

第二个:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class TestB {

private JButton jbt1;
private JButton jbt2;
private JButton jbt3;
private Light light;
private JPanel jp;

public TestB(){

jbt1 = new JButton("我想日");
jbt2 = new JButton("他想日");
jbt3 = new JButton("你想日");
light = new Light();
jp = new JPanel();

jp.setLayout(new GridLayout(3,1));
jp.add(jbt1);
jp.add(jbt2);
jp.add(jbt3);

jbt1.addActionListener(new mainSun());
jbt2.addActionListener(new mainSun());
jbt3.addActionListener(new mainSun());

}
public JPanel getP2(){
return jp;
}

//按钮监听器
/* 想实现点击第一个按钮,然后那个日本人的面板上设置整型变量自加一次,然后灯变成绿色
* 想实现点击第二个按钮,然后那个美国人的面板上设置整型变量自加一次,然后灯变成绿色
* 想实现点击第三个按钮,然后那个印度人的面板上设置整型变量自加一次,然后灯变成绿色*/
class mainSun implements ActionListener{
int num = 0;
public void actionPerformed(ActionEvent e) {

if(e.getSource() == jbt1){

light.turnOn();//灯的颜色不变 --
}else if(e.getSource() == jbt2){


}else {

}
}
}
}

第三个:
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

public class TestC {

private TestA a;
private TestA b;
private TestA c;

private JPanel jpp;

public TestC(){

a = new TestA("日本人");
b = new TestA("美国人");
c = new TestA("印度人");
jpp = new JPanel();

jpp.setLayout(new GridLayout(3,1));
jpp.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));


jpp.add(a.getP1());
jpp.add(b.getP1());
jpp.add(c.getP1());
}
public JPanel getJPP(){
return jpp;
}

}


第四个:
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;


public class Light extends JPanel{

private static boolean light ;

public Light(){
light = false;
}

public void turnOn(){
light = true;
}

public void turnOff(){
light = false;
}

public void paintComponent(Graphics g){

super.paintComponent(g);

if(light == true){
g.setColor(Color.GREEN);
}
else{
g.setColor(Color.RED);
}
g.fillOval( 2, 5, 10, 10);
}

}


第五个,测试类:
import java.awt.BorderLayout;

import javax.swing.JFrame;



public class TestMain extends JFrame{

private JFrame frame;
//private TestA a;
private TestB b;
private TestC c;

public TestMain(){

b = new TestB();
c = new TestC();
frame = new JFrame("面板测试");

frame.setLayout(null);

c.getJPP().setBounds(180, 50, 100, 100);
frame.add(c.getJPP());

b.getP2().setBounds(20, 50, 100, 100);
frame.add(b.getP2());

frame.setSize(400,320);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {

new TestMain();
}

}
...全文

相关推荐

实训八GUI编程练习.doc

实训八GUI编程练习.doc

zzzzl333

用户登录Java程序

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Text extends JFrame implements ActionListener,TextListener { private JLabel username,password,email,telephone; private JTextField usernameField,emailField; private TextField teleField; private JPasswordField passwordField; private JButton registButton,cancelButton; public Text() { super("用户注册"); Container container = getContentPane(); container.setLayout(new FlowLayout()); username = new JLabel("用户名"); username.setToolTipText("请输入用户名"); password = new JLabel("密 码"); password.setToolTipText("密码不能少于6位"); email = new JLabel("邮 箱"); email.setToolTipText("邮箱中必须包含@字符"); telephone = new JLabel("电 话"); telephone.setToolTipText("电话只能为数字"); usernameField = new JTextField(15); usernameField.addActionListener(this); passwordField = new JPasswordField(15); passwordField.addActionListener(this); emailField = new JTextField(15); emailField.addActionListener(this); teleField = new TextField(21); teleField.addActionListener(this); teleField.addTextListener(this); registButton = new JButton("注册"); cancelButton = new JButton("取消"); container.add(username); container.add(usernameField); container.add(password); container.add(passwordField); container.add(email); container.add(emailField); container.add(telephone); container.add(teleField); container.add(registButton); container.add(cancelButton); registButton.addActionListener(this); cancelButton.addActionListener(this); setSize(250,200); setVisible(true); setResizable(false); } public void textValueChanged(TextEvent event) { if (event.getSource() == teleField) { /*if (!checkNumber(teleField.getText())) { JOptionPane.showMessageDialog(this,"电话必须为数字","温馨提示",JOptionPane.INFORMATION_MESSAGE); teleField.setText(""); } */ } } public void actionPerformed(ActionEvent event) { if (event.getSource() == usernameField) { if (usernameField.getText().equals("")) { JOptionPane.showMessageDialog(null,"用户名不能为空","温馨提示",JOptionPane.INFORMATION_MESSAGE); } } if (event.getSource() == passwordField) { if (passwordField.getPassword().length == 0) { JOptionPane.showMessageDialog(this,"密码不能为空","温馨提示",JOptionPane.INFORMATION_MESSAGE); } else if (passwordField.getPassword().length < 6) { JOptionPane.showMessageDialog(this,"密码长度不能小于6位","温馨提示",JOptionPane.INFORMATION_MESSAGE); passwordField.setText(""); } } if (event.getSource() == emailField) { if (emailField.getText().indexOf("@") = (emailField.getText().length() - 1) || emailField.getText().equals("")) { JOptionPane.showMessageDialog(this,"您的邮箱格式不正确","温馨提示",JOptionPane.INFORMATION_MESSAGE); emailField.setText(""); } } if (event.getSource() == teleField) { if (teleField.getText().equals("")) { JOptionPane.showMessageDialog(this,"联系电话不能为空","温馨提示",JOptionPane.INFORMATION_MESSAGE); } else if (!checkNumber(teleField.getText())) { JOptionPane.showMessageDialog(this,"电话必须为数字","温馨提示",JOptionPane.INFORMATION_MESSAGE); teleField.setText(""); } } if (event.getSource() == registButton) { if (usernameField.getText().equals("") || passwordField.getPassword().length == 0 || emailField.getText().equals("") || teleField.getText().equals("")) { JOptionPane.showMessageDialog(this, "您填写的信息不完整","温馨提示",JOptionPane.INFORMATION_MESSAGE); usernameField.setText(""); passwordField.setText(""); emailField.setText(""); teleField.setText(""); } else if (passwordField.getPassword().length < 6) { JOptionPane.showMessageDialog(this,"密码长度不能小于6位","温馨提示",JOptionPane.INFORMATION_MESSAGE); passwordField.setText(""); } else if (emailField.getText().indexOf("@") = (emailField.getText().length() - 1)) { JOptionPane.showMessageDialog(this,"您的邮箱格式不正确","温馨提示",JOptionPane.INFORMATION_MESSAGE); emailField.setText(""); } else if (!checkNumber(teleField.getText())) { JOptionPane.showMessageDialog(this,"电话必须为数字","温馨提示",JOptionPane.INFORMATION_MESSAGE); teleField.setText(""); } else JOptionPane.showMessageDialog(this,"恭喜您,注册成功","温馨提示",JOptionPane.INFORMATION_MESSAGE); } if (event.getSource() == cancelButton) { usernameField.setText(""); passwordField.setText(""); emailField.setText(""); teleField.setText(""); } } public boolean checkNumber(String str) { for (int i=0;i<str.length();i++) { return false; } return true; } public static void main(String args[]) { Text application = new Text(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

luckbearshu

java处理按钮点击事件的方法

下面小编就为大家带来一篇java处理按钮点击事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

weixin_38555304

java实现点击按钮事件弹出子窗口

主要为大家详细介绍了java实现点击按钮事件弹出子窗口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

假装高冷小姐姐

java实现点击按钮弹出新窗体功能

主要为大家详细介绍了java实现点击按钮弹出新窗体功能,旧窗体不进行操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

weixin_38502292
评论 2
收藏 已收藏
分享
返回顶部
yjyddwj
Eclipse 58,439 成员 49,457 内容