JAVA一个窗体添加三个文本框具体怎么 *** 作?

JAVA一个窗体添加三个文本框具体怎么 *** 作?,第1张

只要文本框添加监听即可,用ActionListener即会在文本框中按下回车键时触发事件。

下面是监听的代码:

public void actionPerformed(ActionEvent e)

{

Object o=e.getSource()

if(o==text1) //文本框一号

{

text2.requestFocus()//文本框2号,请求焦点

}

else if(o==text2)

{

text3.requestFocus()

}

}

程序如下:

import java.awt.FlowLayout

import java.awt.Toolkit

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JTextField

public class MyFrame extends JFrame implements ActionListener

{

private JButton button

private JTextField textField

private JLabel label

public MyFrame()

{

button = new JButton("Hello world")

textField = new JTextField(15)

label = new JLabel("输入内容:")

setTitle("Simple-Frame")

setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,

(Toolkit.getDefaultToolkit().getScreenSize().height - 300)/2

, 500, 300)

setLayout(new FlowLayout())

add(label)

add(textField)

add(button)

setResizable(false)

setVisible(true)

this.button.addActionListener(this)

}

public static void main(String[] args)

{

new MyFrame()

}

@Override

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == this.button)

{

this.textField.setText("Hello World")

}

}

}

调用组件

setBorder(new LineBorder(Color color))

可以绘制组件的边框,边框为像素为1,颜色为color的直线

具体可以参考组件setBorder()方法和LineBorder边框类的API

还有其他的边框类

可以这样:

import java.awt.*

public class Test extends Frame{

public void go(){

Button btn=new Button("带边框的按钮")

setLayout(new FlowLayout())/*Frame的默认布局为BorderLayout,如果直接添加,则Button就会充满整个Frame,无法显示画的边框*/

add(btn)

setSize(100,100)

setVisible(true)

//必须将窗口显示出来,才能用Graphics画图,否则无效

Graphics g=getGraphics()

g.setColor(Color.RED)

Point p=btn.getLocation()

g.drawRect(p.x,p.y,btn.getSize().width,btn.getSize().height)

}

public static void main(String args[]){

new Test().go()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/7958466.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存