wie füge ich ein JLabel in ein JFrame ein?

Hallo zusammen,

Ich bin noch sehr neu auf diesem Gebiet und bennötige noch viel Unterstüzung was das Programmieren angeht.

ich programmiere mit Java und arbeite mit verschiedenen Klassen.

Leider funktioniert etwas nicht jedoch weiss ich nicht was...
Das Frame öffnet, jedoch wird das Bild nicht eingefügt.

vielen Dank schon im Voraus

LG Roman

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JLabel;

public class vLabel extends JLabel{

	private static final long serialVersionUID = 1L;

	
	protected void paintComponent (Graphics g) {
		
		
		super.paintComponent(g);
		Graphics2D gd2 = (Graphics2D) g;
		gd2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		try {
			System.out.println("succesfully loaded picture");
			g.drawImage(Var.ib1, 0, Var.y, 1200, 800, null);
			
			
			
			
		} catch (Exception e) {
			
			e.printStackTrace();
			Var.fehlerbeimhochladen = true;
			System.out.println("something went wrong");
		}
		repaint();
	}
	public vLabel() {
		
		
		Var.geladeneklassen ++;
		System.out.println(Var.geladeneklassen + " / " + Var.anzklassen + " ~ class vLabel");
	}
}
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Gui {
	public Gui() {
		
		Var.jf1 = new JFrame();
		Var.jf1.setSize(Var.screenw, Var.screenh);
		Var.jf1.setVisible(true);
		Var.jf1.setResizable(false);
		Var.jf1.setTitle("Projekt one");
		Var.jf1.requestFocus();
		Var.jf1.setLayout(null);
		Var.jf1.setLocationRelativeTo(null);
		
		Var.lbl1 = new vLabel();
		Var.lbl1.setBounds(0, 0, Var.screenw, Var.screenh);
		Var.lbl1.setVisible(true);
		try {
			Var.jf1.add(Var.lbl1);
			
			System.out.println("succesfully loaded Label into JFrame");
		}
		catch (Exception e){
			System.out.println("something went wrong " + "jf1.add(Var.lbl1)");
			Var.fehlerbeimhochladen = true;
		}
		
		Var.geladeneklassen ++;
		System.out.println(Var.geladeneklassen + " / " + Var.anzklassen + " ~ class Gui");
	}

}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Var {
 

	
	static int x = 6;
	static int y = 0;
	static int gox = 10;
	static int goy = 10;
	static JFrame jf1;
	static int screenw = 1200, screenh = 800;
	static JLabel lbl1;
	static int geladeneklassen = 0, anzklassen = 0;
	static boolean geladen = false, fehlerbeimhochladen = false;
	
	static BufferedImage ib1;
	public Var() {
		
		
		try {
			
			ib1 = ImageIO.read(new File("src/p2.png"));
			
		}
		catch (IOException e){
			
			e.printStackTrace();
			Var.fehlerbeimhochladen =true;
			System.out.println("something went wrong "+ "images");
		}
		
		
		geladeneklassen ++;
		System.out.println(geladeneklassen + " / " + anzklassen + " ~ class Var");
	}
}
Computer, programmieren, Java
Minecraft Plugins zu localhost Server hinzufügen?

Hey,

Ich versuche seit kurzer Zeit programmieren mit Java zu lernen und dachte mir, dass ich mit simplen Plugins für das Computerspiel Minecraft anfange.

Ich habe mir bereits auf meinem Pc einen Spigot-Server für Minecraft von Bukkit erstellt der soweit auch funktioniert. Außerdem habe ich mit Eclipse ein erstes Test-Plugin erstellt. Dieses habe ich nun als Jar-Datei exportiert und anschließend in den Plugins-Ordner, der sich im Ordner meines erstellten Spigot-Server befindet. Soweit ich es verstanden habe, sollte das Plugin dadurch beim Starten des Servers geladen und nach dem Joinen auf den Server verwendbar sein.

Allerdings funktionert dies bei mir nicht. Das Plugin kann weder auf dem Server benutzt werden, noch wird es beim Starten des Servers in der Konsole angezeigt. Auch wenn ich plugins in die Konsole, bzw. in den Chat auf dem Minecraft-Server eingebe, wird angezeigt, dass kein Plugin auf dem Server vorhanden ist.

Laut dem YouTuber BiVieh, durch den ich versuche meine ersten Schritte bei der Programmierung zu machen, sollte beim Start des Servers jedoch folgende Nachricht (bzw. so ähnlich) in der Konsole auftauchen:

Dieses Bild habe ich aus einem Video, in dem er erklärt, wie man ein Plugin aus Eclipse exportiert und sie auf einem localhost Server zum Laufen bringen kann:

(4) 💡 Plugin exportieren und plugin.yml! 💡 02-04 || Minecraft Plugins Programmieren für Anfänger - YouTube

Außerdem habe ich das Problem, dass die exportierten Jar-Dateien, im Gegensatz zu denen, in BiViehs Tutuorials nicht das Java-Icon haben, sondern das, einer txt-Datei.

Ich weiß jedoch nicht, ob dies mit meinem obigen Problem zusammenhängt, bzw. ob es eventuell sogar egal ist, ob sie ein anderes Icon haben. Mir ist jedoch aufgefallen, dass die yml-Dateien hingegen das Java-Icon haben. Hat es damit vielleicht irgendwas zu tun?

Meine 3 Fragen sind also:

  1. Wie bringe ich mein Plugin auf dem Spigot-Server zum Laufen?
  2. Warum haben die Jar-Dateien bei mir nicht das Java-Logo und bringt das irgendwelche Nachteile?
  3. Was genau sind yml-Dateien? Habe das noch nicht ganz verstanden.

Ich freue mich über jede Antwort, vor allem über die, die mir bei meinem Problem weiterhelfen können. ; )

Bild zum Beitrag
Server, Computer, programmieren, Java, Minecraft, Technologie, Spiele und Gaming
Programm beenden in Java?

Biite so machen das es nach ner Zeit beendet (Zeit egal)

int numBalls = 5 + (int)(Math.random() * ((15 - 5) + 1));
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
int score = 0;
int timer = 2;
int Java = 1;
Ball[] balls = new Ball[numBalls];




void setup() {
  size(1920, 920);
  for (int i = 0; i < numBalls; i++ ) {
    balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls);
  }
  noStroke();
  fill(255, 204);
  //  circleColor = color(255);
}


void draw() {
  background(255, 255, 255);
  for (Ball ball : balls) {
    ball.collide();
    ball.move();
    ball.display();
  }
}


void mousePressed() {
  for (Ball ball : balls) {
    if (ball == null) {
      break;
    }
    if (ball.wasHit ( mouseX, mouseY)) {
      ball.destroyBall();
    }
  }
}


class Ball {


  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;
  float lifespan;
  float radius;
  color circleColor;
  float xin;
  float yin;
  float din;
  int idin;
  Ball[] others;


  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
    lifespan = random(2000.0 - 750.0) + 750;
    radius = diameter/2;
    circleColor =  color(random(255), random(255), random(255));
  }


  void collide() {
    for (int i = id + 1; i < numBalls; i++) {
      float dx = others[i].x - x;
      float dy = others[i].y - y;
      float distance = sqrt(dx*dx + dy*dy);
      float minDist = others[i].diameter/2 + diameter/2;
      if (distance < minDist) {
        float angle = atan2(dy, dx);
        float targetX = x + cos(angle) * minDist;
        float targetY = y + sin(angle) * minDist;
        float ax = (targetX - others[i].x) * spring;
        float ay = (targetY - others[i].y) * spring;
        vx -= ax;
        vy -= ay;
        others[i].vx += ax;
        others[i].vy += ay;
      }
    }
  }


  void move() {
    vy += gravity;
    x += vx;
    y += vy;
    lifespan -= 2.0;
    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction;
    } else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > height) {
      y = height - diameter/2;
      vy *= friction;
    } else if (y - diameter/2 < 0) {
      y = diameter/2;
      vy *= friction;
    }
    /* boolean isDead() {
     return (lifespan < 0.0);
     }*/
  }


  void display() {
    stroke(255, lifespan);
    fill(circleColor, lifespan);
    ellipse(x, y, diameter, diameter);
    textAlign(CENTER, CENTER);
    fill(200);
    textSize(50);
    text("Score = " + (score), 110, 10);
  }
  boolean wasHit(float ShotX, float ShotY) {
    if ((y +diameter/2 >= ShotY && y - diameter/2 <= ShotY) &&
      (x +diameter/2 >= ShotX && x - diameter/2 <= ShotX)) {
      return true;
    }
    return false;
  }
  void destroyBall() {
    lifespan = random(500.0 - 250.0) + 250;
    score++;
    x = random(width);
    y = random(height);
  }
}
Computer, programmieren, Java, Processing, Spiele und Gaming
Java Processing Game over Screen erstellen?

Hey ich habe eine Version von Pong erstellt um mich ein wenig rumzuprobierem. Demnach ist es weder schön noch besonders ausgereift. Ich habe folgendes gecodet und Frage mich wie ich auf Knopfdruck ein neues Spiel starten kann. Ich hab schon was implementiert was aber nicht funktioniert hatt. Vielleicht ist der Ansatz ja auch richtig. Vielen Dank:

int screen = 0;
int end = 0;
float x = 0;
float y = 0;

int xball = 500;
int yball = 500;
int xdir = 1;
int ydir = 1;



void setup(){
  
 fullScreen();
 smooth();
  
}

void draw(){
 background(38,30,38);
 strokeWeight(5);
 stroke(39,255,36);
 fill(39, 181, 36);
 rect(25,(mouseY/25)*25,25,25);
 rect(25,((mouseY/25)*25)+25,25,25);
 rect(25,((mouseY/25)*25)-25,25,25);
 rect(25,((mouseY/25)*25)-50,25,25);
 rect(25,((mouseY/25)*25)+50,25,25);
  
  
 stroke(255,39,36);
 fill(181,39,36);
  
 
  
  
 fill(255,39,36);
 stroke(181,39,36);
 rect((xball/25)*25,(yball/25)*25,25,25);
 xball = xball+xdir*4;
 yball = yball+ydir*2;
  
  
  
  if (xball > width-25){
   xdir = xdir * -1;
  }  
 
 if (xball < 25){
  screen = 1;
 }
 
 if (xball < 75){
  if (xball > 25){
  if (yball > ((mouseY/25)*25)-50){
   if(yball < ((mouseY/25)*25)+50){
   xdir = xdir * -1;
  }}}}
  
  
 if (yball > height-25 || yball < 25){
   ydir = ydir * -1;
  }
   
 if (screen > 0){
  noStroke();
  fill(0);
  rect(0,0,width,height);
  stroke(255);
  fill(255);
  textSize(150);
  text("Game Over",width/4-100,height/2);
  fill(150);
  stroke(150);
  text("again?",width/4+100,height/2+260);
  end = 1;
 }
   
}



void mouseClicked (){
  
 background(38,30,38);
 if(mouseX > width/4+100){
  if(mouseX < width/4+600){
   if(mouseY > height/2+260){
    if(mouseY < height/+310){
     if(end > 0){
      screen = -1;
      end = 0;
      xball = 500;
      yball = 500;
      xdir = 1;
      ydir = 1;
      x = 0;
      y = 0;
      
 
 
 }}}}}
  
}
programmieren, Java, Processing
Wieso ändern sich die Position der Elemente am Handy?

Hey Leute,

ich habe ein kleines Preoblem und zwar die Positionen der Elemente im Android Studio enspricht nicht der am Handy also am Handy werden sie komplett anders angeordernt und nicht wie ich sie im Android Studio positioniert hatte.

Z.B. der Button sollte oben sein und nicht unten!

Hat jemand vielleicht eine Idee voran es liegen könnte?

Danke im Voraus


  

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="158dp"
        android:layout_marginBottom="274dp"
        android:text="@string/button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>



---------------------------manifest------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Test">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


-----------------gradle-------------------------
plugins {
    id 'com.android.application'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.test"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Bild zum Beitrag
programmieren, Design, Java, Android App, Android Studio

Meistgelesene Fragen zum Thema Java