简体   繁体   English

在Java框架中看不到所有文本

[英]Can't see all the text in a java frame

I have a problem I can't fix. 我有一个无法解决的问题。 I am really new in programming. 我真的是编程新手。

The main frame (glavnoOkno) opens normal. 主框架(glavnoOkno)打开正常。 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. 我尝试了.pack(),但是没有用,然后我尝试了ScrollPane(),但是也没有用(可能是因为我不知道将它放在哪里:()..无论如何,我的代码是分为四节课,我认为这很重要,如果我错了,我也可以再加上两节,所有变量都不是英文的,我希望这不是太大的问题。

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

} }

Side note: non-english variable names make it extremely hard to read code. 旁注:非英语变量名使代码阅读变得极为困难。 Avoid that. 避免那样。


I think you should be using a JScrollPane. 认为您应该使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM