I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}
Related
import java.awt.*;
import javax.swing.*;
public class guiAs {
public static void main (String args [] )
{
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension (300,300));
frame.setTitle("Calculator");
JPanel panel = new JPanel();
// JLabel label = new JLabel();
//creating num1
JPanel panel2 = new JPanel();
JLabel lable1 = new JLabel("Number 1 ");
JTextField tf1 = new JTextField();
//creating num2
JLabel lable2 = new JLabel("Number 2 ");
JTextField tf2 = new JTextField();
//creating result
JPanel panel3 = new JPanel();
JLabel lable3 = new JLabel("Result: ");
JTextField tf3 = new JTextField(10);
//creating button
JButton Add= new JButton("Add");
JButton Subtract = new JButton("Subtract");
JButton Multiply = new JButton("Multiply");
JButton Division = new JButton("Division");
//creating num1
panel2.add(lable1);
panel2.add(tf1);
//creating num2
panel2.add(lable2);
panel2.add(tf2);
//creating result
panel3.add(lable3);
panel3.add(tf3);
//creating buttons
panel.add(Add);
panel.add(Subtract);
panel.add(Multiply);
panel.add(Division);
// frame.getContentPane().add(BorderLayout.WEST, panel3);
//creating Box Layout for num1 and num2
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
frame.add(panel2);
//creating Box Layout for buttons
BoxLayout layout1 = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(layout1);
frame.setLayout(new FlowLayout());
frame.add(panel);
// Add.setAlignmentX(Component.RIGHT_ALIGNMENT);
// panel.add(Add);
// frame.getContentPane().add(BorderLayout.SOUTH, panel);
//creating Border Layout for num1 and num2
frame.getContentPane().add(BorderLayout.WEST, panel2);
//creating Border Layout for Buttons
frame.getContentPane().add(BorderLayout.EAST, panel);
//creating Box Layout for Result
frame.getContentPane().add(BorderLayout.SOUTH, panel3);
frame.setVisible(true);
}
}
this is my code, and I have no idea to make the text field next to the numbers. i try several times but does not comes together. whenever i add something the panels moves
its work only with the result but for the number doesn't work
here i used Box Layout for buttons and number 1 and number 2:
You were very close to solving it.
Replace this line
JPanel panel2 = new JPanel();
With this line
JPanel panel2 = new JPanel(new GridLayout(2, 2));
Then, get rid of these 3 lines
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
Now your code looks like this
I want to add labels and buttons above and below the border layout. How can I do that? Here is what I did:
import java.awt.*;
import javax.swing.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.NORTH);
panel1.add(button2,BorderLayout.SOUTH);
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.add(panel1);
frame.pack();
}
}
Above and below of border layout, set new 2 containers (for example JPanel) and make them flow layout. enter image description here
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
add something like that before frame.pack(); code.
Actually what do you mean by above and below? Do you mean north and south? If It is you should have something like this enter image description here
and you should write code this way
import java.awt.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.getContentPane().add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.getContentPane().add(panel1);
JPanel panel = new JPanel();
panel1.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JPanel panel_1 = new JPanel();
panel1.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel_1.add(rdbtnNewRadioButton_1);
frame.pack();
}
}
In two conditions, you should add two containers into your code and make them flow layout.
Below is the image that will clearly define my problem while using GridLayout
private void init() {
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField loginUNTextField = new JTextField();
JTextField loginPTextField = new JTextField();
JTextField registerUNTextField = new JTextField();
JTextField registerPTextField = new JTextField();
JTextField registerETextField = new JTextField();
JButton login = new JButton("Login");
JButton register = new JButton("Register");
JLabel loginUsername = new JLabel("Username");
JLabel loginPassword = new JLabel("Password");
JLabel registerUsername = new JLabel("Username");
JLabel registerPassword = new JLabel("Password");
JLabel registerEmail = new JLabel("Email");
JPanel loginUNPanel = new JPanel(new GridLayout(3,2));
loginUNPanel.add(loginUsername);
loginUNPanel.add(loginUNTextField);
loginUNPanel.add(loginPassword); // Cause Problem
loginUNPanel.add(loginPTextField);
loginUNPanel.add(new JLabel("")); // Cause Problem
loginUNPanel.add(login);
JPanel registerUNPanel = new JPanel(new GridLayout(4,2));
registerUNPanel.add(registerUsername);
registerUNPanel.add(registerUNTextField);
registerUNPanel.add(registerPassword);
registerUNPanel.add(registerPTextField);
registerUNPanel.add(registerEmail);
registerUNPanel.add(registerETextField);
registerUNPanel.add(new JLabel(""));
registerUNPanel.add(register);
loginUNPanel.add(loginPassword);
JPanel main = new JPanel(new GridLayout(1,2));
main.add(loginUNPanel);
main.add(registerUNPanel);
frame.add(main);
frame.pack();
frame.setVisible(true);
}
Desire Result:
UserName | TextField
Passowrd | TextField
| LoginButon
Please let me know where I am worng using GridLayout.
You're adding a component twice to the container.
JPanel loginUNPanel = new JPanel(new GridLayout(3,2));
loginUNPanel.add(loginUsername);
loginUNPanel.add(loginUNTextField);
loginUNPanel.add(loginPassword); // ***** adding it once *****
loginUNPanel.add(loginPTextField);
loginUNPanel.add(new JLabel(""));
loginUNPanel.add(login);
JPanel registerUNPanel = new JPanel(new GridLayout(4,2));
registerUNPanel.add(registerUsername);
registerUNPanel.add(registerUNTextField);
registerUNPanel.add(registerPassword);
registerUNPanel.add(registerPTextField);
registerUNPanel.add(registerEmail);
registerUNPanel.add(registerETextField);
registerUNPanel.add(new JLabel(""));
registerUNPanel.add(register);
loginUNPanel.add(loginPassword); // ***** adding it again. *****
JPanel main = new JPanel(new GridLayout(1,2));
Get rid of that 2nd addition:
// loginUNPanel.add(loginPassword); // *** this
JPanel main = new JPanel(new GridLayout(1,2));
I've been creating a program for login screen with JFrame and I seem to get long bars of login and cancel(using grid layout(2,1,10,10). so I've created small buffer labels within grid layout and changed it to 2,3,10,10 with 4 buffers in the grid. I've used that because the setPreferredSize syntax wasn't working. can you give me the working PrefferedSize syntax for Login and cancel?
here's my initialization code with the buffer labels
public class access implements ActionListener
{
boolean flag=false;
JFrame frame;
JTextField username;
JPasswordField password;
JButton login,clear;
JPanel panel1,panel2,panel3;
JLabel user,pass,output,b1,b2,b3,b4;
String[] details;
access()
{
frame = new JFrame("authentication ");
panel1= new JPanel();
panel2= new JPanel();
panel3= new JPanel();
username=new JTextField(10);
password=new JPasswordField(10);
b1=new JLabel("");
b2=new JLabel("");
b3=new JLabel("");
b4=new JLabel("");
user= new JLabel("username : ");
pass= new JLabel("password : ");
output=new JLabel(" ");
login=new JButton("login");
login.setPreferredSize(new Dimension(40, 10));
clear= new JButton("clear");
clear.setPreferredSize(new Dimension(100,40));
login.addActionListener(this);
clear.addActionListener(this);
initialize();
}
public void initialize()
{
panel1.setLayout(new GridLayout(2,2,10,10));
panel1.add(user);
panel1.add(username);
panel1.add(pass);
panel1.add(password);
panel2.setLayout(new GridLayout(2,3,5,5));
panel2.add(b1);
panel2.add(login);
panel2.add(b2);
panel2.add(b3);
panel2.add(clear);
panel2.add(b4);
panel3.setLayout(new FlowLayout());
panel3.add(output);
frame.setLayout(new GridLayout(3,1,10,10));
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.getContentPane().add(panel3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,180);
frame.setVisible(true);
}
GridLayout ignores preferred size. Use another LayoutManager e.g. GridBagLayout
Can you please tell me why the scroll doesn't work. It is visible, but it doesn't work,
here you can view that piece of code. What could be the missing part?
// GUI elements
private JTextField textSend = new JTextField(20);
private JTextArea textArea = new JTextArea(5, 20);
private JScrollPane scroll = new JScrollPane(textArea);
private JButton buttonConnect = new JButton("Connect");
private JButton buttonSend = new JButton("Send");
private JButton buttonDisconnect = new JButton("Disconnect");
private JButton buttonQuit = new JButton("Quit");
private JPanel leftPanel = new JPanel();
private JPanel rightPanel = new JPanel();
private JLabel empty = new JLabel("");
ChessHeroChatClient() {
setTitle("ChessHero Chat Client");
setLocationRelativeTo(null);
setSize(500, 500);
setResizable(false);
leftPanel.setLayout(new BorderLayout());
rightPanel.setLayout(new GridLayout(6, 1));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
leftPanel.add(textSend, BorderLayout.NORTH);
leftPanel.add(textArea, BorderLayout.WEST);
leftPanel.add(scroll, BorderLayout.EAST);
add(leftPanel, BorderLayout.CENTER);
add(rightPanel, BorderLayout.EAST);
textArea.setEditable(false);
private JTextArea textArea = new JTextArea(5, 20);
private JScrollPane scroll = new JScrollPane(textArea);
You create the scrollPane with the textArea which is good.
//leftPanel.add(textArea, BorderLayout.WEST); // this is wrong
leftPanel.add(scroll, BorderLayout.EAST);
But then you add the textArea to the WEST and the scroll to the EAST, which is wrong. A Swing component can only have a single parent, so just leave the textArea alone and add the scrollPane to the EAST or WEST.