1 year ago
#206304

user18268755
How to bring JTextField to front?
I'm having a problem creating a Java GUI.
I'm creating a simple wordle game just to get the hang of GUI in Java. The idea is the program picks a random 5 letter word from a text file and the user gets 6 guesses to guess the word. If he guesses the correct letter in the right place the background panel will turn green, and if he guesses the correct letter but in the wrong spot it will turn yellow. But I'm having trouble with the JTextField
, both in that it pops up under the panels and in the wrong place (included image). I tried adding the text field in a JPanel
and the panel to the frame but that did not help.
I have no idea why this is happening or how to fix it. Does anyone have any experience with this sort of thing?
Here is the code:
import java.awt.Color;
public class WordleFrame extends JFrame implements ActionListener {
JButton button;
JTextField text;
public boolean isPress;
public String guess;
WordleFrame() {
this.setTitle("Wordle");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(415, 700);
this.setResizable(false);
this.getContentPane().setBackground(new Color(225, 225, 225));
ImageIcon image = new ImageIcon("W.png");
this.setIconImage(image.getImage());
JLabel label = new JLabel();
label.setText("WORDLE");
label.setForeground(new Color(225, 225, 225));
label.setFont(new Font("CVS", Font.BOLD, 40));
label.setOpaque(true);
label.setBackground(new Color(106, 170, 100));
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.TOP);
label.setVerticalTextPosition(JLabel.CENTER);
label.setBounds(100, 5, 200, 60);
addingPanels();
Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
attributes.put(TextAttribute.TRACKING, 2);
text = new JTextField();
text.setBounds(25, 80, 325, 50);
text.setForeground(new Color(47, 47, 47));
text.setOpaque(false);
text.setBorder(BorderFactory.createEmptyBorder());
text.setFont(new Font("Playbox", Font.BOLD, 35).deriveFont(attributes));
text.requestFocusInWindow();
this.add(text);
JButton button = new JButton();
button.setBounds(124, 560, 150, 30);
button.addActionListener(this);
button.setText("Factor guess");
button.setFocusable(false);
button.setFont(new Font("Playbox", Font.BOLD, 13));
button.setForeground(Color.white);
button.setBackground(new Color(99, 99, 99));
getContentPane().setLayout(null);
this.add(label);
this.add(button);
this.setLayout(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String guess = text.getText();
System.out.println(guess);
}
public void addingPanels() {
Border border = BorderFactory.createLineBorder(Color.gray, 4);
JPanel panel = new JPanel();
panel.setBackground(new Color(225, 225, 225));
panel.setFocusable(false);
panel.setBounds(25, 80, 50, 50);
panel.setBorder(border);
this.add(panel);
JPanel panel1 = new JPanel();
panel1.setBackground(new Color(225, 225, 225));
panel1.setBounds(100, 80, 50, 50);
panel1.setBorder(border);
this.add(panel1);
JPanel panel2 = new JPanel();
panel2.setBackground(new Color(225, 225, 225));
panel2.setBounds(175, 80, 50, 50);
panel2.setBorder(border);
this.add(panel2);
JPanel panel3 = new JPanel();
panel3.setBackground(new Color(225, 225, 225));
panel3.setBounds(250, 80, 50, 50);
panel3.setBorder(border);
this.add(panel3);
JPanel panel4 = new JPanel();
panel4.setBackground(new Color(225, 225, 225));
panel4.setBounds(325, 80, 50, 50);
panel4.setBorder(border);
this.add(panel4);
JPanel bpanel = new JPanel();
bpanel.setBackground(new Color(225, 225, 225));
bpanel.setBounds(25, 160, 50, 50);
bpanel.setBorder(border);
this.add(bpanel);
JPanel bpanel1 = new JPanel();
bpanel1.setBackground(new Color(225, 225, 225));
bpanel1.setBounds(100, 160, 50, 50);
bpanel1.setBorder(border);
this.add(bpanel1);
JPanel bpanel2 = new JPanel();
bpanel2.setBackground(new Color(225, 225, 225));
bpanel2.setBounds(175, 160, 50, 50);
bpanel2.setBorder(border);
this.add(bpanel2);
JPanel bpanel3 = new JPanel();
bpanel3.setBackground(new Color(225, 225, 225));
bpanel3.setBounds(250, 160, 50, 50);
bpanel3.setBorder(border);
this.add(bpanel3);
JPanel bpanel4 = new JPanel();
bpanel4.setBackground(new Color(225, 225, 225));
bpanel4.setBounds(325, 160, 50, 50);
bpanel4.setBorder(border);
this.add(bpanel4);
JPanel cpanel = new JPanel();
cpanel.setBackground(new Color(225, 225, 225));
cpanel.setBounds(25, 240, 50, 50);
cpanel.setBorder(border);
this.add(cpanel);
JPanel cpanel1 = new JPanel();
cpanel1.setBackground(new Color(225, 225, 225));
cpanel1.setBounds(100, 240, 50, 50);
cpanel1.setBorder(border);
this.add(cpanel1);
JPanel cpanel2 = new JPanel();
cpanel2.setBackground(new Color(225, 225, 225));
cpanel2.setBounds(175, 240, 50, 50);
cpanel2.setBorder(border);
this.add(cpanel2);
JPanel cpanel3 = new JPanel();
cpanel3.setBackground(new Color(225, 225, 225));
cpanel3.setBounds(250, 240, 50, 50);
cpanel3.setBorder(border);
this.add(cpanel3);
JPanel cpanel4 = new JPanel();
cpanel4.setBackground(new Color(225, 225, 225));
cpanel4.setBounds(325, 240, 50, 50);
cpanel4.setBorder(border);
this.add(cpanel4);
JPanel dpanel = new JPanel();
dpanel.setBackground(new Color(225, 225, 225));
dpanel.setBounds(25, 320, 50, 50);
dpanel.setBorder(border);
this.add(dpanel);
JPanel dpanel1 = new JPanel();
dpanel1.setBackground(new Color(225, 225, 225));
dpanel1.setBounds(100, 320, 50, 50);
dpanel1.setBorder(border);
this.add(dpanel1);
JPanel dpanel2 = new JPanel();
dpanel2.setBackground(new Color(225, 225, 225));
dpanel2.setBounds(175, 320, 50, 50);
dpanel2.setBorder(border);
this.add(dpanel2);
JPanel dpanel3 = new JPanel();
dpanel3.setBackground(new Color(225, 225, 225));
dpanel3.setBounds(250, 320, 50, 50);
dpanel3.setBorder(border);
this.add(dpanel3);
JPanel dpanel4 = new JPanel();
dpanel4.setBackground(new Color(225, 225, 225));
dpanel4.setBounds(325, 320, 50, 50);
dpanel4.setBorder(border);
this.add(dpanel4);
JPanel epanel = new JPanel();
epanel.setBackground(new Color(225, 225, 225));
epanel.setBounds(25, 400, 50, 50);
epanel.setBorder(border);
this.add(epanel);
JPanel epanel1 = new JPanel();
epanel1.setBackground(new Color(225, 225, 225));
epanel1.setBounds(100, 400, 50, 50);
epanel1.setBorder(border);
this.add(epanel1);
JPanel epanel2 = new JPanel();
epanel2.setBackground(new Color(225, 225, 225));
epanel2.setBounds(175, 400, 50, 50);
epanel2.setBorder(border);
this.add(epanel2);
JPanel epanel3 = new JPanel();
epanel3.setBackground(new Color(225, 225, 225));
epanel3.setBounds(250, 400, 50, 50);
epanel3.setBorder(border);
this.add(epanel3);
JPanel epanel4 = new JPanel();
epanel4.setBackground(new Color(225, 225, 225));
epanel4.setBounds(325, 400, 50, 50);
epanel4.setBorder(border);
this.add(epanel4);
JPanel fpanel = new JPanel();
fpanel.setBackground(new Color(225, 225, 225));
fpanel.setBounds(25, 480, 50, 50);
fpanel.setBorder(border);
this.add(fpanel);
JPanel fpanel1 = new JPanel();
fpanel1.setBackground(new Color(225, 225, 225));
fpanel1.setBounds(100, 480, 50, 50);
fpanel1.setBorder(border);
this.add(fpanel1);
JPanel fpanel2 = new JPanel();
fpanel2.setBackground(new Color(225, 225, 225));
fpanel2.setBounds(175, 480, 50, 50);
fpanel2.setBorder(border);
this.add(fpanel2);
JPanel fpanel3 = new JPanel();
fpanel3.setBackground(new Color(225, 225, 225));
fpanel3.setBounds(250, 480, 50, 50);
fpanel3.setBorder(border);
this.add(fpanel3);
JPanel fpanel4 = new JPanel();
fpanel4.setBackground(new Color(225, 225, 225));
fpanel4.setBounds(325, 480, 50, 50);
fpanel4.setBorder(border);
this.add(fpanel4);
}
}
java
swing
layout-manager
jtextfield
null-layout-manager
0 Answers
Your Answer