JPanel gets compressed and disappears when trying to move to the bottom - java

I have to do a project in Java and thought a GUI Text Adventure would be cool. My Problem is that when I create a JPanel and move it further down on the screen, the panel first changes its size and then disappears completely at one point.
On the GameScreen there should be a panel for choice Options to be put on but it refuses to go further down than about half the size of the Screen.
Here's the code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Yeet {
JFrame epicOfYeet;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
JButton startButton;
JButton choice1;
JButton choice2;
JButton choice3;
JButton choice4;
JTextArea mainTextArea;
TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
new Yeet();
}
public Yeet() {
epicOfYeet = new JFrame();
epicOfYeet.setSize(1200, 1000);
epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epicOfYeet.getContentPane().setBackground(Color.black);
epicOfYeet.setLayout(null);
con = epicOfYeet.getContentPane();
titleNamePanel = new JPanel();
titleNamePanel.setBounds(190, 100, 800, 230);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("EPIC OF YEET");
titleNameLabel.setForeground(Color.red);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(400, 500, 400, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel);
con.add(startButtonPanel);
epicOfYeet.setVisible(true);
}
public void createGameScreen(){
titleNamePanel.setVisible(false);
startButtonPanel.setVisible(false);
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 1000, 400);
mainTextPanel.setBackground(Color.green);
con.add(mainTextPanel);
mainTextArea = new JTextArea("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
mainTextArea.setBounds(100, 100, 1000, 250);
mainTextArea.setBackground(Color.blue);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
choiceButtonPanel.setBounds(300, 500, 600, 550);
choiceButtonPanel.setBackground(Color.red);
con.add(choiceButtonPanel);
}
public class TitleScreenHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
}

Here is a basic implementation using layout mangers, avoiding the bad practice of null layout manager.
It is not an optimal one, but should get you started:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Yeet {
JFrame epicOfYeet;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
JButton startButton, coice1, choice2, choice3, choice4;
JTextArea mainTextArea;
TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
SwingUtilities.invokeLater(()->new Yeet());
}
public Yeet() {
epicOfYeet = new JFrame();
//epicOfYeet.setSize(1200, 1000); // do not set size. let layout manager calcualte it
epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epicOfYeet.getContentPane().setBackground(Color.black);
//epicOfYeet.setLayout(null); //aviod null layouts Jframe uses BorderLayout by default
con = epicOfYeet.getContentPane();
titleNamePanel = new JPanel();
//titleNamePanel.setBounds(190, 100, 800, 230);
titleNamePanel.setPreferredSize(new Dimension(800, 230));//set preferred size to be used by layout manager
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("EPIC OF YEET");
titleNameLabel.setForeground(Color.red);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
//startButtonPanel.setBounds(400, 500, 400, 100);
startButtonPanel.setPreferredSize(new Dimension(400, 100));
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel, BorderLayout.PAGE_START); //set to top
con.add(startButtonPanel, BorderLayout.PAGE_END); //set to bottom
epicOfYeet.pack();
epicOfYeet.setVisible(true);
}
public void createGameScreen(){
//titleNamePanel.setVisible(false);
//startButtonPanel.setVisible(false);
con.remove(titleNamePanel);
con.remove(startButtonPanel);
mainTextPanel = new JPanel();
//mainTextPanel.setBounds(100, 100, 1000, 400);
mainTextPanel.setBackground(Color.green);
con.add(mainTextPanel); // added to center position of the BorderLayout (the default)
mainTextArea = new JTextArea(10, 20); //size in rows, cols
mainTextArea.setText("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
//mainTextArea.setBounds(100, 100, 1000, 250);
mainTextArea.setBackground(Color.blue);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
//choiceButtonPanel.setBounds(300, 500, 600, 550);
choiceButtonPanel.setPreferredSize(new Dimension(600, 550));
choiceButtonPanel.setBackground(Color.red);
con.add(choiceButtonPanel, BorderLayout.PAGE_END);//add to bottom
epicOfYeet.pack();
}
public class TitleScreenHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
}

Related

Why doesn't a panel with GridBagLayout show the content?

I wrote a little something here. It's working if I don't backPanel.setLayout(new GridBagLayout);
But without the grid bag, the content stays in the top left I maximise the screen.
With the grid bag I only get the red backPanel in the frame. Well, there is a gray pixel in the middle of the screen. I'm assuming that's my panel, but I can't make it bigger. I tried setSize but it doesn't change. Also, I had the panel.setBounds(0, 0, getWidth(),getHeight());. I'm not sure why I removed it.
My main is in the other file. The only thing it does at the moment is to call the LoginFrame.
Here is the code:
package first;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class LoginFrame extends JFrame implements ActionListener {
private JTextField textField;
private JPasswordField passwordField;
public LoginFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 300);
JPanel backPanel = new JPanel(new GridBagLayout());
backPanel.setBackground(Color.RED);
JPanel panel = new JPanel();
panel.setSize(500, 300);
panel.setBackground(Color.LIGHT_GRAY);
panel.setLayout(null);
JLabel label;
panel.add(label = new JLabel("Username:"));
label.setBounds(20, 100, 100, 25);
panel.add(textField = new JTextField());
textField.setBounds(140, 100, 200, 25);
panel.add(label = new JLabel("Password:"));
label.setBounds(20, 145, 100, 25);
panel.add(passwordField = new JPasswordField());
passwordField.setBounds(140, 145, 200, 25);
panel.add(label = new JLabel("CTC Bank"));
label.setFont(new Font("New Times Roman", Font.BOLD, 50));
label.setBounds(0, 0, getWidth(), 100);
label.setHorizontalAlignment(JLabel.CENTER);
JButton button;
panel.add(button = new JButton("Login"));
button.setBounds(140, 200, 100, 25);
button.addActionListener(this);
button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);
panel.add(button = new JButton("Register"));
button.setBounds(240, 200, 100, 25);
button.addActionListener(this);
button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);
//add(panel);
backPanel.add(panel);
add(backPanel, BorderLayout.CENTER);
revalidate();
repaint();
setLocationRelativeTo(null);
setVisible(true);
}
public static JButton defaultActionKeyEnter(JButton button, int desiredKeyCode) {
InputMap inputMap = button.getInputMap(JComponent.WHEN_FOCUSED);
KeyStroke spaceKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false);
KeyStroke spaceKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true);
KeyStroke desiredKeyPressed = KeyStroke.getKeyStroke(desiredKeyCode, 0, false);
KeyStroke desiredKeyReleased = KeyStroke.getKeyStroke(desiredKeyCode, 0, true);
inputMap.put(desiredKeyPressed, inputMap.get(spaceKeyPressed));
inputMap.put(desiredKeyReleased, inputMap.get(spaceKeyReleased));
inputMap.put(spaceKeyPressed, "none");
inputMap.put(spaceKeyReleased, "none");
return button;
}
// Unfinished code dont worry bout it...
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Login")) {
if (textField.getText().equals("Heinz")
&& (new String(passwordField.getPassword()).equals("password123"))) {
// color = Color.GREEN;
} else {
JOptionPane.showMessageDialog(this, "Wrong Username or Password", "Error", JOptionPane.WARNING_MESSAGE);
// color = Color.RED;
}
} else {
JOptionPane.showMessageDialog(this, "Cya");
dispose();
setVisible(false);
}
// panel.setBackground(color);
}
}
I have seen questions about this but none of the answers were helpful in my case.
Calling the following didn't help.
revalidate();
repaint();
Did I maybe add it in the wrong order?
And how does the code look like to you? Would you consider this clean?
The layout of backPanel will mis-calculate the dimensions of "panel" because "panel" does not participate in layout management properly, without a layout manager of its own.
One solution to this is to use setLayout(null) also on the "backPanel", or add "panel" directly to the JFrame.
With the first suggestion ("backPanel.setLayout(null);" just after it is created), plus the following main method:
public static void main(String[] args) {
new LoginFrame();
}
I get this:

Having a JTextField popup at a certain location after clicking a JButton

I have my three JButtons located where I want them (at the top center of the frame), and when the user clicks one, a JTextField pops up in the BoxLayout like wanted.
The problem is, when the JTextField shows up, it is to the left of the buttons, and it moves them.
I tried setting the alignment of the JTextField and using various glues, but the JTextField doesn't move.
If I want to have the JTextField pop up below my JButtons and in the center of the screen, what should I use?
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Library extends JFrame implements ActionListener {
private JFrame jf1;
private JPanel jp1;
private JTextField jtf1;
private JButton jb1;
private JButton jb2;
private JButton jb3;
public Library() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception q) {
q.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(false);
jf1 = new JFrame("Library");
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setSize(1080, 900);
jf1.setResizable(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);
jp1 = (JPanel) jf1.getContentPane();
jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));
jb1 = new JButton("Genre");
jb1.addActionListener(this);
jb1.setMinimumSize(new Dimension(140, 60));
jb1.setPreferredSize(new Dimension(150, 60));
jb1.setMaximumSize(new Dimension(150, 60));
jb1.setAlignmentY(-70.0f);
jb2 = new JButton("Author");
jb2.addActionListener(this);
jb2.setMinimumSize(new Dimension(140, 60));
jb2.setPreferredSize(new Dimension(150, 60));
jb2.setMaximumSize(new Dimension(150, 60));
jb2.setAlignmentY(-70.0f);
jb3 = new JButton("Title");
jb3.addActionListener(this);
jb3.setMinimumSize(new Dimension(140, 60));
jb3.setPreferredSize(new Dimension(150, 60));
jb3.setMaximumSize(new Dimension(150, 60));
jb3.setAlignmentY(-70.0f);
jp1.add(Box.createHorizontalGlue());
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
jp1.add(Box.createHorizontalGlue());
jf1.validate();
}
#Override
public void actionPerformed(ActionEvent e) {
Object code = e.getSource();
if (code == jb1) {
jtf1 = new JTextField("Enter Text");
jtf1.setPreferredSize(new Dimension(200,20));
jtf1.setMaximumSize(new Dimension(200,20));
jtf1.setMinimumSize(new Dimension(10,10));
jp1.add(jtf1);
jp1.validate();
}
else if (code == jb2) {
}
else if (code == jb3) {
}
}
public static void main(String[] args) {
Library shoe = new Library();
}
}
Suggestion: do not add/remove UI elements dynamically. Just add all of those things initially, and simply call setVisible(false) on your text field then.
(instead of adding/removing fields using your action listener)
A good and easy solution may be to use another JPanel to hold your dynamically created text fields. This may be what you wanted:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Library extends JFrame implements ActionListener {
private JFrame jf1;
private JPanel jp1;
private JPanel jp2;
private JTextField jtf1;
private JButton jb1;
private JButton jb2;
private JButton jb3;
public Library() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception q) {
q.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(false);
jf1 = new JFrame("Library");
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setSize(1080, 900);
jf1.setResizable(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);
jp1 = new JPanel();
jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));
jp2 = new JPanel();
jp2.setLayout(new BoxLayout(jp2, BoxLayout.LINE_AXIS));
jb1 = new JButton("Genre");
jb1.addActionListener(this);
jb1.setMinimumSize(new Dimension(140, 60));
jb1.setPreferredSize(new Dimension(150, 60));
jb1.setMaximumSize(new Dimension(150, 60));
jb1.setAlignmentY(-70.0f);
jb2 = new JButton("Author");
jb2.addActionListener(this);
jb2.setMinimumSize(new Dimension(140, 60));
jb2.setPreferredSize(new Dimension(150, 60));
jb2.setMaximumSize(new Dimension(150, 60));
jb2.setAlignmentY(-70.0f);
jb3 = new JButton("Title");
jb3.addActionListener(this);
jb3.setMinimumSize(new Dimension(140, 60));
jb3.setPreferredSize(new Dimension(150, 60));
jb3.setMaximumSize(new Dimension(150, 60));
jb3.setAlignmentY(-70.0f);
jp1.add(Box.createHorizontalGlue());
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
jp1.add(Box.createHorizontalGlue());
jf1.getContentPane().add(jp1);
jp1.setBounds(0, 0, 1080, 900);
jf1.getContentPane().add(jp2);
jp2.setBounds(0, 0, 1080, 900);
validate();
}
public void actionPerformed(ActionEvent e) {
Object code = e.getSource();
if (code == jb1) {
jtf1 = new JTextField("Enter Text");
jtf1.setPreferredSize(new Dimension(200,20));
jtf1.setMaximumSize(new Dimension(200,20));
jtf1.setMinimumSize(new Dimension(10,10));
jp2.add(Box.createHorizontalGlue());
jp2.add(jtf1);
jp2.add(Box.createHorizontalGlue());
jp2.validate();
}
else if (code == jb2) {
}
else if (code == jb3) {
}
}
public static void main(String[] args) {
Library shoe = new Library();
}
}
I gave it a quick try to solve the issue. Hope it helps. Do more tweaking in your code to perfectly match the layout.

Not adding Card Layout in JFrame

Can anyone see the code ? I want to make a page that has a banner and a pannel in which cards will change on the requirement. I added the Banner in JFrame (That is working fine) but The problem is that " CardLayout Panel is not adding in the JFrame".
Actually, I need this.
When button is pressed only card1 change to card2 but banner will remain same.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class gui extends JFrame{
private static final long serialVersionUID = 1L;
JPanel
basic_panel,
card_Layout_panel,
banner_panel,
welcome_authenticaion_panel_card1;
CardLayout basic2;
JLabel
logo_label,
name_label;
public gui(){
server_login_gui();
add(basic_panel);
standard_gui();
}
public void server_login_gui(){
basic_panel = new JPanel();
basic_panel.setLayout(null);
basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
banner_panel = new JPanel();
banner_panel.setLayout(null);
banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
banner_panel.setSize(680, 200);//(400,100,400,100);
//////Banner inner things//////////////////////////////////////////////////
logo_label = new JLabel("Logo");
logo_label.setBounds(30,40,100,100);
logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(logo_label);
name_label = new JLabel(" Name..... ");
name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC,25));
name_label.setBounds(200,80,400,50);
name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(name_label);
////////////////////////////////////////////////////////////////////////
// basic_panel.add(banner_panel,BorderLayout.NORTH);
///////// Card Layout//////////////
basic2 = new CardLayout();
card_Layout_panel = new JPanel(basic2);
card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
basic_panel.add(card_Layout_panel,BorderLayout.CENTER);
welcome_authenticaion_panel_card1 = new JPanel();
welcome_authenticaion_panel_card1.setLayout(null);
welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
welcome_authenticaion_panel_card1.setBounds(0,200,680,460);
card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
basic_panel.add(card_Layout_panel,BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////
}
public void standard_gui(){
setSize(700,700);
setTitle("System");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
I want to make a page that has a banner and a pannel in which cards
will change on the requirement.
your component aren't focusable, there is required some event (JButton, Swing Timer) for switching the view by using CardLayout
for more info about CardLayout to read Oracle tutorial, for working code exampes, tons code examples are here
you code works without NullLayout (by set BorderLayout to parent JPanel), default LayoutManager for Jpanel is FlowLayout (accepts only getPreferredSize, childs aren't resizable with its parent/s)
my question is for why reason is there code line basic_panel.add(card_Layout_panel, BorderLayout.CENTER); twice, and another ...
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Gui extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel basic_panel, card_Layout_panel,
banner_panel, welcome_authenticaion_panel_card1;
private CardLayout basic2;
private JLabel logo_label, name_label;
public Gui() {
server_login_gui();
add(basic_panel);
standard_gui();
}
public void server_login_gui() {
basic_panel = new JPanel();
basic_panel.setLayout(new BorderLayout(10, 10));
basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
banner_panel = new JPanel();
//banner_panel.setLayout(null);
banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
banner_panel.setSize(680, 200);//(400,100,400,100);
//////Banner inner things//////////////////////////////////////////////////
logo_label = new JLabel("Logo");
//logo_label.setBounds(30, 40, 100, 100);
logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(logo_label);
name_label = new JLabel(" Name..... ");
name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC, 25));
//name_label.setBounds(200, 80, 400, 50);
name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(name_label);
////////////////////////////////////////////////////////////////////////
basic_panel.add(banner_panel, BorderLayout.NORTH);
///////// Card Layout//////////////
basic2 = new CardLayout();
card_Layout_panel = new JPanel(basic2);
card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
welcome_authenticaion_panel_card1 = new JPanel();
welcome_authenticaion_panel_card1.setLayout(null);
welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
//welcome_authenticaion_panel_card1.setBounds(0, 200, 680, 460);
card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////
}
public void standard_gui() {
setSize(700, 700);
setTitle("System");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Gui();
}
});
}
}
you're doing this basic_panel.add(card_Layout_panel,BorderLayout.CENTER); twice, hence the error. ( check before and after the welcome_authentication_panel_card )

Adding 2 JLists to a JPanel using BorderLayout

I need to add 2 JLists inside a JPanel for a crossword. The JPanel is located SOUTH and I'm using BorderLayout in the constructor to locate the JPanel.
The problem is, I can't see the 2 JLists inside the JPanel
package crossword;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame implements MouseListener {
Container container;
//this is the constructor
public Main(){
super("Crossword");
container = getContentPane();
container.setLayout(new BorderLayout(1, 1));
container.setBackground(new Color(91,119,162));
container.add(login(),BorderLayout.NORTH);
container.add(buttons(),BorderLayout.WEST);
container.add(clues(),BorderLayout.SOUTH);
container.add(createMatrix(crosswordPanel()),BorderLayout.CENTER);
container.add(eastPanel(),BorderLayout.EAST);
}
public JPanel login(){
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout(1, 15, 5));
loginPanel.setPreferredSize(new Dimension(8, 40));
loginPanel.setBackground(new Color(47,47,47));
loginPanel.setVisible(true);
JTextField inputUserName = new JTextField(15);
loginPanel.add(inputUserName);
JButton btnEnter = new JButton("Play!");
btnEnter.setPreferredSize(new Dimension(80,25));
loginPanel.add(btnEnter);
return loginPanel;
}
public JPanel buttons(){
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout(5, 45, 15));
buttonsPanel.setPreferredSize(new Dimension(250, 200));
buttonsPanel.setBackground(new Color(91,119,162));
buttonsPanel.setVisible(true);
JButton newGame = new JButton("New game");
newGame.setPreferredSize(new Dimension(130,27));
newGame.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(newGame);
JButton showChar = new JButton("Reveal letter");
showChar.setPreferredSize(new Dimension(130,27));
showChar.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(showChar);
JButton showWord = new JButton("Reveal word");
showWord.setPreferredSize(new Dimension(130,27));
showWord.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(showWord);
JButton verifyWord = new JButton("Verify");
verifyWord.setPreferredSize(new Dimension(130,27));
verifyWord.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(verifyWord);
JButton revealAll = new JButton("Solution");
revealAll.setPreferredSize(new Dimension(130,27));
revealAll.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(revealAll);
JLabel labelScore = new JLabel("Score:");
labelScore.setFont(new Font("Arial", Font.BOLD, 13));
buttonsPanel.add(labelScore);
return buttonsPanel;
}
public JPanel clues(){
JPanel clues = new JPanel();
clues.setLayout(new FlowLayout(3, 1, 1));
clues.setPreferredSize(new Dimension(200, 200));
clues.setBackground(new Color(91,119,162));
clues.setVisible(true);
JList horizontalList = new JList();
horizontalList.setBackground(Color.BLUE);
horizontalList.setBorder(BorderFactory.createEtchedBorder(1));
clues.add(horizontalList);
JList verticalList = new JList();
verticalList.setBackground(Color.ORANGE);
verticalList.setBorder(BorderFactory.createEtchedBorder(1));
clues.add(verticalList);
return clues;
}
public JPanel eastPanel(){
JPanel fill = new JPanel();
fill.setLayout(new FlowLayout(5, 25, 15));
fill.setPreferredSize(new Dimension(50, 200));
fill.setBackground(new Color(91,119,162));
fill.setVisible(true);
return fill;
}
public JPanel crosswordPanel(){
JPanel crosswordPanel = new JPanel();
crosswordPanel.setLayout(new GridLayout(10,10,0,0));
crosswordPanel.setBackground(Color.white);
//crosswordPanel.setPreferredSize(new Dimension(200, 200));
crosswordPanel.setBackground(new Color(91,119,162));
crosswordPanel.setVisible(true);
return crosswordPanel;
}
public JPanel createMatrix(JPanel crosswordPanel){
JPanel crosswordMatrix [][] = new JPanel [10][10];
for (int i = 0; i < crosswordMatrix.length ; i++) {
for (int j = 0; j < crosswordMatrix[0].length; j++) {
crosswordMatrix[i][j] = new JPanel();
crosswordMatrix[i][j].setBackground(Color.lightGray);
crosswordMatrix[i][j].setBorder(BorderFactory.createLineBorder(Color.black, 1));
crosswordMatrix[i][j].addMouseListener(this);
//crosswordMatrix[i][j].add(label);
crosswordPanel.add(crosswordMatrix[i][j]);
}
}
return crosswordPanel;
}
public static void main(String[] args) {
Main guiWindow = new Main();
Toolkit screenSize = Toolkit.getDefaultToolkit();
int width = screenSize.getScreenSize().width * 4/5;
int height = screenSize.getScreenSize().height * 4/5;
guiWindow.setSize(width, height);
guiWindow.setLocationRelativeTo(null);
guiWindow.setVisible(true);
guiWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Its not an MCVE, but based upon the limited code you posted: in the clues method a JPanel is constructed, yet nothing is ever added to it. Rather, you add the JLists directly to the object which contains the method (which I presume this extends JFrame). Perhaps try adding the components to the JPanel
public JPanel clues(){
JPanel clues = new JPanel();
...
clues.add(horizontalList);
....
clues.add(verticalList);
}

Why isn't anything showing up in my JFrame?

Why is my GUI not showing any buttons, labels, or text fields?
I think I have it all setup, but when I run it, only the frame shows, and none of the contents appear.
package BasicGame;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Gui extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel label;
private JTextField textField;
private JButton button;
private buttonHandler bHandler;
public Gui(){
setTitle("Basic Gui");
setResizable(false);
setSize(500, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(null);
button = new JButton("button");
button.setBounds(50, 60, 50, 70);
bHandler = new buttonHandler();
button.addActionListener(bHandler);
label = new JLabel("Hello", SwingConstants.RIGHT);
label.setBounds(50, 60, 50, 70);
textField = new JTextField(10);
textField.setBounds(50, 60, 50, 70);
pane.add(button);
pane.add(label);
pane.add(textField);
}
public class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
#SuppressWarnings("unused")
public static void main(String[] args){
Gui gui = new Gui();
}
}
Move your setVisible() to the end of the constructor. You are adding all of your components after you set your JFrame up and make is visible, so you don't see any of the changes.
This should show your JFrame with all the components:
public Gui(){
setTitle("Basic Gui");
setResizable(false);
setSize(500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(null);
button = new JButton("button");
button.setBounds(50, 60, 50, 70);
bHandler = new buttonHandler();
button.addActionListener(bHandler);
label = new JLabel("Hello", SwingConstants.RIGHT);
label.setBounds(50, 60, 50, 70);
textField = new JTextField(10);
textField.setBounds(50, 60, 50, 70);
pane.add(button);
pane.add(label);
pane.add(textField);
setVisible(true); // Move it to here
}
Here's what the frame's looked like before and after I moved the setVisible statement and compiled your code.
Before:
After:

Categories

Resources