因为你把显示语句的方法体定义在了Jpanelscreena的有参构造器中,而你Jpanelscreena jll=new Jpanelscreena();这一句调用的确实无参构造器,这样的话,有参构造器当中的方法体当然不会被执行啦。改法如下:
import javaawt;
import javaxswing;
public class JPanelDemo {
public static void main(String args[]) {
JFrameDemoa jf = new JFrameDemoa();
jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
jfsetVisible(true);
}
}
class JFrameDemoa extends JFrame {
public JFrameDemoa() {
thissetTitle("nihao");
thissetBounds(100, 100, 200, 200);
Jpanelscreena jll = new Jpanelscreena();
add(jll);
}
}
class Jpanelscreena extends JPanel {
private int width = 20;
private int height = 20;
public void paintComponnet() {
}
public void paint(Graphics g) {
superpaintComponent(g);
gdrawString("Java is money", 20, 20);
}
}
记得给我分哦^_^
import javaappletApplet;
import javaawt;
import javaawtgeom;
import javaxswingJFrame;
public class picture extends JFrame {
public picture(){
//如果你想让标题栏消失你可以不注释这条语句
// thissetUndecorated(true);
setLocation(50, 50);
setResizable(false);
setSize(535, 412);
setVisible(true);
}
public void paint(Graphics g) {
//把背景设成白色
setBackground(ColorWHITE);
//把画笔设成2D的这样画笔的粗细可以改变
Graphics2D g_2d =(Graphics2D)g;
//设置画笔大小
BasicStroke bs = new BasicStroke(4,BasicStrokeCAP_SQUARE,BasicStrokeJOIN_ROUND);
g_2dsetStroke(bs);
//画圆
g_2dsetColor(ColorORANGE);
Ellipse2D oval = new Ellipse2DDouble(430, 50, 80, 80);
g_2ddraw(oval);
//画房子
g_2dsetColor(ColorBLACK);
Line2D line1=new Line2DDouble(120,140,40,220);
Line2D line2=new Line2DDouble(120,140,200,220);
Line2D line3=new Line2DDouble(40,222,200,222);
g_2ddraw(line1);
g_2ddraw(line2);
g_2ddraw(line3);
Rectangle2D rect = new Rectangle2DDouble(40,222,162,100);
g_2ddraw(rect);
//花小草
g_2dsetColor(Colorgreen);
Line2D line4=new Line2DDouble(240,270,220,320);
Line2D line5=new Line2DDouble(240,270,250,315);
Line2D line6=new Line2DDouble(250,315,260,270);
Line2D line7=new Line2DDouble(260,270,275,320);
Line2D line8=new Line2DDouble(275,320,280,270);
Line2D line9=new Line2DDouble(280,270,300,314);
Line2D line10=new Line2DDouble(300,314,315,270);
Line2D line11=new Line2DDouble(315,270,325,330);
g_2ddraw(line4);
g_2ddraw(line5);
g_2ddraw(line6);
g_2ddraw(line7);
g_2ddraw(line8);
g_2ddraw(line9);
g_2ddraw(line10);
g_2ddraw(line11);
}
public static void main(String[] args) {
new picture();
}
}
//你自己对这座标改改由于时间有限只能这样了不如你的图好看哪弄不懂可以密我896182794
import javaxswing;
import javaawt;
import javaawtevent;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton(""),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton(""),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //当前运算的运算符
private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private double resultNum = 00; // 计算的中间结果
private boolean operateValidFlag = true; //判断 *** 作是否合法
public Calculator()
{
super("Calculator");
thissetBounds(300, 300, 300, 300);
thissetResizable(false);
thissetBackground(Colororange);
thissetDefaultCloseOperation(EXIT_ON_CLOSE);
thisgetContentPane()setLayout(new BorderLayout());//设置布局
text_double=new JTextField("0",20);//设置文本区
text_doublesetHorizontalAlignment(JTextFieldRIGHT);//设置水平对齐方式未右对齐
thisgetContentPane()add(text_double,BorderLayoutNORTH);//将文本区添加到Content北部
JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局
thisgetContentPane()add(panel);//添加panel面板
for(int i=0;i<buttonlength;i++)//在面板上添加按钮
paneladd(button[i]);
for(int i=0;i<buttonlength;i++)
button[i]addActionListener(this);//为按钮注册
text_doublesetEditable(false);//文本框不可编辑
text_doubleaddActionListener(this);//
thissetVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= egetActionCommand();//返回与此动作相关的命令字符串。
Systemoutprintln("##########command is "+c);
if(cequals("C")){
handleC(); //用户按了“C”键
}
else if (cequals("CE")) // 用户按了"CE"键
{
text_doublesetText("0");
}
else if ("0123456789"indexOf(c) >= 0) // 用户按了数字键或者小数点键
{
handleNumber(c); // handlezero(zero);
} else //用户按了运算符键
{
handleOperator(c);
}
}
private void handleC() // 初始化计算器的各种值
{
text_doublesetText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//输入的第一个数字
{
text_doublesetText(button);
} else if ((buttonequals("")) && (text_doublegetText()indexOf("") < 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1
{
text_doublesetText(text_doublegetText() + "");
} else if (!buttonequals(""))// 如果输入的不是小数点,则将数字附在结果文本框的后面
{
text_doublesetText(text_doublegetText() + button);
}
// 以后输入的肯定不是第一个数字了
firstDigit = false;
}
private void handleOperator(String button) {
if (operatorequals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0
if (getNumberFromText() == 00){
// *** 作不合法
operateValidFlag = false;
text_doublesetText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operatorequals("+")){
// 加法运算
resultNum += getNumberFromText();
} else if (operatorequals("-")){
// 减法运算
resultNum -= getNumberFromText();
} else if (operatorequals("")){
// 乘法运算
resultNum = getNumberFromText();
} else if (operatorequals("sqrt")) {
// 平方根运算
if(getNumberFromText()<0){
operateValidFlag = false;
text_doublesetText("被开方数不能为负数");}
else
resultNum = Mathsqrt(resultNum);
}
else if (operatorequals("+/-")){
// 正数负数运算
resultNum = resultNum (-1);
} else if (operatorequals("=")){
// 赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
text_doublesetText(StringvalueOf(t1));
} else {
text_doublesetText(StringvalueOf(resultNum));
}
}
operator = button; //运算符等于用户按的按钮
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() //从结果的文本框获取数字
{
double result = 0;
try {
result = DoublevalueOf(text_doublegetText())doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 实例
} catch (NumberFormatException e){
}
return result;
}
public static void main(final String[] args) {
new Calculator();
}
}
关键是你要说清楚你想找哪类的啊,有很多类,游戏类,网络类,web类的,还有手机上的J2ME,你搞清楚后在来问吧,这孩子,怪可怜的,如果你要applets的话,java安装目录下的DEMO中就有,你可以自己琢磨
import javautilScanner;
public class chart {
public static void main(String[] args) {
Scanner sc = new Scanner(Systemin);
String str=scnextLine();
if(strequals("你好")) Systemoutprintln("hello!");
else Systemoutprintln("听不懂哦");
}
}
public class RandomNum
{
public static void main(String[] args)
{
int num=(int)(Mathrandom()800);//取800以内随机数
Systemoutprintln("所取随机数为:"+num);
Systemoutprintln("它的百位数字为:"+(num/100));//取百位
Systemoutprintln("它的十位数字为:"+((num-(num/100)100))/10);//取十位
Systemoutprintln("它的十位数字为:"+(num%10));//取个位
}
}
import javaawtColor;
import javaawtGraphics;
import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJPanel;
public class DrawTest extends JFrame {
JPanel buttonPanel=new JPanel();
JButton round=new JButton("圆");
JButton rectangle=new JButton("长方形");
int roundX=130,roundY=150,roundW=50,roundH=50;
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
setTitle("圆和长方形");
setLayout(null);
setSize(600,400);
setResizable(false);
setLocationRelativeTo(null);
buttonPanelsetLayout(null);
buttonPanelsetBounds(0, 300, 600, 100);
roundsetBounds(170, 20, 60, 30);
rectanglesetBounds(350, 20, 80, 30);
buttonPaneladd(round);
buttonPaneladd(rectangle);
add(buttonPanel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(final Graphics g){
superpaint(g);
gsetColor(Colorblack);
gfillOval(roundX,roundY,roundW,roundH);
gfillRect(rectangleX,rectangleY,rectangleW,rectangleH);
roundaddMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (egetButton() == MouseEventBUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
gfillOval(roundX,roundY,roundW,roundH);
repaint();
}
}
});
rectangleaddMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (egetButton() == MouseEventBUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
gfillRect(rectangleX,rectangleY,rectangleW,rectangleH);
repaint();
}
}
});
}
public static void main(String[] args) {
new DrawTest()init();
}
}
以上就是关于java SWING小程序全部的内容,包括:java SWING小程序、求用JAVA编写一个关于静园的小程序 比如一个院子里有房子 小草 太阳 树。。。。。。、java:编写一个计算器小程序,要求可以做加减乘除运算等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)