0

我有一个无法解决的问题。我真的是编程新手。

主框架 (glavnoOkno) 正常打开。单击主框架上的按钮时,会打开新框架。而且您看不到其中的所有文本或文本框。可以进入屏幕宽度的那些,但其他的则不能。

我尝试了 .pack(),但没有用,然后我尝试了 ScrollPane(),但也没有用(可能是因为我不知道把它放在哪里:().. 无论如何,我的代码是在四个班级中。在这里我放了两个,我认为这很重要。如果我错了,我也可以放其他两个。变量都不是英文的,我希望这不是太大的问题。

所以……请帮忙。这是主框架的类。并且在底部定义了第二帧。

    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);
    }
}

}

这是第二帧的一个类。

    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");
        }
    }
}

}

4

1 回答 1

3

旁注:非英文变量名使代码难以阅读。避免这种情况。


认为您应该使用 JScrollPane。尝试这个:

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);
于 2013-04-23T08:38:52.673 回答