/ / pass i zmienne nazw - java, swing, jframe, jscrollpane, jtextarea

zmienne pass i name - java, swing, jframe, jscrollpane, jtextarea

Jestem początkującym programistą Java i próbuję wymyślić, jak zbudować JScrollPane. Do tej pory mam następujące kody, ale mam problem z wywołaniem JScrollPane. Proszę pomóż. Z góry dziękuję.

public class DemoTest {

public String sTEXT = null;
public JTextArea jTEXTAREA = null;
public JScrollPane jPANE = null;
public JFrame jFRAME = null;

public static void main(String[] args) {
DemoTest demo = new DemoTest();
}

public DemoTest() {
setText();
setPane();
setFrame();
}

public void setFrame() {
JFrame jFRAME = new JFrame("Demo");

jFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFRAME.setSize(350, 300);
jFRAME.setLocationRelativeTo(null);
jFRAME.setVisible(true);
jFRAME.add(jPANE); // **THIS SEEMS TO BE THE PROBLEM**
}

public void setPane() {
JScrollPane jPANE = new JScrollPane(jTEXTAREA);
}

public void setText() {
JTextArea jTEXTAREA = new JTextArea();
jTEXTAREA.setText("Hello World!");
jTEXTAREA.setEditable(false);
}
}

Odpowiedzi:

0 dla odpowiedzi № 1

Podczas próby utworzenia różnych huśtawekPrzedmioty, tworzyliście nowe zmienne lokalne zamiast przypisywać do zmiennych składowych (poziom klasy). Zauważ, że jeśli masz zmienną składową i zmienną lokalną o tej samej nazwie, możesz przypisać do zmiennej składowej za pomocą this.VAR_NAME = ...;. Aby lepiej zrozumieć zmienny zakres, przeczytaj to Zakres zmiennych w Javie post, który dobrze wyjaśnia podstawy.

public class DemoTest {

public  String          sTEXT           = null;
public  JTextArea       jTEXTAREA       = null;
public  JScrollPane     jPANE           = null;
public  JFrame          jFRAME          = null;

public static void main(String[] args) {
DemoTest demo = new DemoTest();
}

public DemoTest(){
setText();
setPane();
setFrame();
}

public void setFrame(){
jFRAME = new JFrame ("Demo");
jFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFRAME.setSize(350,300);
jFRAME.setLocationRelativeTo(null);
jFRAME.setVisible(true);
jFRAME.add(jPANE);

}

public void setPane(){
jPANE = new JScrollPane(jTEXTAREA);
}

public void setText(){
jTEXTAREA= new JTextArea();
jTEXTAREA.setText("Hello World!");
jTEXTAREA.setEditable(false);
}
}