日期:2014-05-20 浏览次数:20934 次
package test24Buttons;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class Hour {
JPanel timePanel = new JPanel();
JBusyTimeButton btn1 = new JBusyTimeButton("1");
JBusyTimeButton btn2 = new JBusyTimeButton("2");
JBusyTimeButton btn3 = new JBusyTimeButton("3");
JBusyTimeButton btn4 = new JBusyTimeButton("4");
JBusyTimeButton btn5 = new JBusyTimeButton("5");
JBusyTimeButton btn6 = new JBusyTimeButton("6");
public Hour() {
timePanel.add(btn1);
timePanel.add(btn2);
timePanel.add(btn3);
timePanel.add(btn4);
timePanel.add(btn5);
timePanel.add(btn6);
}
public JPanel getBusyTimePanel() {
return timePanel;
}
public class JBusyTimeButton extends JToggleButton {
private static final long serialVersionUID = 1L;
public JBusyTimeButton(String text) {
super(text);
setBackground(Color.green);
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!((JToggleButton)e.getSource()).getModel().isSelected()) {
((JToggleButton)e.getSource()).setBackground(Color.green);
} else {
((JToggleButton)e.getSource()).setBackground(Color.yellow);
}
}
});
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Hour bt = new Hour();
frame.add(bt.getBusyTimePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Hour {
JPanel timePanel = new JPanel();
JBusyTimeButton btn1 = new JBusyTimeButton("1");
JBusyTimeButton btn2 = new JBusyTimeButton("2");
JBusyTimeButton btn3 = new JBusyTimeButton("3");
JBusyTimeButton btn4 = new JBusyTimeButton("4");
JBusyTimeButton btn5 = new JBusyTimeButton("5");
JBusyTimeButton btn6 = new JBusyTimeButton("6");
public Hour() {
timePanel.add(btn1);
timePanel.add(btn2);
timePanel.add(btn3);
timePanel.add(btn4);
timePanel.add(btn5);
timePanel.add(btn6);
}
public JPanel getBusyTimePanel() {
return timePanel;
}
public class JBusyTimeButton extends JToggleButton {
private static final long serialVersionUID = 1L;
public JBusyTimeButton(String text) {
super(text);
setBackground(Color.green);
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// if (!((JToggleButton)e.getSource()).isSelected()) {
// ((JToggleButton)e.getSource()).setBackground(Color.green);
// } else {
// ((JToggleButton)e.getSource()).setBackground(Color.YELLOW);
// }
JToggleButton j=(JToggleButton)e.getSource();
ButtonModel bm=j.getModel();
if(bm.isSelected()) {
j.setBackground(Color.YELLOW);
System.out.println("奇数次按下,更改颜色");
}
}
});
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Hour bt = new Hour();
frame.add(bt.getBusyTimePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}