小编Hov*_*els的帖子

窗口不想在应该在java中运行的东西上运行

我正在和一个朋友一起开发一个程序而且它不想运行,因为程序中唯一的东西是错误,窗口,它没有显示错误或修复它.

这也是我在堆栈溢出时的第一次

码:

package examplepackage;
//imports
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import examplepackage.GetFilepath;

public class Starter {

    public static void main(String[] args){
        GetFilepath FP = new GetFilepath();
        JFrame win1 = new JFrame("windowname");
        win1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win1.getContentPane().add(win1, FlowLayout.LEFT, FlowLayout.LEADING);
        win1.pack();
        win1.setVisible(true);
        win1.setLocationRelativeTo(win1);
        win1.setIconImage(new ImageIcon(FP + "\\window\\Main").getImage());
    }

}
Run Code Online (Sandbox Code Playgroud)

java swing

-2
推荐指数
1
解决办法
38
查看次数

如何将数据添加到文本文件中,而不是覆盖我在Java中的内容

到目前为止这是我的代码,但它已经覆盖了我在文本文件中的内容.我想要的是将它添加到文本文件中的新行.

import java.io.*;
import java.util.Scanner;

public class Login{
  public static void main(String[] args) throws IOException {
    Scanner s1,s2;
    s1 = new Scanner(new FileInputStream("login.txt"));
    s2 = new Scanner(System.in);
    boolean loggedIn = false;
    String name,pword,n,p;
    System.out.println("Are you a new user? (Type y for yes or n for no)");
    String nU = s2.next();

    if (nU.equals("n"))
    {
    System.out.println("Enter username:");
    n=s2.next();
    System.out.println("Enter password:");
    p=s2.next();
    while(s1.hasNext()){
      name=s1.next();
      pword=s1.next();
      if(n.equals(name) && p.equals(pword)){
        System.out.println("You are logged in.");
        loggedIn = true;
        break;
      }
    }
    if(!loggedIn)
      System.out.println("Incorrect password or username."); …
Run Code Online (Sandbox Code Playgroud)

java file-io file

-2
推荐指数
1
解决办法
473
查看次数

编译器/解释器中的符号前瞻

在为简单的编程语言构建某种解释器时,我偶然发现了一个有趣的问题。我称之为“符号前瞻”问题。

我这是什么意思?例如,在 C/C++ 编译器中,您将要使用的符号必须始终已在代码上方的某处声明。像这样:

struct int_pair;

struct rectangle {
    int_pair position;
    int_pair size;
};

struct int_pair {
    int x, y;
};
Run Code Online (Sandbox Code Playgroud)

而不是这样的:

struct rectangle {
    int_pair position;
    int_pair size;
};

struct int_pair {
    int x, y;
};
Run Code Online (Sandbox Code Playgroud)

在 C# 或 Java 中,可以在文件中的任意位置使用任何符号:

public class Rectangle {
    private IntPair position, size; // using IntPair before declaring it
}

public class IntPair {
    public int Sum() { // using x and y before declaring it
        return x + y;
    }

    public int …
Run Code Online (Sandbox Code Playgroud)

c compiler-construction interpreter

-2
推荐指数
1
解决办法
240
查看次数

Platform.RunLater(),swingUtilities.InvokeLater()和Thread(runnable)混淆

请有人帮我澄清这些概念来自c ++,并尝试学习java一段时间,并且当我尝试尝试一些摇摆应用程序时,我只是达到这些条件(前两个)...

java concurrency swing javafx

-3
推荐指数
1
解决办法
1464
查看次数

编写程序以使用java swing滚动文本

有两个java文件Animate和Anim1.Anim1文件有JFrame,我想附加Animate文件,它具有屏幕滚动的文本逻辑(应该是JFrame屏幕).但我找不到方法.并且还有代码抛出以下编译时错误 - 线程"Thread-0"中的异常java.lang.Error:未解决的编译问题:方法repaint()未定义为类型Animation

import java.awt.Graphics;

public class Animation implements Runnable {
    int x=500;
    String s="hello world";
    public void run(){
        while(true){
            repaint();
            try{
                Thread.sleep(50);
            }catch(InterruptedException e){
                e.printStackTrace();
                }

        }

    }
    public void paint(Graphics g){
        g.drawString("hello world", x,-10 );
        x--;
        if(x< -100){
            x=500;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Anim1 {
    public static void main(String[] args){
        Animation a= new Animation();
        Thread t= new Thread(a);
        t.start();

        JFrame frame= new JFrame("animate");
        frame.setVisible(true);
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
}
Run Code Online (Sandbox Code Playgroud)

java animation swing

-3
推荐指数
1
解决办法
827
查看次数

没有自变量构造函数的超类

根据Oracle的文档https://docs.oracle.com/javase/tutorial/java/IandI/super.html,其书面内容是:如果超类没有无参数构造函数,则会出现编译时错误。

但就我而言,我有一个没有任何构造函数的超类。在我的基类中,我正在其无参数构造函数中编写super()。在这里,我在超类中没有no-arg构造函数,但是它没有显示任何错误。

class Person { 

} 
Run Code Online (Sandbox Code Playgroud)
/* subclass Student extending the Person class */
class Student extends Person { 
    Student() { 
        // invoke or call parent class constructor 
        super(); 
        System.out.println("Student class Constructor"); 
    } 
} 
Run Code Online (Sandbox Code Playgroud)
// Driver class 
class Practice {
    public static void main(String[] args) {
        Student s = new Student(); 
    }
}
Run Code Online (Sandbox Code Playgroud)

java inheritance default-constructor

-3
推荐指数
1
解决办法
57
查看次数

超过年份时,日历添加方法错误

我必须使用java.util.Calendar #add(...)方法错误,因为它给了我意想不到的结果.假设一些初始条件:

  • 我创建了一个Calendar对象并将其初始化为2012年1月30日.
  • 我尝试添加47周,从中提取日期并打印出结果.
  • 我尝试将48周添加到原始实例(或其克隆副本),并打印出结果.

我认为这两个结果之间会有7天的差异,但我得到7天+ 1年.

例如:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;

public class CalendarFun {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();

      cal.set(Calendar.MONTH, Calendar.JANUARY);
      cal.set(Calendar.DAY_OF_MONTH, 30);
      cal.set(Calendar.YEAR, 2012);

      Date date = cal.getTime();
      SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY");
      System.out.println(dateFormat .format(date));

      Calendar newCal = (Calendar) cal.clone();
      newCal.add(Calendar.WEEK_OF_YEAR, 47);
      System.out.println("add 47 weeks: " + dateFormat.format(newCal.getTime()));

      newCal = (Calendar) cal.clone();
      newCal.add(Calendar.WEEK_OF_YEAR, 48);
      System.out.println("add 48 weeks: " + dateFormat.format(newCal.getTime()));
   }
}
Run Code Online (Sandbox Code Playgroud)

打印出:

01/30/2012
add 47 weeks: 12/24/2012
add 48 …
Run Code Online (Sandbox Code Playgroud)

java calendar date

-4
推荐指数
1
解决办法
956
查看次数

Java,在Try-Catch之后继续

当我运行程序时,100除以数字,但一旦达到0,就会出现一条消息告诉用户100不能除以0.

如何在错误发生后让程序继续运行,并将100除以其余数字.

这是我目前的代码:

public class testing {
    int[] table;
    int number;

    public testing(int number) {
        this.number = number;
        table = new int[number];
    }

    public static void main(String[] args) {
        try {
            int[] numbers = { 2, 3, 0, 4, 5 };
            testing division;
            for (int i = 0; i < 5; i++) {
                division = new testing(100 / numbers[i]);
                System.out.println("Answer is " + division.number);
            }
        } catch (Exception e) {
            System.out.println("A number cannot be divided by 0");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java try-catch

-4
推荐指数
1
解决办法
349
查看次数

JAVA GAME循环?

我的java游戏有问题.我是初学者,但我必须把它写成一个学校项目.
游戏被称为"生日蛋糕",蛋糕上有7个蜡烛,其中一个正在显示为30秒,在这段时间你必须点击它才能得到点,如果你在这段时间没有点击它下一支蜡烛将显示.10个蜡烛显示游戏结束.
我做了循环,我试图使它工作太久,我沮丧
我的for循环工作,但它是如此之快,我使用Thread.sleep(1000),我尝试了很多解决方案,它看起来不错.但是,当我开始游戏时,没有任何事情发生,几秒钟后,所有7支蜡烛显示并迅速消失.我想我做错了什么,但我不知道是什么.

   if(Dane.start){

        int liczbaLosowa = 0;

        for(int i=0; i<10 ;i++){
            liczbaLosowa = (int)(Math.random()*7);

            this.wspX= wspX_p[liczbaLosowa];
            this.wspY= wspY_p[liczbaLosowa];
            g2d.drawImage(plomienImg, wspX, wspY,null);
            Toolkit.getDefaultToolkit().sync();
            try {
                Thread.sleep(1000);     
            } catch (Exception ex) { }
            //repaint();
        }
        Dane.start=false;

        }
Run Code Online (Sandbox Code Playgroud)

java swing loops image java-2d

-5
推荐指数
1
解决办法
776
查看次数

如何使用"this"关键字作为参数?

我尝试执行此操作时为什么会出错?它说需要参数,找到int int int.

public class testt
{   int a;
    int b;
    int c;
    public testt (){
        this(0,0,0);
    }
}
Run Code Online (Sandbox Code Playgroud)

还有,将"this"作为参数是什么意思?例如

Object object = new Object (this);
Run Code Online (Sandbox Code Playgroud)

java

-6
推荐指数
1
解决办法
51
查看次数

无需运行程序即可计算

我被要求命名变量x的值,下面的算法将在运行后输出.我不知道如何计算它,而不实际将其写入实际的Java程序.解决方案应该是1024.如何在纸上计算?

算法:

int n = 4;
int x = 1;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
       x += x;
    }
}
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)

java

-6
推荐指数
1
解决办法
90
查看次数