tdavidson_9
asked on
Java Swing JTextArea didn't show up???
Can you help me to debug my simple code please? The JTextArea displayArea didn't show up :(
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListe ner;
import java.awt.event.ActionEvent ;
public class Display implements ActionListener{
JPanel GUI;
JTextArea displayArea;
JLabel separatorLabel;
JTextField separatorField;
JButton enterButton;
String content = "Hello";
public JPanel createContentPane (){
GUI = new JPanel();
GUI.setLayout(null);
displayArea = new JTextArea(content);
displayArea.setEditable(fa lse);
displayArea.setLineWrap(fa lse);
displayArea.setWrapStyleWo rd(true);
JScrollPane scrollBar = new JScrollPane(displayArea, JScrollPane.VERTICAL_SCROL LBAR_AS_NE EDED,
JScrollPane.HORIZONTAL_SCR OLLBAR_AS_ NEEDED);
scrollBar.setPreferredSize (new Dimension(0, 300));
GUI.add(scrollBar);
separatorLabel = new JLabel("Please enter a character ");
separatorLabel.setForegrou nd(Color.r ed);
separatorLabel.setLocation (20, 525);
separatorLabel.setSize(220 , 40);
GUI.add(separatorLabel);
separatorField = new JTextField(8);
separatorField.setLocation (250, 530);
separatorField.setSize(50, 30);
GUI.add(separatorField);
enterButton = new JButton("Enter");
enterButton.setLocation(31 0, 530);
enterButton.setSize(80, 30);
enterButton.addActionListe ner(this);
GUI.add(enterButton);
GUI.setOpaque(true);
return GUI;
}
public void actionPerformed(ActionEven t e) {
if(e.getSource() == enterButton)
{
System.out.println("ok");
System.exit(0);
}
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFe elDecorate d(true);
JFrame frame = new JFrame("Request message cookies");
Display display = new Display();
frame.setContentPane(displ ay.createC ontentPane ());
frame.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater (new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListe
import java.awt.event.ActionEvent
public class Display implements ActionListener{
JPanel GUI;
JTextArea displayArea;
JLabel separatorLabel;
JTextField separatorField;
JButton enterButton;
String content = "Hello";
public JPanel createContentPane (){
GUI = new JPanel();
GUI.setLayout(null);
displayArea = new JTextArea(content);
displayArea.setEditable(fa
displayArea.setLineWrap(fa
displayArea.setWrapStyleWo
JScrollPane scrollBar = new JScrollPane(displayArea, JScrollPane.VERTICAL_SCROL
JScrollPane.HORIZONTAL_SCR
scrollBar.setPreferredSize
GUI.add(scrollBar);
separatorLabel = new JLabel("Please enter a character ");
separatorLabel.setForegrou
separatorLabel.setLocation
separatorLabel.setSize(220
GUI.add(separatorLabel);
separatorField = new JTextField(8);
separatorField.setLocation
separatorField.setSize(50,
GUI.add(separatorField);
enterButton = new JButton("Enter");
enterButton.setLocation(31
enterButton.setSize(80, 30);
enterButton.addActionListe
GUI.add(enterButton);
GUI.setOpaque(true);
return GUI;
}
public void actionPerformed(ActionEven
if(e.getSource() == enterButton)
{
System.out.println("ok");
System.exit(0);
}
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFe
JFrame frame = new JFrame("Request message cookies");
Display display = new Display();
frame.setContentPane(displ
frame.setDefaultCloseOpera
frame.setSize(800, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater
public void run() {
createAndShowGUI();
}
});
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here take a look at the other LayoutManager options:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
ASKER
Thanks a lot!!!
No problem. You are welcome.
ASKER
Hello mwvisa1,
I am so sorry that can I ask you just one more question? If I want to set the value of the variable content i.e. String content = "Hello"; equal to the argument of args[0] instead of "hello", is it possible to do it? Thanks!
I am so sorry that can I ask you just one more question? If I want to set the value of the variable content i.e. String content = "Hello"; equal to the argument of args[0] instead of "hello", is it possible to do it? Thanks!
Yes it is possible.
If you have difficulty implementing you can start a new Q.
If you have difficulty implementing you can start a new Q.
ASKER