用java设计一个简单的界面设计,越简单越好,谢谢

Python013

用java设计一个简单的界面设计,越简单越好,谢谢,第1张

用java设计一个简单的界面可以参考如下实例:

import javax.swing.JFrame//框架

import javax.swing.JPanel//面板

import javax.swing.JButton//按钮

import javax.swing.JLabel//标签

import javax.swing.JTextField//文本框

import java.awt.Font//字体

import java.awt.Color//颜色

import javax.swing.JPasswordField//密码框

import java.awt.event.ActionListener//事件监听

import java.awt.event.ActionEvent//事件处理

import javax.swing.JOptionPane//消息窗口public class UserLogIn extends JFrame{

 public JPanel pnluser

 public JLabel lbluserLogIn

 public JLabel lbluserName

 public JLabel lbluserPWD

 public JTextField txtName

 public JPasswordField pwdPwd

 public JButton btnSub

 public JButton btnReset

 public UserLogIn(){

  pnluser = new JPanel()

  lbluserLogIn = new JLabel()

  lbluserName = new JLabel()

  lbluserPWD = new JLabel()

  txtName = new JTextField()

  pwdPwd = new JPasswordField()

  btnSub = new JButton()

  btnReset = new JButton()

  userInit()

 }

 public void userInit(){

  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//设置关闭框架的同时结束程序

  this.setSize(300,200)//设置框架大小为长300,宽200

  this.setResizable(false)//设置框架不可以改变大小

  this.setTitle("用户登录")//设置框架标题

  this.pnluser.setLayout(null)//设置面板布局管理

  this.pnluser.setBackground(Color.cyan)//设置面板背景颜色

  this.lbluserLogIn.setText("用户登录")//设置标签标题

  this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14))//设置标签字体

  this.lbluserLogIn.setForeground(Color.RED)//设置标签字体颜色

  this.lbluserName.setText("用户名:")

  this.lbluserPWD.setText("密    码:")

  this.btnSub.setText("登录")

  this.btnReset.setText("重置")

  this.lbluserLogIn.setBounds(120,15,60,20)//设置标签x坐标120,y坐标15,长60,宽20

  this.lbluserName.setBounds(50,55,60,20)

  this.lbluserPWD.setBounds(50,85,60,25)

  this.txtName.setBounds(110,55,120,20)

  this.pwdPwd.setBounds(110,85,120,20)

  this.btnSub.setBounds(85,120,60,20)

  this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口

   {

    public void actionPerformed(ActionEvent e){

     btnsub_ActionEvent(e)

    }    

   }

  ) 

  this.btnReset.setBounds(155,120,60,20)

  this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口

   {

    public void actionPerformed(ActionEvent e){

     btnreset_ActionEvent(e)

    }    

   }

  )   

  this.pnluser.add(lbluserLogIn)//加载标签到面板

  this.pnluser.add(lbluserName)

  this.pnluser.add(lbluserPWD)

  this.pnluser.add(txtName)

  this.pnluser.add(pwdPwd)

  this.pnluser.add(btnSub)

  this.pnluser.add(btnReset)

  this.add(pnluser)//加载面板到框架

  this.setVisible(true)//设置框架可显  

 }

 public void btnsub_ActionEvent(ActionEvent e){

  String name = txtName.getText()

  String pwd = String.valueOf(pwdPwd.getPassword())

  if(name.equals("")){

   JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE)

   return

  }else if (pwd.equals("")){

   JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE)

   return

  }else if(true){

   this.dispose()

  }else{

   JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE)

   return

  }

 }

 public void btnreset_ActionEvent(ActionEvent e){

  txtName.setText("")

  pwdPwd.setText("")

 }

 public static void main(String[] args){

  new UserLogIn()

 }

}

首先:采用什么技术实现

java语言可以使用awt 和swing等技术实现图形界面

推荐使用Swing,因为Swing比AWT更专业,更漂亮,组件更丰富,功能更强大。

2.   其次:分析采用什么布局

边界布局BorderLayout,配合表格布局GridLayout,既简单又美观

3.   最后:分析需求中需要用的组件

学生姓名 学号  显示信息 需要用到文本框JTextField

单选按钮 需要用到组件 JRadioButton

复选框    需要用到组件 JCheckBox

组合框    需要用到组件 JComboBox

图片效果

参考代码如下

//导入所需要的包

import java.awt.event.*

import javax.swing.border.*

import javax.swing.*

import java.awt.*

public class ClassFrame extends JFrame {// 写一个类继承自JFrame 窗体

// 定义组件

private static final long serialVersionUID = 1L

private JPanel contentPane

private JTextField tfName, tfNum, allInfo

private JRadioButton rb1, rb2

private JCheckBox cb1, cb2, cb3

private JComboBox<String> t1, t2, t3

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

ClassFrame frame = new ClassFrame()// 创建一个窗口实例

frame.setVisible(true)// 让该窗口实例可见

} catch (Exception e) {

e.printStackTrace()

}

}

})

}

/**

 * 窗口属性的设置,内部组件的初始化

 */

public ClassFrame() {

setTitle("选课ing...")//标题

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)// 设置关闭是退出JVM

setSize(450, 339)// 设置窗体大小

setLocationRelativeTo(null)// 窗体居中

contentPane = new JPanel()// 内容面板

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5))

contentPane.setLayout(new BorderLayout(0, 0))// 设置布局

setContentPane(contentPane)

JPanel panel = new JPanel(new GridLayout(5, 1, 5, 10))//5行1列的表格布局

panel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null))

contentPane.add(panel, BorderLayout.CENTER)//给panel添加边框

JPanel panel_1 = new JPanel()

panel.add(panel_1)

JLabel label = new JLabel("姓名")

panel_1.add(label)

tfName = new JTextField()

panel_1.add(tfName)

tfName.setColumns(10)

JLabel label_2 = new JLabel("学号")

panel_1.add(label_2)

tfNum = new JTextField()

tfNum.setColumns(10)

panel_1.add(tfNum)

rb1 = new JRadioButton("男")

panel_1.add(rb1)

rb1.setSelected(true)//设置单选按钮中,默认选择的按钮

rb2 = new JRadioButton("女")

panel_1.add(rb2)

ButtonGroup bts = new ButtonGroup()//单选按钮需要加入同一个ButonGroup中才能生效

bts.add(rb1)

bts.add(rb2)

JPanel panel_2 = new JPanel()

panel.add(panel_2)

cb1 = new JCheckBox("高等数学")

panel_2.add(cb1)

t1 = new JComboBox<String>()

t1.setModel(new DefaultComboBoxModel<String>(new String[] { "林老师", "赵老师", "孙老师" }))

panel_2.add(t1)

JPanel panel_3 = new JPanel()

panel.add(panel_3)

cb2 = new JCheckBox("世界经济")

panel_3.add(cb2)

t2 = new JComboBox<String>()

t2.setModel(new DefaultComboBoxModel<String>(new String[] { "张老师", "刘老师" }))

panel_3.add(t2)

JPanel panel_4 = new JPanel()

panel.add(panel_4)

cb3 = new JCheckBox("音乐赏析")

panel_4.add(cb3)

t3 = new JComboBox<String>()

t3.setModel(new DefaultComboBoxModel<String>(new String[] { "王老师", "周老师" }))

panel_4.add(t3)

JPanel panel_5 = new JPanel()

panel.add(panel_5)

JButton jbOk = new JButton("确定")

panel_5.add(jbOk)

JButton jbRest = new JButton("重填")

panel_5.add(jbRest)

JPanel panelSouth = new JPanel()

contentPane.add(panelSouth, BorderLayout.SOUTH)

JLabel labe = new JLabel("选课信息")

labe.setHorizontalAlignment(SwingConstants.LEFT)

panelSouth.add(labe)

allInfo = new JTextField()

allInfo.setColumns(30)

panelSouth.add(allInfo)

JPanel panelNorth = new JPanel()

contentPane.add(panelNorth, BorderLayout.NORTH)

JLabel labelTitle = new JLabel("学生选课界面")

labelTitle.setForeground(Color.DARK_GRAY)

labelTitle.setFont(new Font("宋体", Font.BOLD, 20))

panelNorth.add(labelTitle)

//给确定按钮添加事件处理代码

jbOk.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

StringBuilder info = new StringBuilder()

String name = tfName.getText()

String num = tfNum.getText()

String sex

if (rb1.isSelected()) {

sex = "男"

} else {

sex = "女"

}

info.append(name + num + sex)

if (cb1.isSelected()) {

String c = cb1.getText()

String t = t1.getSelectedItem().toString()

info.append(" " + c + t)

}

if (cb2.isSelected()) {

String c = cb2.getText()

String t = t2.getSelectedItem().toString()

info.append(" " + c + t)

}

if (cb3.isSelected()) {

String c = cb3.getText()

String t = t3.getSelectedItem().toString()

info.append(" " + c + t)

}

allInfo.setText(info.toString())//把学生信息和选课信息放到文本框

}

})

//给重填按钮 设置事件处理代码

jbRest.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

tfName.setText("")

tfNum.setText("")

rb1.setSelected(true)

cb1.setSelected(false)

t1.setSelectedIndex(0)

cb2.setSelected(false)

t2.setSelectedIndex(0)

cb3.setSelected(false)

t3.setSelectedIndex(0)

allInfo.setText("")

}

})

}

}