Reputation:
I want to add a JTextField to the north region of a frame and a panel (which holds the copy of the same JTextField) to the east region.
But only the field in the north region appears. The panel in the east region is there but the problem is that the field isn't in the panel.
import java.awt.*;
import javax.swing.*;
public class Gui {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().setBackground(Color.BLACK);
panel.setBackground(Color.YELLOW);
JTextField field = new JTextField("Your name");
panel.add(field);
frame.add(BorderLayout.NORTH, field);
frame.add(BorderLayout.EAST, panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Upvotes: 1
Views: 385
Reputation: 3557
You need to have two seperate JTextField
objects if you want two seperate fields: try adding
JTextField field2 = new JTextField("Your second field");
and change the first add()
call on your frame
to
frame.add(BorderLayout.NORTH, field2);
This will produce
which I assume is what you want.
Upvotes: 3