我有一个问题我无法解决。我是编程新手。
主框架(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");
}
}
}
}
答案 0 :(得分: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);