Tak prosto ale nie działa

0

Czemu po dodaniu panelu do okienka nie pojawia się ono w oknie?
Coś pominąłem, bo nie wiem czemu tak się dzieje

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

public class MainWindow extends JFrame {
    JFrame frame1;
    Panels panels;
    MainWindow(){
        frame1 = new JFrame();
        frame1.setTitle("Dicco disco");
        frame1.setBounds(100,100,700,700);
        frame1.setResizable(true);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setLayout(null);
        JPanel panel = new JPanel();

        panel.setBounds(100,100,200,200);
        panel.setBackground(new Color(135,211,131));
        panel.setLayout(new BorderLayout());
        panel.setVisible(true);
        frame1.add(panel);
        frame1.setVisible(true);
        frame1.getContentPane().add(panels);
    }
}


0

A w ogóle coś się pojawia? Jakiś screen? BTW -- masz dwa JFrame -- frame1 oraz this -- tak ma być?

0

Okienko się pojawia oto jego kod:

import javax.swing.JFrame;

public class MainWindow extends JFrame {
    JFrame frame1;
    Panels panels;
    MainWindow(){
        frame1 = new JFrame();
        frame1.setTitle("Dicco disco");
        frame1.setBounds(100,100,700,700);
        frame1.setResizable(true);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setLayout(null);
        frame1.add(new Panels());

    }
}


No i mój Panel


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

public class Panels extends JPanel{


    Panels() {
        setBounds(100,100,200,200);
        setName("działaj");
        setForeground(Color.red);
        setVisible(true);
        }


}

Nie rozumiem czemu miałoby się nie pojawiać w Okienku, które pokazuje się normalnie itp, ale Panel się nie chce dodać - no już specjalnie zmieniam kolor by go zauważyć i nic, zła kolejność dodawania?

0

Nie powinno być tak (na szybko bez testowania)?

import javax.swing.JFrame;

public class MainWindow extends JFrame {
    MainWindow(){
        setTitle("Dicco disco");
        setBounds(100,100,700,700);
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        add(new Panels());
        setVisible(true);
    }
}

Bo po co Ci frame1 (ponawiam pytanie)?

0

No tak też próbowałem i jakoś nie zdało specjalnie egzaminu
I jestem zaskoczony czemu tak się dzieje

0
aolo23 napisał(a):

No tak też próbowałem i jakoś nie zdało specjalnie egzaminu
I jestem zaskoczony czemu tak się dzieje

To daj swój pierwotny niedziałający kod. Najprostszy, który nie działa. I pokaż screen po jego uruchomieniu -- żebyśmy widzieli co działa, a co nie.

0

Oto cały program
Nie działa dodawanie panelu

/**
 *Klasa główna programu
 */
public class Main {
    public static void main(String[] args ){
        Brett bret = new Brett();
    }

}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Klasa odpowiedzialna za wprowadzanie danych i ich kontorolę
 */
public class Brett extends JFrame implements ActionListener {
    MainWindow mainWindow;
    JLabel info_l,mhight_l,Speed_l,Probability_l;
    JTextArea nwidth_t,mhight_t,Speed_t,Probability_t;
    JLabel Size_l;
    JButton Ok_b;

    int n,m,speed;
    float probability;
    Brett(){

        setTitle("Disco disco");
        setBounds(100,100,600,400);
        setResizable(true);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);

        info_l = new JLabel("Wpisz wszystkie potrzebne paramaetry:");
        info_l.setFont(new Font("Arial",Font.BOLD,20));
        info_l.setForeground((new Color(58,195,27)));
        info_l.setBounds(70,0,500,60);
        add(info_l);

        Size_l = new JLabel("Rozmiar: n");
        Size_l.setFont(new Font("Arial",Font.BOLD,15));
        Size_l.setBounds(60,70,100,40);
        add(Size_l);

        mhight_l = new JLabel("m:");
        mhight_l.setFont(new Font("Arial",Font.BOLD,15));
        mhight_l.setBounds(350,70,30,40);
        add(mhight_l);

        Speed_l = new JLabel("Szybkość [ms]: ");
        Speed_l.setFont(new Font("Arial",Font.BOLD,15));
        Speed_l.setBounds(60,150,150,40);
        add(Speed_l);

        Probability_l = new JLabel("Prawdopodobieństwo[0-1]: ");
        Probability_l.setFont(new Font("Arial",Font.BOLD,15));
        Probability_l.setBounds(60,230,250,40);
        add(Probability_l);

        nwidth_t = new JTextArea();
        nwidth_t.setFont(new Font("Arial",Font.BOLD,15));
        nwidth_t.setBounds(180,80,150,30);
        add(nwidth_t);

        mhight_t = new JTextArea();
        mhight_t.setFont(new Font("Arial",Font.BOLD,15));
        mhight_t.setBounds(380,80,150,30);
        add(mhight_t);

        Speed_t = new JTextArea("");
        Speed_t.setFont(new Font("Arial",Font.BOLD,15));
        Speed_t.setBounds(210,150,320,30);
        add(Speed_t);

        Probability_t = new JTextArea("");
        Probability_t.setFont(new Font("Arial",Font.BOLD,15));
        Probability_t.setBounds(310,230,220,30);
        add(Probability_t);

        Ok_b = new JButton("OK");
        Ok_b.setFont(new Font("Arial",Font.BOLD,15));
        Ok_b.setBounds(60,280,470,30);
        add(Ok_b);
        Ok_b.addActionListener(this);



    }

    /**
     * Obsługa wyjątków i kontrola błędów
     * @param event
     */
    @Override
    public void actionPerformed(ActionEvent event) {
        if(event.getSource() == Ok_b){

            boolean temp = true;
            try {
                n = Integer.parseInt(nwidth_t.getText());
                if(n < 10){
                    JOptionPane.showMessageDialog(null,"Podano nieprawidłową daną: n", "Error",JOptionPane.ERROR_MESSAGE);
                    temp = false;
                }
            }catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null,"Podano nieprawidłową daną: n", "Error",JOptionPane.ERROR_MESSAGE);
                temp = false;
            }
            try {
                m = Integer.parseInt(mhight_t.getText());
                if(m <11){
                    JOptionPane.showMessageDialog(null,"Podano nieprawidłową daną: m", "Error",JOptionPane.ERROR_MESSAGE);
                    temp = false;
                };
            }catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null,"Podano nieprawidłową daną: m", "Error",JOptionPane.ERROR_MESSAGE);
                temp = false;
            }
            try {
                probability = Float.parseFloat(Probability_t.getText());
                if(probability<0 || probability >1  ){
                    JOptionPane.showMessageDialog(null,"Podano nieprawidłową daną: probability", "Error",JOptionPane.ERROR_MESSAGE);
                    temp = false;
                }
            }catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null,"Podano nieprawidłową daną: probability", "Error",JOptionPane.ERROR_MESSAGE);
                temp = false;
            }
            try {
                speed = Integer.parseInt(Speed_t.getText());
                if(speed < 0){temp = false;}
            }catch (NumberFormatException ex) {
                System.out.println(Speed_t.getText());
                JOptionPane.showMessageDialog(null,"Podano nieprawidłową daną: speed", "Error",JOptionPane.ERROR_MESSAGE);
                temp = false;
            }

            if(temp == true){
                mainWindow = new MainWindow();

            }

        }

    }
}



import javax.swing.JFrame;

/**
 * Okno w którym będzie przebiegać symulacja
 */

public class MainWindow extends JFrame {

   Panels panels= new Panels();

    MainWindow(){
       setTitle("Disco disco");
        setBounds(200,0,700,700);
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setVisible(true);
        add(panels);

    }
}

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class Panels extends JPanel{


    Panels() {
        setBounds(100,100,200,200);
        setName("działaj");
        setForeground(Color.red);
        setVisible(true);
        setLayout(null);
        JButton ok = new JButton("ok");
        ok.setBounds(10,10,30,30);
        add(ok);

        }


}

title

0

U mnie chyba działa. Spróbuj w Panels:

       //setForeground(Color.red);
        setBackground(Color.pink);

1 użytkowników online, w tym zalogowanych: 0, gości: 1