效脊哗果图
参考代码
import java.awt.*import 掘悄java.awt.event.*
import javax.swing.*
public class KeyFrame extends JFrame implements KeyListener{
JLabel jlkey
public KeyFrame() {
jlkey = new JLabel("请输入字母或者数字,其他字符不显示")
add(jlkey)
addKeyListener(this)
setLayout(new FlowLayout())
setSize(260, 160)
setTitle("输入...")
setLocationRelativeTo(null)
setDefaultCloseOperation(EXIT_ON_CLOSE)
setVisible(true)
}
public static void main(String[] args) {
new KeyFrame()
}
public void keyTyped(KeyEvent e) {//敲击键盘
c=e.getKeyChar()//得到键入的字符
repaint()//重绘
}
public void keyPressed(KeyEvent e) {//按下键盘
// c=e.getKeyChar()
// repaint()
}
public void keyReleased(KeyEvent e) {//释放键盘
}
char c
@Override
public void paint(Graphics g) {
super.paint(g)
//如果只能显示输入樱散行的是字母或者数字,那么需要if判断下
if((c<='Z'&& c>='A')||(c<='z'&&c>='a')||(c<='9'&&c>='0')){// 注意比较的是字符 '9' 和字符'0'
Font font = g.getFont()
g.setColor(Color.BLUE)
g.setFont(new Font(font.getName(),font.getStyle(),20))
g.drawString(c+"",100, 100) //绘制
}
}
}
你好旦册滑,无意间看到模腊了,代码如下:public class AppletTEST extends Applet{
TextField tfa,tfb
public void init(){
tfa=new TextField(20)
tfb=new TextField(20)
tfa.setText("21:33:35"姿皮)
Button b1 =new Button("系统时间")
this.add(tfa)
this.add(b1)
this.add(tfb)
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tfb.setText("21:33:29")
}
})
}
public static void main(String[] args) {
new AppletTEST()
}
}
import javax.swing.*import javax.swing.text.*
import java.awt.*
import java.awt.event.*
/**
* @author Hardneedl
*/
final class ButtonDemo extends JFrame {
public String getTitle() {return "ButtonDemo"}
static private final Dimension size = new Dimension(600,400)
public Dimension getPreferredSize() {return size}
public Dimension getMaximumSize() {return size}
public Dimension getMinimumSize() {return size}
public Dimension getSize(){return size}
private class ButtonAction extends AbstractAction{
private JTextComponent t
private String n
private ButtonAction(String name,JTextComponent t) {
super(name)
n=name
this.t=t
}
public void actionPerformed(ActionEvent e) {
t.setText(n)
}
}
private JButton b0,b1
private JTextField t
ButtonDemo() throws HeadlessException {
init()
attachListeners()
doLay()
}
private void init(){
t=new JTextField(20)
b0=new JButton(new ButtonAction("YES",t))
b1=new JButton(new ButtonAction("NO",t))
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
private void doLay(){
Container container = getContentPane()
JPanel p = new JPanel()
p.add(b0)
p.add(b1)
container.add(p,BorderLayout.NORTH)
container.add(t,BorderLayout.SOUTH)
pack()
setVisible(true)
}
public static void main(String...args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new ButtonDemo()
}
}
)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)