I use a BorderLayout combined with a FlowLayout, which works perfectly when I set up the BorderLayout.NORTH. However, in the CENTER area I would like to add a JTextArea (to print out console), but when I create a panel, add JTextArea, after adding a panel to BorderLayout.CENTER nothing appears and became grey. I have tried several combinations and tricks, also I have checked several forum posts without luck. Here is my very simplified code (should run flawlessly, I have commented the problematic part if you remove the comment the bug present):
public static void main(String[] args) {
blogGUI();
}
public static void blogGUI() {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(700, 500);
frame.setLayout(new BorderLayout()); //a frame BorderLayout elrendezésü
JPanel panel1 = new JPanel();
//panel.setBounds(0,0,700,500);
panel1.setBackground(new java.awt.Color(255,255,255));
panel1.setVisible(true);
panel1.setSize(new Dimension(140, 30));
JPanel panel2 = new JPanel();
//panel.setBounds(0,0,700,500);
panel2.setBackground(new java.awt.Color(255,255,255));
panel2.setVisible(true);
panel2.setSize(new Dimension(140, 30));
JButton btNewEntry = new JButton("New Post");
JButton btModifyEntry = new JButton("Modify");
JButton btDeleteEntry = new JButton("Delete");
JButton btShowEntries = new JButton("List");
JButton btExit = new JButton("Exit");
JLabel lbFile = new JLabel("Open Blog:");
JLabel lbFilePath = new JLabel("Nothing selected...");
JButton btFileOpen = new JButton("Open");
btNewEntry.setPreferredSize(new Dimension(100,30));
btModifyEntry.setPreferredSize(new Dimension(100,30));
btDeleteEntry.setPreferredSize(new Dimension(100,30));
btShowEntries.setPreferredSize(new Dimension(100,30));
btExit.setPreferredSize(new Dimension(100,30));
lbFile.setPreferredSize(new Dimension(100,30));
lbFilePath.setPreferredSize(new Dimension(310,30));
btFileOpen.setPreferredSize(new Dimension(100,30));
panel1.add(btNewEntry);
panel1.add(btModifyEntry);
panel1.add(btDeleteEntry);
panel1.add(btShowEntries);
panel1.add(btExit);
panel2.add(lbFile);
panel2.add(btFileOpen);
panel2.add(lbFilePath);
JPanel cpanelNorth = new JPanel();
cpanelNorth.setBackground(new java.awt.Color(135,206,250));
cpanelNorth.setLayout(new FlowLayout());
cpanelNorth.setPreferredSize(new Dimension(500, 95));
cpanelNorth.add(panel1);
cpanelNorth.add(panel2);
frame.add(cpanelNorth, BorderLayout.NORTH);
/*Something wrong here! From this point, if this uncommented ruins the gui.*/
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
//panel.setBounds(0,0,700,500);
panel3.setBackground(new java.awt.Color(255,255,255));
//panel3.setVisible(true);
//panel3.setPreferredSize(new Dimension(30, 30));
JTextArea textArea = new JTextArea("Welcome to...!");
//textArea.setPreferredSize(new Dimension(50,50));
textArea.setBackground(new java.awt.Color(255,0,0));
panel3.add(textArea);
frame.add(panel3, BorderLayout.CENTER);
Hi Andrew Thompson and user16320675, Thanks for the quick help, this solved my issue. How can I further improve the quality the code? I had to put only one setPrefferedSize()
at the JTextArea
, all other places I removed (without this the window shrinks to zero)
allsetPrefferedSize()
frame.setVisible(true)
at the end, and also frame.pack()
at the endHere is my code:
package bloggui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Start {
public static void main(String[] args) {
blogGUI();
}
public static void blogGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setBackground(new java.awt.Color(255,255,255));
JPanel panel2 = new JPanel();
panel2.setBackground(new java.awt.Color(255,255,255));
JButton btNewEntry = new JButton("New Post");
JButton btModifyEntry = new JButton("Modify");
JButton btDeleteEntry = new JButton("Delete");
JButton btShowEntries = new JButton("List");
JButton btExit = new JButton("Exit");
JLabel lbFile = new JLabel("Open Blog:");
JLabel lbFilePath = new JLabel("Nothing selected...");
JButton btFileOpen = new JButton("Open");
panel1.add(btNewEntry);
panel1.add(btModifyEntry);
panel1.add(btDeleteEntry);
panel1.add(btShowEntries);
panel1.add(btExit);
panel2.add(lbFile);
panel2.add(btFileOpen);
panel2.add(lbFilePath);
JPanel cpanelNorth = new JPanel();
cpanelNorth.setBackground(new java.awt.Color(135,206,250));
cpanelNorth.setLayout(new FlowLayout());
cpanelNorth.add(panel1);
cpanelNorth.add(panel2);
frame.add(cpanelNorth, BorderLayout.NORTH);
frame.pack();
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.setBackground(new java.awt.Color(135,206,250));
JTextArea textArea = new JTextArea("Welcome to...!");
textArea.setBackground(new java.awt.Color(255,255,255));
textArea.setPreferredSize(new Dimension(620, 400)); //Must not use setPreferredSize() [I must use it only once here!]
panel3.add(textArea);
frame.add(panel3, BorderLayout.CENTER);
frame.pack(); //Must be at the end
frame.setVisible(true); //Must be at the end
}
}