用Java语言设计一个界面,
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Java语言设计一个界面,相关的知识,希望对你有一定的参考价值。
通过输入学生姓名,学号,通过单选按钮选择性别,通过复选框选择课程,通过组合框选择任课教师,点击确定按钮,在文本框中显示所填写的信息。
是用Netbeans可以运行就可以
首先:采用什么技术实现
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("");
);
参考技术A 你的意思是图形编辑器??
java开发工具eclipse和myeclipse都不具备图形编译功能
不过你可以试一试Dreamweaver 这个是专业的网页编辑工具追问
netbeans可以的
追答netbeans 国内用的并不多,相对主流的eclipse的来说,较卡
追问恩,可以帮我写一下这个选课界面的代码吗
参考技术B <!DOCTYPE html><html
class="chrome js no-touch rgba multiplebgs backgroundsize boxshadow textshadow opacity cssanimations cssgradients csstransitions generatedcontent
js no-touch rgba multiplebgs backgroundsize boxshadow textshadow opacity cssanimations cssgradients csstransitions generatedcontent">
<head>
<script type="text/javascript">
var viewInfo = function()
var name=document.getElementById('name').value;
var number=document.getElementById('number').value;
var sex=document.getElementById('sex').value;
var selection1=document.getElementById('selection1').value;
var selection2=document.getElementById('selection2').value;
var selection3=document.getElementById('selection3').value;
var selection4=document.getElementById('selection4').value;
var message = "姓名:"+name+"\r\n"
+"学号:"+number+"\r\n"
+"性别:"+sex+"\r\n"
+"课程:"+selection1+"中的"+selection2+"\r\n"
+"教师:"+selection3+"中的"+selection4+"\r\n"
document.getElementById("message").value = message;
</script>
<style id="holderjs-style" type="text/css">
.bootstrap-frm
margin-left:auto;
margin-right:auto;
max-width: 500px;
background: #FFF;
padding: 20px 30px 20px 30px;
font: 12px "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #888;
text-shadow: 1px 1px 1px #FFF;
border:1px solid #DDD;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
.bootstrap-frm h1
font: 25px "Helvetica Neue", Helvetica, Arial, sans-serif;
padding: 0px 0px 10px 40px;
display: block;
border-bottom: 1px solid #DADADA;
margin: -10px -30px 30px -30px;
color: #888;
.bootstrap-frm h1>span
display: block;
font-size: 11px;
.bootstrap-frm label
display: block;
margin: 0px 0px 5px;
.bootstrap-frm label>span
float: left;
width: 20%;
text-align: right;
padding-right: 10px;
margin-top: 10px;
color: #333;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: bold;
.bootstrap-frm input[type="text"], .bootstrap-frm input[type="email"], .bootstrap-frm textarea, .bootstrap-frm select
border: 1px solid #CCC;
color: #888;
height: 20px;
line-height:15px;
margin-bottom: 16px;
margin-right: 6px;
margin-top: 2px;
outline: 0 none;
padding: 5px 0px 5px 5px;
width: 70%;
border-radius: 4px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
.bootstrap-frm input[type="radio"]
height: 15px;
line-height:15px;
margin-bottom: 16px;
padding: 5px 0px 5px 5px;
margin-right: 6px;
margin-top: 2px;
.bootstrap-frm select
background: #FFF url('down-arrow.png') no-repeat right;
background: #FFF url('down-arrow.png') no-repeat right;
appearance:none;
-webkit-appearance:none;
-moz-appearance: none;
text-indent: 0.01px;
text-overflow: '';
width: 30%;
height: 35px;
line-height:15px;
.bootstrap-frm textarea
height:100px;
padding: 5px 0px 0px 5px;
width: 70%;
.bootstrap-frm .button
background: #FFF;
border: 1px solid #CCC;
padding: 10px 25px 10px 25px;
color: #333;
border-radius: 4px;
.bootstrap-frm .button:hover
color: #333;
background-color: #EBEBEB;
border-color: #ADADAD;
</style>
</head>
<body>
<form action="" method="post" class="bootstrap-frm">
<h1>Table
<span>Please fill all the texts in the fields.</span>
</h1>
<label>
<span>姓名 :</span>
<input id="name" type="text" name="name" placeholder="姓名" />
</label>
<label>
<span>学号 :</span>
<input id="number" type="email" name="number" placeholder="学号" />
</label>
<label>
<span>性别 :</span>
<input id="sex" type="radio" name="email" value="男" />男
<input id="sex" type="radio" name="email" value="女" />女
</label>
<label>
<span>课程 :</span><select name="selection" id="selection1">
<option value="理科">理科</option>
<option value="文科">文科</option>
</select>
<select name="selection" id="selection2">
<option value="语文">语文</option>
<option value="数学">数学</option>
</select>
</label>
<label>
<span>任课教师 :</span><select name="selection" id="selection3">
<option value="理科老师">理科老师</option>
<option value="文科老师">文科老师</option>
</select>
<select name="selection" id="selection4">
<option value="语文老师">语文老师</option>
<option value="数学老师">数学老师</option>
</select>
</label>
<label>
<span>显示信息 :</span>
<textarea id="message" name="message" placeholder="显示信息"></textarea>
</label>
<label>
<span> </span>
<input type="button" class="button" value="Send" onclick="viewInfo();"/>
</label>
</form>
</body>
</html>追问
求一个·能用netbeans运行的,不是网页,
用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算
设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。
急急急!!!拜托!!!!
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器
package aaa;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class YunSuan implements ActionListener,ItemListener
public static JFrame jf;
public static Container c;
public static JTextField jtf1,jtf2,jtf3;
public static JButton jb1;
public static Choice c2;
public static String fuhao="";
public YunSuan()
jf=new JFrame("加减乘除运算");
c=jf.getContentPane();
c.setLayout(new FlowLayout());
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
c2=new Choice();
jb1=new JButton("=");
public void Jiemian()
c.add(jtf1);
c2.addItemListener(this);
c2.add("");
c2.add("+");
c2.add("-");
c2.add("*");
c2.add("/");
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);
jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
public static void main(String args[])
YunSuan ys=new YunSuan();
ys.Jiemian();
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
double s1=Integer.parseInt(jtf1.getText());
double s2=Integer.parseInt(jtf2.getText());
double result=0;
if(fuhao.equals("+"))
result=s1+s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
if(fuhao.equals("-"))
result=s1-s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
if(fuhao.equals("*"))
result=s1*s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
if(fuhao.equals("/"))
result=s1/s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
public void itemStateChanged(ItemEvent ie)
if(ie.getSource()==c2)
String str1=c2.getSelectedItem();
fanhui(str1);
public String fanhui(String str2)
return fuhao=str2;
以上是关于用Java语言设计一个界面,的主要内容,如果未能解决你的问题,请参考以下文章