运行如图
参考代码如下
import java.awt.*import java.awt.event.*
import javax.swing.*
public class RegDemo extends JFrame implements ActionListener{
JTextField jtf
JPasswordField jpf
public RegDemo() {
//组件的创建, 和布局安排
JPanel jpc = new JPanel()//默认流式布局
JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10))//网格布局
jp1.setBorder(BorderFactory.createTitledBorder("用户注册"))
JLabel jl1 = new JLabel("用户名:")
jtf = new JTextField(10)
JLabel jl2 = new JLabel("密码:")
jpf = new JPasswordField(10)
jpf.setEchoChar('*')//用*号来隐藏密码的显示
jp1.add(jl1)jp1.add(jtf)
jp1.add(jl2)jp1.add(jpf)
jpc.add(jp1)
add(jpc)
JButton jb1 = new JButton("提交")
jb1.addActionListener(this)
jb1.setActionCommand("yes")
JButton jb2 = new JButton("取消")
jb2.addActionListener(this)
jb2.setActionCommand("no")
JPanel jp2 = new JPanel()
jp2.add(jb1)jp2.add(jb2)
add(jp2,BorderLayout.SOUTH)
setTitle("用户注册界面")
setSize(280, 280)
setLocationRelativeTo(null)//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE)//
setVisible(true)
}
public static void main(String[] args) {
new RegDemo()
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("yes")){
String name = jtf.getText().trim()
String pwd = new String(jpf.getPassword())
if(name.equals("")||pwd.equals("")){
JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码")
}else{
JOptionPane.showMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd)
}
}else{
jtf.setText("")
jpf.setText("")
}
}
}
程序如下:import java.awt.FlowLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.ButtonGroup
import javax.swing.JButton
import javax.swing.JCheckBox
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JRadioButton
import javax.swing.JTextArea
public class JFrameDemo extends JFrame implements ActionListener
{
private JPanel panel
private JButton button
private JTextArea textArea
private JCheckBox musicBox
private JCheckBox danceBox
private JRadioButton hanButton
private JRadioButton manButton
private JRadioButton huiButton
private ButtonGroup buttonGroup
public JFrameDemo()
{
panel = new JPanel()
button = new JButton("确定")
textArea = new JTextArea(40,30)
musicBox = new JCheckBox("唱歌")
danceBox = new JCheckBox("跳舞")
huiButton = new JRadioButton("回族")
hanButton = new JRadioButton("汉族")
manButton = new JRadioButton("满族")
buttonGroup = new ButtonGroup()
buttonGroup.add(huiButton)
buttonGroup.add(hanButton)
buttonGroup.add(manButton)
panel.setLayout(new FlowLayout(3))
panel.add(huiButton)
panel.add(hanButton)
panel.add(manButton)
panel.add(musicBox)
panel.add(danceBox)
panel.add(button)
panel.add(textArea)
add(panel)
setTitle("选择兴趣爱好")
setBounds(100, 100, 400, 280)
setResizable(false)
setVisible(true)
this.button.addActionListener(this)
}
public static void main(String[] args)
{
new JFrameDemo()
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
String info = ""
if(this.huiButton.isSelected())
{
info += this.huiButton.getText() + "\n"
}
if(this.hanButton.isSelected())
{
info += this.hanButton.getText() + "\n"
}
if(this.manButton.isSelected())
{
info += this.manButton.getText() + "\n"
}
if(this.danceBox.isSelected())
{
info += this.danceBox.getText() + "\n"
}
if(this.musicBox.isSelected())
{
info += this.musicBox.getText() + "\n"
}
this.textArea.setText(info)
}
}
}
有问题欢迎提问,满意请采纳,谢谢!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)