📄 textview.java
字号:
/**
*
*/
package hnu.software.pfdai;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
/**
* @author daipengfei
*
*/
public class TextView extends JPanel implements Observer {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel tipLabel;
private JTextField countIn;
private ButtonGroup group;
private JRadioButton noChangeRB;
private JRadioButton randomRB;
private JRadioButton changeRB;
/**
* Constructs a new JPanel that is initially invisible.
*
*/
public TextView() {
// input view
tipLabel = new JLabel("please input your play times:");
countIn = new JTextField(10);
// initial the radioButton
noChangeRB = new JRadioButton("No change", false);
randomRB = new JRadioButton("Random choice", false);
changeRB = new JRadioButton("Choose another door", false);
// initial the radioButton group
group = new ButtonGroup();
group.add(noChangeRB);
group.add(randomRB);
group.add(changeRB);
this.setLayout(new GridLayout(5, 1));
// add component to panel
add(tipLabel);
JPanel panel = new JPanel();
JLabel label1 = new JLabel(" ");
JLabel label2 = new JLabel(" ");
// add the input for countIn
panel.add(label1);
panel.add(countIn);
panel.add(label2);
add(panel);
// add radioButton but visible is false
add(noChangeRB);
add(randomRB);
add(changeRB);
noChangeRB.setVisible(false);
randomRB.setVisible(false);
changeRB.setVisible(false);
}
/**
* main test
*
* @param args
*/
public static void main(String[] args) {
JFrame myFrame = new JFrame();
myFrame.add(new TextView());
myFrame.setSize(400, 300);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
/**
* This method is called whenever the observed object is changed. An
* application calls an <tt>Observable</tt> object's
* <code>notifyObservers</code> method to have all the object's observers
* notified of the change.
*
* @param o
* the observable object.
* @param arg
* an argument passed to the <code>notifyObservers</code>
* method.
*/
public void update(Observable o, Object arg) {
Model model = (Model) o;
if (model.getTimes() == 0) {
// hide radioButton
noChangeRB.setVisible(false);
randomRB.setVisible(false);
changeRB.setVisible(false);
// appear the input
tipLabel.setText("please input your play times:");
countIn.setVisible(true);
countIn.setText("");
} else {
// show the radioButton
noChangeRB.setVisible(true);
randomRB.setVisible(true);
changeRB.setVisible(true);
countIn.setVisible(false);
tipLabel.setText("your choice is " + model.getChoice()
+ " one goat is in " + model.getTipChoice());
}
}
/**
* add Listener
*
* @param controller
*/
public void addActionListener(Controller controller) {
countIn.addActionListener(controller);
noChangeRB.addActionListener(controller);
randomRB.addActionListener(controller);
changeRB.addActionListener(controller);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -