Java – die neusten Beiträge

Java GUI wird nicht angezeigt?

Hey,

ich bin gerade dabei ein Java GUI Programm zu entwerfen. Allerdings sehe ich keine Buttons oder sonstige Anwendungen auf der Oberfläche.

(Sorry wenn ich mich nicht fachmännisch ausdrücke, bin noch Anfänger)

Hier ein Ausschnit

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 01.11.2016
  * @author 
  */

public class GUI extends JFrame {
  // Anfang Attribute
  private Canvas canvas1 = new Canvas();
  private JLabel jLabel1 = new JLabel();
  private JLabel jLabel2 = new JLabel();
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  private JButton jButton4 = new JButton();
  private JButton jButton5 = new JButton();
  private JButton jButton6 = new JButton();
  private JLabel jLabel3 = new JLabel();
  private JButton jButton7 = new JButton();
  private JButton jButton8 = new JButton();
  private JButton jButton9 = new JButton();
  private JTextField jTextField1 = new JTextField();
  private JButton jButton10 = new JButton();
  private JButton jButton11 = new JButton();
  private JLabel jLabel4 = new JLabel();
  private JButton jButton14 = new JButton();
  private JButton jButton12 = new JButton();
  private JButton jButton13 = new JButton();
  private JLabel jLabel5 = new JLabel();
  private JButton jButton15 = new JButton();
  private JButton jButton16 = new JButton();
  private JLabel jLabel6 = new JLabel();
  private JButton jButton17 = new JButton();
  private JButton jButton18 = new JButton();
  private JButton jButton19 = new JButton();
  // Ende Attribute
  
  public GUI() { 
    // Frame-Initialisierung
    super();
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 415; 
    int frameHeight = 865;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setTitle("Formular");
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);
    // Anfang Komponenten
    
    canvas1.setBounds(0, -8, 396, 828);
    cp.add(canvas1);
    jLabel1.setBounds(88, 24, 220, 28);
    jLabel1.setText("Dieser Text soll verändert werden");
    cp.add(jLabel1);
    jLabel2.setBounds(24, 72, 207, 20);
    jLabel2.setText("Aufgabe 1: Hintergrundfarbe ändern");
    cp.add(jLabel2);
    jButton1.setBounds(24, 104, 107, 25);
    jButton1.setText("Rot");
    jButton1.setMargin(new Insets(2, 2, 2, 2));
    jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton1_ActionPerformed(evt);
      }
    });

Danke im Voraus für alle Antworten

Anfänger, Java, Java Editor

Java Random nummern nur ein mal?

Hallo liebe Communtity! Ich habe seit dem Neuen Schuljahr in der Oberstufe jetzt auch informatik! Wir sollen eine Art "Lottomaschiene" programmieren. Unten ist jetzt schon mein Anfang. Mein Problem ist jetzt, dass auch manchmal zahlen Doppelt Generiert werden! Ich müsste um das zu verhindern jede Zahl mit jeder vergleichen (das wären ja 36 Abfragen!!) und falls eine Zahl doppelt ist, alles nochmal generieren lassen. Gibt es eine elegantere Methode dafür?

Nein, dass sind auch nicht meine Hausaufgaben. Ich bin alt genug um zu erkennen, dass Hausaufgaben für mich selbst wichtig sind und alleine gmacht werden sollen.

Danke im Voraus lg Basti

import java.util.Random; import java.util.Arrays; public class LottoZahlen {

  public static void main(String[] args) {
    //Erstellung der LotoZahlhen
    Random zufallsObjekt1 = new Random();
    int zufallszahl1 = zufallsObjekt1.nextInt(50);
    
    Random zufallsObjekt2 = new Random();
    int zufallszahl2 = zufallsObjekt2.nextInt(50);
    
    Random zufallsObjekt3 = new Random();
    int zufallszahl3 = zufallsObjekt3.nextInt(50);
    
    Random zufallsObjekt4 = new Random();
    int zufallszahl4 = zufallsObjekt4.nextInt(50);
    
    Random zufallsObjekt5 = new Random();
    int zufallszahl5 = zufallsObjekt5.nextInt(50);
    
    Random zufallsObjekt6 = new Random();
    int zufallszahl6 = zufallsObjekt6.nextInt(50);
    
    //Sollen die Zahlen angezeigt werden?
    System.out.println("Sollen die Zahlen angezeigt werden? J / N?");
    char antwort = Tastatur.leseZeichen();    
    if (antwort == 'j') {
      // Zahlen.
      int[] ints = {zufallszahl1,zufallszahl2,zufallszahl3,zufallszahl4,zufallszahl5,zufallszahl6};
      
      // Zahlen sortieren.
      Arrays.sort(ints);
      
      // Die sortierten Zahlen so ausgeben.
      String result = Arrays.toString(ints);
      result = result.replaceAll(",", " -");
      result = result.substring(1, result.length() - 1);
      
      // Zahlen ausgeben.
      System.out.println("");
      System.out.println(result);
    } // end of if
    
    } // end of main
    
    } // end of class LottoZahlen
  
programmieren, Java

Java Command "show" ist durchgestrichen(Bild)?

Ich glaube, dass durchgestrichene Commands erneuert werden oder so, stimmt das? Wenn das stimmt, was ist dann der neue Command? Oder warum funktioniert das sonst nicht? Für alle die es haben wollen den Code:

package cardlayout.tutorial;

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ItemEvent; import java.awt.event.ItemListener;

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

public class CardLayout extends JFrame {

public static void main(String[] args) {
    new CardLayout();

}

private static final long serialVersionUID = 420622292333313289L;

private Toolkit t;
private int x, y, width = 800, height = 600;

//Komponenten
private JPanel mainContainer;
private JComboBox comboBox;

public CardLayout(){
    t = Toolkit.getDefaultToolkit();
    Dimension d = t.getScreenSize();
    x = (int) ((d.getWidth() - width) / 2);
    y = (int) ((d.getHeight() - height) / 2);

    setTitle("Gui Tutorial");
    setBounds(x, y, 800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.initialisierung();
    this.initListener();


    setVisible(true);
}


private void initialisierung(){
    this.getContentPane().setLayout(new BorderLayout());

    this.mainContainer = new JPanel(new java.awt.CardLayout());
    this.comboBox = new JComboBox(new String[]{"Card1", "Card2", "Card3", "Card4"});

    this.comboBox.setEditable(false);

    this.getContentPane().add(comboBox, BorderLayout.PAGE_START);
    this.getContentPane().add(mainContainer, BorderLayout.CENTER);

}


private void initListener(){
    this.comboBox.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            CardLayout cl = (CardLayout) mainContainer.getLayout();
            cl.show(mainContainer, e.getItem().toString());
        }
    });

}

}

Bild zum Beitrag
programmieren, Java

Meistgelesene Beiträge zum Thema Java