Swing Inhalt vom Fenster wird erst durch Hovern oder Klicken sichtbar

UpdateDotExe

Mitglied
Hallo,
ich bin ein fortgeschrittener Einsteiger in Java ^^
Ich lerne diese Programmiersprache über YouTube Tutorials und bin nun endlich im Bereich der Benutzerdefinierten GUI Programmierung und habe mir ein zwar ganz schickes Fenster erstellt, siehe unten.

Wenn ich es aber starte, über Eclipse aber auch als exportiere JAR, komm es immer zu dem Grafikbug, dass ich einige Felder, siehe unten, nicht sehen kann.

Ich weiß mir als Noob nicht zu helfen und möchte natürlich, dass es schön aussieht, könnt ihr mir helfen?
Die Bilder:
https://picload.org/upload,7b28b9bf...056.html?sid=b772edc13a9ff976933d720884ee110b

Hier ist mein Quellcode:

Java:
package einführungGUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

@SuppressWarnings("serial")
public class Frame2 extends JFrame {

    private JPanel panelBox;
   
    private JLabel label1;
    private JLabel label2;
    private JButton button;
    private JTextField eingabe;
    private JCheckBox boxBold;
    private JCheckBox boxItalic;
    private JCheckBox boxLarge;
   
    private static int countClicks = 0;
   
   
    public static void main(String[] args) {
        @SuppressWarnings("unused")
        JFrame jf = new Frame2();
    }
   
   
    public Frame2() {
        //Window
        setTitle("Title");
        setVisible(true);
        setResizable(false);
        setAlwaysOnTop(true);
        setSize(500, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
       
       
       
       
       
        // --> Content <--
        //Panel Check Box
        panelBox = new JPanel();
        panelBox.setBounds(20, 20, 300, 100);
        panelBox.setLayout(null);
       
        add(panelBox);
       
            //CheckBox Bold
            boxBold = new JCheckBox("Fett");
            boxBold.setBounds(0, 0, 100, 25);
           
            panelBox.add(boxBold);
            boxBold.addItemListener(new FontListener());
           
            //CheckBox Italic
            boxItalic = new JCheckBox("Kursiv");
            boxItalic.setBounds(100, 0, 100, 25);
           
            panelBox.add(boxItalic);
            boxItalic.addItemListener(new FontListener());
       
            //CheckBox Large
            boxLarge = new JCheckBox("Groß");
            boxLarge.setBounds(200, 0, 150, 25);
           
            panelBox.add(boxLarge);
            boxLarge.addItemListener(new FontListener());
       
       
            //Label1
        label1 = new JLabel("Text");
        //Font fontLabel1 = (label1.getFont().deriveFont(Font.PLAIN, 30));
        {
            Font fontLabel1 = new Font("Calibri", Font.PLAIN, 30);
            label1.setFont(fontLabel1);
        }
        label1.setBounds(250, 130, 200, 50);
        label1.setToolTipText("Hallo :3");
       
        add(label1);
       
       
            //Label2
        label2 = new JLabel("Clicks");
        Font fontLabel2 = new Font("Calibri", Font.BOLD, 30);
        label2.setFont(fontLabel2);
        label2.setBounds(20, 130, 200, 50);
       
        add(label2);

       
            //Button
        button = new JButton("Click here!");
        Font fontButton = new Font("Code Bold", Font.CENTER_BASELINE, 20);
        button.setFont(fontButton);
        button.setBounds(20,  70, 200, 50);
        button.setEnabled(false);
        button.setBackground(Color.GRAY);
       
        add(button);
        button.addActionListener(new ButtonListener());
       
       
            //TextField
        eingabe = new JTextField();
        eingabe.setBounds(250, 70, 200, 51);
       
        add(eingabe);
        eingabe.addCaretListener(new SchreibkopfListener());
       
       
       
       
        //DEV Tool
        System.out.println(toString());
    }
   
   
   
    private class ButtonListener implements ActionListener {
       
        @Override
        public void actionPerformed(ActionEvent arg0) {
            label1.setText(eingabe.getText());
           
            countClicks++;
            String str = String.valueOf("Clicks: " + countClicks);
            label2.setText(str);
        }
    }
   
    private class SchreibkopfListener implements CaretListener {
       
        @Override
        public void caretUpdate(CaretEvent arg0) {
            String str = eingabe.getText();
            str = str.trim();
            if (str.isEmpty()) {
                button.setEnabled(false);
                button.setBackground(Color.GRAY);
            } else {
                button.setEnabled(true);
                button.setBackground(Color.BLACK);
                button.setForeground(Color.WHITE);
            }
        }
    }
   
   
    //Panel Check Box
    private class FontListener implements ItemListener {
       
        @Override
        public void itemStateChanged(ItemEvent e) {
            boolean bold = boxBold.isSelected();
            boolean italic = boxItalic.isSelected();
            boolean large = boxLarge.isSelected();
            int check = 0;
           
            if (bold) { label1.setFont(new Font("Calibri", Font.BOLD, 30)); check = 1;}
            if (italic) { label1.setFont(new Font("Calibri", Font.ITALIC, 30)); check = 1;}
            if (large) { label1.setFont(new Font("Calibri", Font.PLAIN, 40)); check = 1;}

            if (bold && italic) { label1.setFont(new Font("Calibri", Font.BOLD + Font.ITALIC, 30)); check = 1;}
            if (bold && large) { label1.setFont(new Font("Calibri", Font.BOLD, 40)); check = 1;}
            if (large && italic) { label1.setFont(new Font("Calibri", Font.ITALIC, 40)); check = 1;}

            if (bold && italic && large) { label1.setFont(new Font("Calibri", Font.BOLD + Font.ITALIC, 40)); check = 1;}

            if (check == 0) {
                label1.setFont(new Font("Calibri", Font.PLAIN, 30));
            }
        }
    }
}
 
Setz mal das setVisible(true) ganz ans Ende des Konstruktors. Wenn ein Frame angezeigt wird und du danach Änderungen am Layout vornimmst, musst du sonst durch ein revalidate() die GUI anweisen sich zu aktualisieren.
 
Moin,
Deine Bilder sind nicht sichtbar ("Gültigkeit der Seite abgelaufen") !!
Warum lädst Du sie nicht direkt hoch ??
Gruß Klaus
 
naja als 'fortgeschrittener Einsteiger' würde ich NIEMALS von JFrame ableiten, ausser man ändert die Funktionalität von JFrame....und das machst du nicht.

setVisible(true) immer am Ende schreiben da du ja dein Fenster sichtbar machst.

wenn ich du wäre dann würde ich mir einen eigenen Eventhandler schreiben wo alle Buttonclicks, Windowevents usw drinnen sind und von zentraler stelle verwaltet werden...
zwecks wartbarkeit.
lieber 'zehn' kleine klassen als eine riesen klasse die mit der zeit nicht mehr wartbar ist....
 

Zurück
Oben