JAVA,如何加一个下拉菜单??
这是一个窗体的程序,实现绘制正弦曲线的目的.现在想加进一个下拉菜单,里面有"正,余,切"三个选项,如何加进这个下拉菜单~,请高手指引!importjava.awt.*;i...
这是一个窗体的程序,实现绘制正弦曲线的目的.
现在想加进一个下拉菜单,里面有"正,余,切"三个选项,如何加进这个下拉菜单~, 请高手指引!
import java.awt.* ;
import java.awt.event.*;
public class Sinea extends Frame{
public static void main(String arg[]) {
Sine s = new Sine("Sine curve");
s.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
s.setSize(430, 250);
MyCanvas cnv= new MyCanvas( );
cnv.setSize(400, 200);
cnv.setBackground(Color.white);
cnv.setForeground(Color.black);
s.add(cnv);
s.setVisible(true); // for JDK 1.5
//s.show(); // for JDK 1.4
}
public Sinea(String title)
{
super(title);
}
}
class P{
int marg=10, xorg=0, yorg=100, width=400, height=200;
double sx = 1.0, sy = 80.0;
int x, y, u, v;
public P(int u, int v){
this.u = u;
this.v = v;
UVtoXY();
}
public void UVtoXY(){
x = xorg + marg + (int)(sx*(double)u);
y = yorg + marg - (int)(sy*(double)v);
}
public void UVtoXY(double du, double dv){
x = xorg + marg + (int)(sx*du);
y = yorg + marg - (int)(sy*dv);
}
}
class MyCanvas extends Canvas{
public void paint(Graphics g) {
int i;
double u, v, frq=2.0;
P org = new P(0, 0);
P x400 = new P(400, 0);
P ypone = new P(0, 1);
P ymone = new P(0, -1);
g.drawLine(org.x, org.y, x400.x, x400.y);
g.drawLine(ypone.x, ypone.y, ymone.x, ymone.y);
P pold = new P(0, 0);
P pnew = new P(0, 0);
for(i = 0; i < 360 ; i=i+1) {
u = (double)i;
v = Math.sin(frq*u*Math.PI/180.0);
pnew.UVtoXY(u, v);
g.drawLine(pold.x, pold.y, pnew.x, pnew.y);
pold.x = pnew.x; pold.y = pnew.y;
}
}
} 展开
现在想加进一个下拉菜单,里面有"正,余,切"三个选项,如何加进这个下拉菜单~, 请高手指引!
import java.awt.* ;
import java.awt.event.*;
public class Sinea extends Frame{
public static void main(String arg[]) {
Sine s = new Sine("Sine curve");
s.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
s.setSize(430, 250);
MyCanvas cnv= new MyCanvas( );
cnv.setSize(400, 200);
cnv.setBackground(Color.white);
cnv.setForeground(Color.black);
s.add(cnv);
s.setVisible(true); // for JDK 1.5
//s.show(); // for JDK 1.4
}
public Sinea(String title)
{
super(title);
}
}
class P{
int marg=10, xorg=0, yorg=100, width=400, height=200;
double sx = 1.0, sy = 80.0;
int x, y, u, v;
public P(int u, int v){
this.u = u;
this.v = v;
UVtoXY();
}
public void UVtoXY(){
x = xorg + marg + (int)(sx*(double)u);
y = yorg + marg - (int)(sy*(double)v);
}
public void UVtoXY(double du, double dv){
x = xorg + marg + (int)(sx*du);
y = yorg + marg - (int)(sy*dv);
}
}
class MyCanvas extends Canvas{
public void paint(Graphics g) {
int i;
double u, v, frq=2.0;
P org = new P(0, 0);
P x400 = new P(400, 0);
P ypone = new P(0, 1);
P ymone = new P(0, -1);
g.drawLine(org.x, org.y, x400.x, x400.y);
g.drawLine(ypone.x, ypone.y, ymone.x, ymone.y);
P pold = new P(0, 0);
P pnew = new P(0, 0);
for(i = 0; i < 360 ; i=i+1) {
u = (double)i;
v = Math.sin(frq*u*Math.PI/180.0);
pnew.UVtoXY(u, v);
g.drawLine(pold.x, pold.y, pnew.x, pnew.y);
pold.x = pnew.x; pold.y = pnew.y;
}
}
} 展开
2个回答
展开全部
import java.awt.Canvas;
import java.awt.Choice;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.*;
public class Sinea extends Frame implements ItemListener{
public static void main(String arg[]) {
Sinea s = new Sinea("Sine curve");
s.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
s.setLayout(new FlowLayout());
s.setSize(430, 280);
Choice choice=new Choice();
choice.addItem("正");
choice.addItem("余");
choice.addItem("切");
choice.addItemListener((ItemListener) new Sinea());
s.add(choice);
MyCanvas cnv= new MyCanvas( );
cnv.setSize(400, 200);
cnv.setBackground(Color.white);
cnv.setForeground(Color.black);
s.add(cnv);
s.setVisible(true); // for JDK 1.5
//s.show(); // for JDK 1.4
}
public Sinea(){}
public Sinea(String title)
{
super(title);
}
public void itemStateChanged(ItemEvent e){
//TODO 写事件处理方法
}
}
class P{
int marg=10, xorg=0, yorg=100, width=400, height=200;
double sx = 1.0, sy = 80.0;
int x, y, u, v;
public P(int u, int v){
this.u = u;
this.v = v;
UVtoXY();
}
public void UVtoXY(){
x = xorg + marg + (int)(sx*(double)u);
y = yorg + marg - (int)(sy*(double)v);
}
public void UVtoXY(double du, double dv){
x = xorg + marg + (int)(sx*du);
y = yorg + marg - (int)(sy*dv);
}
}
class MyCanvas extends Canvas{
public void paint(Graphics g) {
int i;
double u, v, frq=2.0;
P org = new P(0, 0);
P x400 = new P(400, 0);
P ypone = new P(0, 1);
P ymone = new P(0, -1);
g.drawLine(org.x, org.y, x400.x, x400.y);
g.drawLine(ypone.x, ypone.y, ymone.x, ymone.y);
P pold = new P(0, 0);
P pnew = new P(0, 0);
for(i = 0; i < 360 ; i=i+1) {
u = (double)i;
v = Math.sin(frq*u*Math.PI/180.0);
pnew.UVtoXY(u, v);
g.drawLine(pold.x, pold.y, pnew.x, pnew.y);
pold.x = pnew.x; pold.y = pnew.y;
}
}
}
import java.awt.Choice;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.*;
public class Sinea extends Frame implements ItemListener{
public static void main(String arg[]) {
Sinea s = new Sinea("Sine curve");
s.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
s.setLayout(new FlowLayout());
s.setSize(430, 280);
Choice choice=new Choice();
choice.addItem("正");
choice.addItem("余");
choice.addItem("切");
choice.addItemListener((ItemListener) new Sinea());
s.add(choice);
MyCanvas cnv= new MyCanvas( );
cnv.setSize(400, 200);
cnv.setBackground(Color.white);
cnv.setForeground(Color.black);
s.add(cnv);
s.setVisible(true); // for JDK 1.5
//s.show(); // for JDK 1.4
}
public Sinea(){}
public Sinea(String title)
{
super(title);
}
public void itemStateChanged(ItemEvent e){
//TODO 写事件处理方法
}
}
class P{
int marg=10, xorg=0, yorg=100, width=400, height=200;
double sx = 1.0, sy = 80.0;
int x, y, u, v;
public P(int u, int v){
this.u = u;
this.v = v;
UVtoXY();
}
public void UVtoXY(){
x = xorg + marg + (int)(sx*(double)u);
y = yorg + marg - (int)(sy*(double)v);
}
public void UVtoXY(double du, double dv){
x = xorg + marg + (int)(sx*du);
y = yorg + marg - (int)(sy*dv);
}
}
class MyCanvas extends Canvas{
public void paint(Graphics g) {
int i;
double u, v, frq=2.0;
P org = new P(0, 0);
P x400 = new P(400, 0);
P ypone = new P(0, 1);
P ymone = new P(0, -1);
g.drawLine(org.x, org.y, x400.x, x400.y);
g.drawLine(ypone.x, ypone.y, ymone.x, ymone.y);
P pold = new P(0, 0);
P pnew = new P(0, 0);
for(i = 0; i < 360 ; i=i+1) {
u = (double)i;
v = Math.sin(frq*u*Math.PI/180.0);
pnew.UVtoXY(u, v);
g.drawLine(pold.x, pold.y, pnew.x, pnew.y);
pold.x = pnew.x; pold.y = pnew.y;
}
}
}
展开全部
//我不知道 动作怎么写但是 可以添加上 你要的菜单栏
//我用的swing窗体 不是awt 但是今本都相同
private JMenuBar menubar;//菜单栏
private JMenu menu;//菜单
JMenuItem jmi1,jmi2,jmi3;//菜单项目
menubar=new JMenuBar();
jmi1=new JMenuItem("正");
jim1.addActionListener(正的动作)
jmi2=new JMenuItem("切");
jim2.addActionListener(切的动作)
jmi3=new JMenuItem("余");
jim3.addActionListener(余的动作)
menu=new JMenu();
jm_student.add(jmi1);
jm_student.add(jmi2);
jm_student.add(jmi3);
menubar.add(menu);
//这个是写在 继承frame的那个类中写的(添加菜单栏)
this.setJMenuBar(menubar);
//我用的swing窗体 不是awt 但是今本都相同
private JMenuBar menubar;//菜单栏
private JMenu menu;//菜单
JMenuItem jmi1,jmi2,jmi3;//菜单项目
menubar=new JMenuBar();
jmi1=new JMenuItem("正");
jim1.addActionListener(正的动作)
jmi2=new JMenuItem("切");
jim2.addActionListener(切的动作)
jmi3=new JMenuItem("余");
jim3.addActionListener(余的动作)
menu=new JMenu();
jm_student.add(jmi1);
jm_student.add(jmi2);
jm_student.add(jmi3);
menubar.add(menu);
//这个是写在 继承frame的那个类中写的(添加菜单栏)
this.setJMenuBar(menubar);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询