Java的图形用户界面设计
我是一个java新手,在做图形用户界面设计时有一道题不会做,各位看看能不能帮我解决一下,在下感激不尽,题目如下:创建一个窗口(1)窗口有“退出”菜单,选择后可退出程序(2...
我是一个java新手,在做图形用户界面设计时有一道题不会做,各位看看能不能帮我解决一下,在下感激不尽,题目如下:
创建一个窗口
(1)窗口有“退出”菜单,选择后可退出程序
(2)窗口内有“提示”按钮,单击后显示自己的班级、学号、姓名
(3)“退出”按钮,单击后可退出程序
(4)在窗口内实现一个计算器,可进行简单的加、减、乘、除操作 展开
创建一个窗口
(1)窗口有“退出”菜单,选择后可退出程序
(2)窗口内有“提示”按钮,单击后显示自己的班级、学号、姓名
(3)“退出”按钮,单击后可退出程序
(4)在窗口内实现一个计算器,可进行简单的加、减、乘、除操作 展开
5个回答
展开全部
package Practise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ListIterator;
import java.util.Stack;
public class Test2 extends JFrame implements ActionListener {
private String src;
JTextField text;
JButton exit,info;
String label []={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
private void add(JPanel p,String s) {
JButton btn=new JButton(s);
btn.setFont(new Font("宋体",Font.BOLD,18));
btn.addActionListener(this);
p.add(btn);
}
public Test2() {
JPanel top=new JPanel();
text=new JTextField(12);
top.add(text);
top.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
JPanel center=new JPanel(new GridLayout(4,4));
center.setBorder(BorderFactory.createLineBorder(Color.BLUE));
for(String s:label) {
add(center,s);
}
JPanel bottom=new JPanel(new FlowLayout(FlowLayout.CENTER));
bottom.setBorder(BorderFactory.createLineBorder(Color.black));
exit=new JButton("退出");
info=new JButton("提示");
exit.addActionListener(this);
info.addActionListener(this);
bottom.add(exit);
bottom.add(info);
setTitle("计算器");
getContentPane().add(top,"North");
getContentPane().add(center,"Center");
getContentPane().add(bottom,"South");
setSize(200,250);
setVisible(true);
}
//************字符串解析******************
//允许括号运算,如果你需要的话,自己添加
public double getResult() {
String postfix = getPostfix();
Stack<String> stk = new Stack<String>();
String parts[] = postfix.split(" +");
double result = 0;
for (int i = 0; i < parts.length; i++) {
char tmp = parts[i].charAt(0);
if (!isOperator(tmp)) {
stk.push(parts[i]);
} else {
double a = Double.parseDouble(stk.pop());
double b = Double.parseDouble(stk.pop());
result = calculate(b, a, tmp);
stk.push(String.valueOf(result));
}
}
return result;
}
private boolean isOperator(char op) {
return (op == '+' || op == '-' || op == '*' || op == '/');
}
public double calculate(double a, double b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return -1;
}
private String getPostfix() {
Stack<String> stk = new Stack<String>();
String postfix = new String();
char op;
int i = 0;
while (i < src.length()) {
if (Character.isDigit(src.charAt(i)) || src.charAt(i) == '.') {
postfix += " ";
do {
postfix += src.charAt(i++);
} while ((i < src.length())
&& (Character.isDigit(src.charAt(i))));
postfix += " ";
}
else {
switch (op = src.charAt(i++)) {
case '(':
stk.push("(");
break;
case ')':
while (stk.peek() != "(") {
String tmp = stk.pop();
postfix += tmp;
if (tmp.length() == 1 && isOperator(tmp.charAt(0)))
postfix += " ";
}
stk.pop();
postfix += " ";
break;
case '+':
case '-':
while ((!stk.empty()) && (stk.peek() != "(")) {
postfix += stk.pop() + " ";
}
stk.push(new Character(op).toString());
break;
case '*':
case '/':
while ((!stk.empty())
&& ((stk.peek() == "*") || (stk.peek() == "/"))) {
postfix += stk.pop() + " ";
}
stk.push(new Character(op).toString());
break;
}
}
}
ListIterator<String> it = stk.listIterator(stk.size());
while (it.hasPrevious())
postfix += it.previous() + " ";
return postfix.trim().replaceAll(" +\\.", ".");
}
public void actionPerformed(ActionEvent e) {
String txt=((JButton)e.getSource()).getText();
for(int i=0;i<label.length;i++) {
if(txt.equals(label[i])&&(!txt.equals("="))) {
String temp=text.getText();
text.setText(temp+txt);
}
}
if (txt.equals("=")) {
src = text.getText();
if (src.length() != 0) {
text.setText(String.valueOf(getResult()));
}
}
else if(txt.equals("退出")) {
System.exit(0);
}
else if(txt.equals("提示")) {
JOptionPane.showMessageDialog(this, "班级:1\n学号:1\n姓名:XXOO");
}
}
public static void main(String[] args) {
new Test2();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ListIterator;
import java.util.Stack;
public class Test2 extends JFrame implements ActionListener {
private String src;
JTextField text;
JButton exit,info;
String label []={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
private void add(JPanel p,String s) {
JButton btn=new JButton(s);
btn.setFont(new Font("宋体",Font.BOLD,18));
btn.addActionListener(this);
p.add(btn);
}
public Test2() {
JPanel top=new JPanel();
text=new JTextField(12);
top.add(text);
top.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
JPanel center=new JPanel(new GridLayout(4,4));
center.setBorder(BorderFactory.createLineBorder(Color.BLUE));
for(String s:label) {
add(center,s);
}
JPanel bottom=new JPanel(new FlowLayout(FlowLayout.CENTER));
bottom.setBorder(BorderFactory.createLineBorder(Color.black));
exit=new JButton("退出");
info=new JButton("提示");
exit.addActionListener(this);
info.addActionListener(this);
bottom.add(exit);
bottom.add(info);
setTitle("计算器");
getContentPane().add(top,"North");
getContentPane().add(center,"Center");
getContentPane().add(bottom,"South");
setSize(200,250);
setVisible(true);
}
//************字符串解析******************
//允许括号运算,如果你需要的话,自己添加
public double getResult() {
String postfix = getPostfix();
Stack<String> stk = new Stack<String>();
String parts[] = postfix.split(" +");
double result = 0;
for (int i = 0; i < parts.length; i++) {
char tmp = parts[i].charAt(0);
if (!isOperator(tmp)) {
stk.push(parts[i]);
} else {
double a = Double.parseDouble(stk.pop());
double b = Double.parseDouble(stk.pop());
result = calculate(b, a, tmp);
stk.push(String.valueOf(result));
}
}
return result;
}
private boolean isOperator(char op) {
return (op == '+' || op == '-' || op == '*' || op == '/');
}
public double calculate(double a, double b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return -1;
}
private String getPostfix() {
Stack<String> stk = new Stack<String>();
String postfix = new String();
char op;
int i = 0;
while (i < src.length()) {
if (Character.isDigit(src.charAt(i)) || src.charAt(i) == '.') {
postfix += " ";
do {
postfix += src.charAt(i++);
} while ((i < src.length())
&& (Character.isDigit(src.charAt(i))));
postfix += " ";
}
else {
switch (op = src.charAt(i++)) {
case '(':
stk.push("(");
break;
case ')':
while (stk.peek() != "(") {
String tmp = stk.pop();
postfix += tmp;
if (tmp.length() == 1 && isOperator(tmp.charAt(0)))
postfix += " ";
}
stk.pop();
postfix += " ";
break;
case '+':
case '-':
while ((!stk.empty()) && (stk.peek() != "(")) {
postfix += stk.pop() + " ";
}
stk.push(new Character(op).toString());
break;
case '*':
case '/':
while ((!stk.empty())
&& ((stk.peek() == "*") || (stk.peek() == "/"))) {
postfix += stk.pop() + " ";
}
stk.push(new Character(op).toString());
break;
}
}
}
ListIterator<String> it = stk.listIterator(stk.size());
while (it.hasPrevious())
postfix += it.previous() + " ";
return postfix.trim().replaceAll(" +\\.", ".");
}
public void actionPerformed(ActionEvent e) {
String txt=((JButton)e.getSource()).getText();
for(int i=0;i<label.length;i++) {
if(txt.equals(label[i])&&(!txt.equals("="))) {
String temp=text.getText();
text.setText(temp+txt);
}
}
if (txt.equals("=")) {
src = text.getText();
if (src.length() != 0) {
text.setText(String.valueOf(getResult()));
}
}
else if(txt.equals("退出")) {
System.exit(0);
}
else if(txt.equals("提示")) {
JOptionPane.showMessageDialog(this, "班级:1\n学号:1\n姓名:XXOO");
}
}
public static void main(String[] args) {
new Test2();
}
}
展开全部
swing 我已多年没有碰过swing了 一直都在web
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
难是不难,但没时间~
慢慢自己把基础搞搞吧~
慢慢自己把基础搞搞吧~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为什么代码贴不上来?疯啦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2010-05-30
展开全部
我只实现了一部分功能,想要的话,可以叫我,代码贴不上了啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询