永发信息网

为什么java中的JFrame不能显示

答案:1  悬赏:10  手机版
解决时间 2021-04-29 05:59
  • 提问者网友:原来太熟悉了会陌生
  • 2021-04-28 23:41

我写了一段代码,编译时没有错误,可运行不出来。代码如下:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.lang.*;
import java.applet.*;

public class c3_ extends JApplet implements ActionListener{
public static void main(String[] arg)
{
new c3_();
}
public c3_()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f=new JFrame("正方形的变换");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton r1,r2,r3,r4;
Container cp=f.getContentPane();
f.pack();
f.setVisible(true);
r1=new JRadioButton("rotate");
r1.addActionListener(this);
cp.add(r1);
r2=new JRadioButton("translate");
cp.add(r2);
r3=new JRadioButton("scale");
cp.add(r3);
r4=new JRadioButton("shear");
cp.add(r4);
r2.addActionListener(this);
r3.addActionListener(this);
r4.addActionListener(this);


}
double a,b;
double c,d;
public void init()
{a=50;
b=50;
c=100;
d=100;
}
public void paint(Graphics g)
{ super.repaint();
Graphics2D g2=(Graphics2D)g;
Line2D.Double line1=new Line2D.Double(a,b,c,b);
Line2D.Double line2=new Line2D.Double(a,d,c,d);
Line2D.Double line3=new Line2D.Double(a,b,a,d);
Line2D.Double line4=new Line2D.Double(c,b,c,d);
g2.draw(line1);
g2.draw(line2);
g2.draw(line3);
g2.draw(line4);

}
public void actionPerformed(ActionEvent e)
{String rbt=e.getActionCommand();
if(rbt=="translate")
{a=50+30;
b=50+30;
c=c+30;
d=d+30;
}
if(rbt=="scale")
{a=a*2;
b=b*2;
c=c*2;
d=d*2;
}
if(rbt=="rotate")
{a=a*Math.cos(15)-b*Math.sin(15);
b=a*Math.sin(15)-b*Math.cos(15);
c=c*Math.sin(15)-d*Math.cos(15);
d=c*Math.cos(15)-d*Math.sin(15);
}

}
}不知道哪里有错误,运行时总是提示小程序启动失败。请高手指点下。万分感谢!

最佳答案
  • 五星知识达人网友:渡鹤影
  • 2021-04-29 00:37

f.setVisible(true);


f.setSize(500,400);


设置个窗口的大小可以出来


我试过了


不知道你是咋运行的


我在eclipse 3.4运行没问题


我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯