0

I have a problem I can't fix. I am really new in programming.

The main frame (glavnoOkno) opens normal. When the button on the main frame is clicked new frame opens. And you can't see all the text or text boxes in it. The ones that go in the width of the screen you can, but others not.

I tried .pack(), but it didn't work, then I tried ScrollPane(), but that didn't work either (probably because i don't know where to put it :( ).. Anyway, my code is in four classes. In here i put two, that i think matters. If i am wrong i can put other two as well. None of the variables are in english, i hope that is not too big of a problem.

So... please help. This is class for the main frame. And on the bottom is defined the second frame.

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

     public class GlavnoOkno extends JFrame implements ActionListener {
          private JLabel naslov;
      private JButton gumbVnesiEleganten;
      private JButton gumbKoncaj;
      private JPanel plosca;
      private JScrollPane zvitek = new JScrollPane();
      private JTextArea tekstnoOkno = new JTextArea();
      private Trgovina trgovina;

public void setTrgovina(Trgovina trgovina) {
    this.trgovina = trgovina;
}

public Trgovina getTrgovina() {
    return trgovina;
}

public JTextArea getTekstnoOkno() {
    return tekstnoOkno;
}

public GlavnoOkno(Trgovina trg) {
    setTrgovina(trg);

    Container cp = this.getContentPane();
    cp.setLayout(new BorderLayout());

    naslov = new JLabel("Prosim, da pritisnete ustrezen gumb ...");
    cp.add(naslov, BorderLayout.PAGE_END);

    plosca = new JPanel();
    cp.add(plosca, BorderLayout.PAGE_START);

    gumbVnesiEleganten = new JButton("Vnesi eleganten cevelj.");
    plosca.add(gumbVnesiEleganten);
    gumbVnesiEleganten.addActionListener(this);

    gumbKoncaj = new JButton("Koncaj");
    plosca.add(gumbKoncaj);
    gumbKoncaj.addActionListener(this);

    zvitek.setAutoscrolls(true);
    cp.add(zvitek, BorderLayout.CENTER);
    zvitek.getViewport().add(tekstnoOkno);

    setSize(300, 400);
    setTitle("Glavno okno programa");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e) {

    Object m = e.getSource();;

    if (m == gumbVnesiEleganten) {
        Eleganten novi = null;
        try {
            JFrame oknoVnesiEleganten = new VnesiEleganten(novi, this);
            oknoVnesiEleganten.setVisible(true);
            oknoVnesiEleganten.pack();
            trgovina.getCevlji1()[0] = novi;
        } catch (Exception e1) {
            getTekstnoOkno().append("Napaka - poskusite znova!\n");
        } 
    } 
    else if (m == gumbKoncaj) {
        System.exit(0);
    }
}

}

And this is a class for the second frame.

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

    public class VnesiEleganten extends JFrame implements ActionListener {

    private Eleganten nov;
private JLabel naslov;
private GlavnoOkno glavnoOkno;
private JButton gumbShraniEleganten;
private JTextField poljeCena;
private JTextField poljePopust;
private JTextField poljeMaterial;
private JTextField poljeBarva;
private JTextField poljeOtroski;
private JTextField poljeSpol;
private JTextField poljePeta;
private JPanel plosca;


public void setEleganten(Eleganten novi) {
    this.nov = novi;
}
public Eleganten getEleganten() {return nov;}


public VnesiEleganten(Eleganten novi, GlavnoOkno okno) {

setEleganten(novi);
glavnoOkno = okno;

Container cp = this.getContentPane();
cp.setLayout(new BorderLayout());

naslov = new JLabel("Prosim, da vnesete polja, shranite eleganten cevelj ter zaprete okno.");
cp.add(naslov, BorderLayout.PAGE_END);

plosca = new JPanel();
cp.add(plosca, BorderLayout.PAGE_START);

plosca.add(new JLabel("Cena = "));
poljeCena = new JTextField(10);
plosca.add(poljeCena);

plosca.add(new JLabel("Popust = "));
poljePopust = new JTextField(10);
plosca.add(poljePopust);

plosca.add(new JLabel("Material = "));
poljeMaterial = new JTextField(20);
plosca.add(poljeMaterial);

plosca.add(new JLabel("Barva = "));
poljeBarva = new JTextField(20);
plosca.add(poljeBarva);

plosca.add(new JLabel("Otroski (false/true) = "));
poljeOtroski = new JTextField(10);
plosca.add(poljeOtroski);

plosca.add(new JLabel("Moski/zenski = "));
poljeSpol = new JTextField(10);
plosca.add(poljeSpol);

plosca.add(new JLabel("Visina pete = "));
poljePeta = new JTextField(10);
plosca.add(poljePeta);

gumbShraniEleganten = new JButton("Shrani");
plosca.add(gumbShraniEleganten);
gumbShraniEleganten.addActionListener(this);

setTitle("Vnos novega elegantnega cevlja.");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

public void actionPerformed(ActionEvent e) {

    Object m = e.getSource();;
    Eleganten novi = null;;

    if (m == gumbShraniEleganten) {
        try {
            novi = new Eleganten();
            novi.setCena(Double.parseDouble(poljeCena.getText()));
            novi.setPopust(Integer.parseInt(poljePopust.getText()));
            novi.setMaterial(poljeMaterial.getText());
            novi.setBarva(poljeBarva.getText());
            novi.setOtroski(Boolean.parseBoolean(poljeOtroski.getText()));
            novi.setSpol(poljeSpol.getText());
            novi.setVisinaPete(Double.parseDouble(poljePeta.getText()));
            setEleganten(novi);
            glavnoOkno.getTekstnoOkno().append("" + novi + "\n");
            poljeCena.setText("");
            poljePopust.setText("");
            poljeMaterial.setText("");
            poljeBarva.setText("");
            poljeOtroski.setText("");
            poljeSpol.setText("");
            poljePeta.setText("");
        } catch (Exception e1) {
            glavnoOkno.getTekstnoOkno().append("Napaka - poskusite znova!\n");
        }
    }
}

}

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
linalina
  • 1
  • 2
  • Added Swing tag to your question. – Magnilex Apr 23 '13 at 08:37
  • An application should only have a single JFrame. A child windows should be a JDialog with the frame specified as the parent. See: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice – camickr Apr 23 '13 at 15:17

1 Answers1

3

Side note: non-english variable names make it extremely hard to read code. Avoid that.


I think you should be using a JScrollPane. Try this:

JPanel yourPanel = new JPanel();
// all kinds of code that adds elements to it
// now wrap the panel in a scroll pane
JScrollPane scrollPane = new JScrollPane(yourPanel);

// and use the scroll pane instead of your main component
Container cp = this.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(scrollPane, BorderLayout.CENTER);
iluxa
  • 6,941
  • 18
  • 36
  • it is working. Thank you so much :) as for the non-english variables, i know, but is it much easier for me to read and work with code. – linalina Apr 23 '13 at 11:36