1
I’m trying to create a project where I print several sentences in one JTextArea
, but for that, I need to include a JScrollPane
. I looked at some examples on the internet but none is working.
public class Projeto extends JFrame {
JPanel painel = new JPanel();
public static JTextArea textarea = new JTextArea();
public static JScrollPane sp = new JScrollPane();
JButton cmd1;
public static void main(String[] args) {
new Projeto();
}
public Projeto(){
super("Árvore");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
painel.setPreferredSize(new Dimension(600,400));
painel.setLayout(null);
cmd1 = new JButton("Start!");
cmd1.setBounds(450, 50, 100, 30);
painel.add(cmd1);
textarea.setBounds(50, 50, 300, 300);
textarea.setEditable(true);
sp.add(textarea);
painel.add(sp);
add(painel);
pack();
setVisible(true);
event e = new event();
cmd1.addActionListener(e);
}
public static void atualiza(String frase){
textarea.append(frase);
}
public class event implements ActionListener{
Pai p = new Pai();
public void actionPerformed(ActionEvent e){
p.start();
}
}
}
The problem is that I use the.add(sp) panel; but Jscrollpane and Jtextarea are no longer appearing in the panel.
– Gustavo Cruz
@Have you changed Gustavocruz as per the answer? This seems to be another different problem.
– user28595
I changed yes and still have the same problem.
– Gustavo Cruz
@Gustavocruz the problem has nothing to do with this, is that you are using absolute layout, which is a terrible practice. Search for layouts managers for swing, absolute layout only causes this kind of problem, especially if you don’t know exactly what you’re doing with it.
– user28595
@Gustavocruz but if you want to continue using absolute layout, see the edition.
– user28595
The problem has been solved. Thank you very much for helping Articuno.
– Gustavo Cruz