java8matlab gui图形界面面问题求助

Tobey_51 的BLOG
用户名:Tobey_51
文章数:27
访问量:2166
注册日期:
阅读量:5863
阅读量:12276
阅读量:396803
阅读量:1087329
51CTO推荐博文
学习资料来源:斯坦福大学公开课编程方法cs106aJAVA(下载中心有资源)相关学习资料已上传至下载中心:学习过程中使用的代码、笔记初稿以及课程讲义(有兴趣者可自行下载)学习过程中的大部分程序需要导入acm.jar包(已上传至下载中心,也可自行进入http://jtf.acm.org/ 进行下载),GObject:&& &super()――调用父类构造函数&&& acm.graphics――添加进画布中的图形是有堆叠顺序的&&& GCanvas――拼贴画的背景画布&&& 画布(GCanvas)和图形程序(GraphicsProgram)具有的方法: &&&&&&& add(object)& 向画布中添加对象&&&&&&& add(object,x,y)& 指定添加对象坐标&&&&&&& remove(object)& 移除对象&&&&&&& getElementAt(x,y)-frontmost or null& 根据坐标获取对象&&&&&&& getHidth()& 获取宽度&&&&&&& getHeight()& 获取高度&&&&&&& setBackground(c)-color 设置画布背景颜色&&& 图形程序独有的方法:&&&&&&& pause(milliseconds) ―― 暂停(单位为毫秒) &&&&&&&& waitForClick() ―― 等待鼠标点击事件&&& CObjects通用方法:&&&&&&& setLocation(x,y) ―― 设置坐标&&&&&&& move(dx,dy) ―― 偏移量 (dx为正向右移动,dx为负向左移动,dy为正向下移动,dy为负向上移动 )&&&&&&& getX() getY() ―― 返回对象的x和y坐标 &&&&&&&& getHidth() getHeight() ―― 返回长和宽 &&&&&&&& contains(x,y) ―― 返回真假(在特定的点处是否有存在对象) &&&&&&&& setColor(c) getColor() ―― 设置/获取对象颜色 &&&&&&&& setVisible(flag) ―― 设置可视性(true/false) &&&&&&&& isVisible() ―― 如果对象可视visible返回true&&&&&&& sendToFront() sendToBack() ―― 改变z轴顺序(将指定对象移至z轴最前|最后位置) &&&&&&& sendForward() sendBackward()――在z轴上移动对象&&& GObject中的常用接口(Interface): &&&&&&& 填充接口:GFillable――setFilled(flag)& isFilled()& setFillColor(c)& getFillColor()&&&&&&& 设置对象窗口:CResizable――setSize(width,height)& setBounds(x,y,width,height)&&&&&&& 缩放接口:GScalable――scale(sf)&& scale(sx,sy)-sx,sy为x,y上的缩放比例&&& 与排版有关的与字符有关的线:&&&&&&& 基线(baseline)――字符出现位置的线(有些字符会超出)-下滑线&&&&&&& 字符串高度(height):是指两行基准线之间的距离&&&&&&& 上线(ascent)――指最高的字符――让字符居中显示时使用&&&&&&& 下线(decent)――指基线下字符最远能达到的距离&&&&& &&& &&& & GPolygon――画出线段组成的对称图形&&& 需要有一个参考点(假想参考点)――一般是图形的中心点&& &&&& 用法:(绘制一个菱形)&& &&& &&& &private GPolygon createDiamend(double width, double height){&& &&& &&& &&& &GPolygon diamend = new GPolygon();&& &&& &&& &&& &//diamend.addVertex(-width / 2, 0);&& &&& &&& &&& &diamend.addEdge(width/2,- height/2);&& &&& &&& &&& &//diamend.addVertex(0, -height / 2);&& &&& &&& &&& &diamend.addEdge(width/2, height/2);&& &&& &&& &&& &//diamend.addVertex(width / 2, 0);&& &&& &&& &&& &diamend.addEdge(-width/2, height/2);&& &&& &&& &&& &//diamend.addVertex(0, height / 2);&& &&& &&& &&& &diamend.addEdge(-width/2,- height/2);&& &&& &&& &&& &&& &&& &&& &}&& &&& &&& &指定多边形顶点:&& &&& &&& &&& &addVertex(x,y)――――x、y是相对于参考点的坐标&& &&& &&& &&& &addEdge(dx,dy)――添加一个顶点与前一个点有关&&& GCompound――画复合的图形-把几种图形复合在一起成为一个对象&&& 事件驱动程序(Event-driven Programs)&&&&&&& 监听器(listener)―impot java.awt.event.*;&&&&&&&&&&& addMouseListeners() ―― 鼠标监听器&&&&&&&&&&& addKeyListeners() ―― 键盘监听器&&&&&&& Mouse Events:&&&&&&&&&&& mouseClicked(e)&&& ――点击&&&&&&&&&&& mousePressed(e)&&& ――按着鼠标&&&&&&&&&&& mouseReleased(e)&& ――松开鼠标&&&&&&&&&&& mouseMoved(e)&&&&& ――移动鼠标&&&&&&&&&&& mouseDragged(e)&&& ――按着鼠标拖动&&&&&&& Keyboard Events: &&&&&&&&&&& keyPressed(e)&&& ――按着键盘中的某键&&&&&&&&&&& keyReleased(e)&& ――松开键盘中的某键&&&&&&&&&&& keyTyped(e)&&&&& ――按下和释放的复合&& &&&& 用法:&& &&& &&& &/*捕获鼠标的实时位置信息*/&& &&& &&& &public class MouseTracker extends GraphicsProgram{&& &&& &&& &&& &public void run(){&& &&& &&& &&& &&& &label = new GLabel("");&& &&& &&& &&& &&& &label.setFont("Times New Roman-36");//设置字体&& &&& &&& &&& &&& &add(label, 50, 50);&& &&& &&& &&& &&& &&& &&& &&& &&& &&& &addMouseListeners();//添加鼠标监听器&& &&& &&& &&& &}&& &&& &&& &&& &public void mouseMoved(MouseEvent e){//鼠标移动时执行的操作&& &&& &&& &&& &&& &label.setLabel("Mouse: ("+e.getX()+", "+e.getY()+")");//显示鼠标位置信息&& &&& &&& &&& &}&& &&& &&& &&& &&& &&& &&& &&& &private GL&& &&& &&& &}图形用户界面 ―― GUI(Graphic User Interface)―― “Gooey”import java.awt.event.*;――追踪事件import javax.swing.*;――创建交互对象&& &组件:&& &&& &Button(按键)& &&& &&& &Sliders(滑块)& &&& &&& &Checkboxes(复选框) &&& &&& &Radio Bottons(单选框)& &&& &&& &Combo box(组合框、下拉框)&& &&& &Text box(文本框)&& &J组件――交互式组件&& &&& &动作接收器:addActionlisteners();&& &&& &程序窗口&& &分为五个部分:―― 边界布局(border layout)&&& &交互式组件的使用:&& &&& &Button:&& &&& &&&& public void init(){&& &&& &&& &&& &but = new JBtton(“Hi”);&& &&& &&& &&& &add(but, SOUTH);//将按钮添加至SOUTH中&& &&& &&& &&& &addActionListeners(); ―― 动作接收器&& &&& &&& &}&& &&& &&& &public void actionPerformed(ActionEvent e){//接收动作并执行相应操作&& &&& &&& &&& String cmd = e.getActionCommend();//返回的是对象的值&& &&& &&& && if( cmd.equals(“Hi”)){&& &&& &&& &&& &prinln(“Hello”);&& &&& &&& && }&& &&& &&& &或:&& &&& &&& && if(e.getSource == but){//getSource返回的是对象&& &&& &&& &&& &prinln(“Hello”);&& &&& &&& && }&& &&& &&& &}&& &&& &&& &&& &&& &&& &private JB//声明按钮组件&& &&& &Checkbox:&& &&& &&& &……init() {&& &&& &&& &&& &check = new JCheckBox(“Front”);&& &&& &&& &&& &check.setSelcted(true); ―― 设置默认为选定状态&& &&& &&& &&& &add(check,SOUTH); ―― 添加复选框&& &&& &&& &}&& &&& &&& &private JCheckB&& &&& &RadioButton:&& &&& &&& &private JRadioB&& &&& &&& &private JRadioB&& &&& &&& &private JRadioB&& &&& &&& &sm = new JRadioButton(“small”);&& &&& &&& &med = new JRadioButton (“medium”);&& &&& &&& &lag = new JRadioButton (“large”);&& &&& &&& &ButtonGroup size = new ButtonGroup(); //将单选框合并到一个组内&& &&& &&& &size.add(sm);&& &&& &&& &size.add(med);&& &&& &&& &size.add(lag);&& &&& &&& &med.setSelected(true); //设置默认选项&& &&& &&& &add(sm, SOUTH);&& &&& &&& &add(med, SOUTH);&& &&& &&& &add(lag, SOUTH);&& &&& &Combo box:&& &&& &&& &pick = new JComboBox();&& &&& &&& &pick.addItem(“Black”);//添加选项&& &&& &&& &pick.addItem (“Blue”);&& &&& &&& &……&& &&& &&& &pick.setEditable(False);//设定用户不能编辑&& &&& &&& &pick.setSelectedItem(“Black”);//设定默认选项&& &&& &&& &add(pick,SOUTH);&& &&& &&& &private JComboB&& &&& &&& &文本框(Text Flied)&& &&& &&& &JTextField tf = new JTextField(10);&& &&& &&& &tf.addActionListeners(this);&& &&& &&& &add(tf.SOUTH);&& &&& &&& &if(e.getSource == tf) {&& &&& &&& && println(“hi” + tf.getText());//tf.getText()返回文本框中的内容&& &&& &&& &}布局(Layout)&& &边界布局(border layout)&& &网格布局(Grid layout)―― 从左上角向右顺序添加元素,直到行尾,然后自动换行&& &表格布局(table layout)―― 很像网格布局但更精确(不会让每个部件填充整个单元)&&& 用法:&& &&& &setLayout(new GridLayout(2,3));//2行3列&& &&& &setLayout(new TableLayout(2,3));&& &&& &add(new JButton("button 1"));&& &&& &add(new JButton("button 2"));&& &&& &add(new JButton("button 3"));&& &&& &add(new JButton("button 4"));&& &&& &add(new JButton("button 5"));&& &&& &add(new JButton("button 6"));&& &&& &组件和对应的容器 ―― Component/Container&&&&addComponentListener(this);//listeners for Components&&&&行分析函数:parseLine();&&&&示例:(创建一个容器MyCanvas)线程(Thread):启动同一个程序,可以同时运行多个进程(一个线程就相当于一个进程,多个线程互不干扰)&& &Runnable接口:继承自Runnable接口的类都可以作为一个线程&& &创建线程:(一个类)&& &&& &public class Myclass implement Runnable {&& &&& &&& &public Myclass () {}&& &&& &&& &public void run() {}&& &&& &}&& &调用线程:(通过thread.start()调用线程的执行方法)&& &&& &Myclass x = new Myclass();&& &&& &Thread t = new Thread(x);&& &&& &t.start();框架(JFrame)―― 装窗口的框架&&& 用法:(创建一个Graphical Hello框架)&& &public class GraphicalHelloWorld {&& &&& &public static void main(String[] args) {&& &&& &&& &JFrame frame = new JFrame("Graphical Hello");//创建框架,并为其命名为―Graphical Hello&& &&& &&& &//创建标签Hello world! 并使其剧中显示&& &&& &&& &JLabel label = new JLabel("Hello world!",JLabel.CENTER);//center justifield&& &&& &&& &//向框架中添加标签,设置框架尺寸&& &&& &&& &frame.add(label);&& &&& &&& &frame.setSize(500, 300);&& &&& &&& &frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户退出时结束进程&& &&& &&& &frame.setVisible(true);//设置框架可视性&& &&& &}&& &}&& &本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)实验内容:实验8&图形用户界面设计
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Java Swing
309(zeroone)
43TeacherComputerFrameGUIGUIMailClass
Teacher.java
public class Teacher
numberOne,numberT
String operator="";
public int giveNumberOne(int n)
numberOne=(int)(Math.random()*n)+1;
return numberO
public int giveNumberTwo(int n)
numberTwo=(int)(Math.random()*n)+1;
public String giveOperator()
d=Math.random();
if(d&=0.5)
operator="+";
operator="-";&
public boolean getRight(int answer)
if(operator.equals("+"))
if(answer==numberOne+numberTwo)&
&&&&&&&&&&&
else if(operator.equals("-"))
if(answer==numberOne-numberTwo)&
&&&&&&&&&&&
&&&&&&&&&&&
ComputerFrame.java
import java.awt.*;
import java.awt.event.*;
public class ComputerFrame extends Frame
implements ActionListener
{& TextField
textOne,textTwo,textR
Button getProblem,giveA
Label operatorLabel,
ComputerFrame(String s)
{ super(s);
teacher=new Teacher();
setLayout(new FlowLayout());
textOne=【代码1】&&&&
//创建textOne,其可见字符长是10
textTwo=【代码2】&&&&
//创建textTwo,其可见字符长是10
textResult=【代码3】&
//创建textResult,其可见字符长是10
operatorLabel=new Label("+");
message=new Label("你还没有回答呢");
getProblem=new Button("获取题目");
giveAnwser=new Button("确认答案");
add(getProblem);
add(textOne);
add(operatorLabel);
add(textTwo);
add(new Label("="));
add(textResult);
add(giveAnwser);
add(message);
textResult.requestFocus();
textOne.setEditable(false);
textTwo.setEditable(false);
4//getProblemActionEvent&&&&
5//giveAnwserActionEvent
6//textResultActionEvent
setBounds(100,100,450,100);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
&&&&&&&&&&&&&&&&&&&&&&&
void windowClosing(WindowEvent e)
&&&&&&&&&&&&&&&&&&&&&&&&&&
System.exit(0);
&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&
public void actionPerformed(ActionEvent e)
{ if(7) //getProblem
int number1=teacher.giveNumberOne(100);
int number2=teacher.giveNumberTwo(100);
String operator=teacher.givetOperator();
textOne.setText(""+number1);
textTwo.setText(""+number2);
operatorLabel.setText(operator);
message.setText("请回答");
textResult.setText(null);
if(8) //giveAnwser
answer=textResult.getText();
&&&&&&&&&&&&&&
int result=Integer.parseInt(answer);
&&&&&&&&&&&&&&
if(teacher.getRight(result)==true)
&&&&&&&&&&
message.setText("你回答正确");
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
{ message.setText("你回答错误");
&&&&&&&&&&&&&&
&&&&&&&&&&&&
catch(NumberFormatException ex)
&&&&&&&&&&&&
message.setText("请输入数字字符");
&&&&&&&&&&&&
textResult.requestFocus();
validate();
MainClass.java
public class MainClass
{ public static void main(String
& {& ComputerFrame
frame=9//算术测试&&
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 java gui图形界面编程 的文章

 

随机推荐