I am trying to create a JPanel that pops up on the frame when a specific action occurs. The JFrame and JPanel are created in two separate classes and I cannot for the life of me figure out how to properly utilize them. When I run the code right now, when the action occurs, only a tiny JPanel appears in the top corner of the JFrame and it only displays the basic mac exit, minimize, and maximize buttons.
The code with the JFrame
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Gui extends JFrame
{
private JFrame mainFrame = new JFrame("Robber - Main menu");
private JFrame gameFrame = new JFrame("Robber - Game");
private final int WINDOW_SIZE_WIDTH = 905;
private final int WINDOW_SIZE_HEIGHT = 940;
public void setMainFrame()
{
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(900,900);
mainFrame.setResizable(false);
}
public void setGameFrame()
{
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setSize(WINDOW_SIZE_WIDTH, WINDOW_SIZE_HEIGHT);
gameFrame.setResizable(false);
}
public void endGame()
{
NameBox name = new NameBox();
name.setVisible(true);
gameFrame.add(name);
The code with the JPanel is
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NameBox extends JPanel implements KeyListener
{
private String name;
private JTextField userInput = new JTextField(10);
private JPanel centerPanel;
private JLabel centerText;
public NameBox()
{
centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
centerText = new JLabel("<html><center>" +"0"+ "<br /><center>Enter your name</html>" , JLabel.CENTER);
centerText.setFont(new Font("Britannic Bold", Font.PLAIN, 18));
centerText.setForeground(new Color(230,180,14));
centerPanel.add(centerText);
setLocation(327,407);
setSize(250, 125);
userInput.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent evt)
{
if(evt.getKeyCode() == KeyEvent.VK_ENTER)
{
submitAction();
}
}
});
centerPanel.add(userInput);
Box theBox = Box.createVerticalBox();
theBox.add(Box.createVerticalStrut(10));
theBox.add(centerPanel);
add(theBox);
}
public void submitAction()
{
name = userInput.getText();
}
public String getName()
{
return name;
}
public static void main(String[] args)
{
new NameBox().setVisible(true);
}
@Override
public void keyTyped(KeyEvent e){}
@Override
public void keyReleased(KeyEvent e){}
@Override
public void keyPressed(KeyEvent e){}
}
If anyone could help me with my probably very poorly constructed code, that would be amazing!
答案 0 :(得分:1)
A JPanel
can not be display in of itself, it needs to be added to some container, which is, eventually, attached to some kind of window, before it can be displayed on the screen. From what I can understand of our problem, you want to use some kind of dialog, see How to make dialogs for more details
As a rough and ready example...
public void endGame()
{
NameBox name = new NameBox();
JOptionPane.showMessageDialog(gameFrame, name);
gameFrame.add(name);