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));
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.
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);
}
I have container jpanel in which i used a boxlayout manager and what i do is i add another panels inside in which the added panel contains a label and textfield using flowlayout manager. everytime i add a panel inside it creates an annoying big space after another added panel. I want to reduce the spacing of the panels i have tried using setsize and setpreferredsize method to adjust it. Here is my code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500,312));
global.setSize(500,312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
//Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
global.add(new JSeparator());
//Name Field
JPanel c = new JPanel();
c.setSize(100, 1);
c.setPreferredSize(new Dimension(100,1));
c.setLayout(new FlowLayout());
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20,25));
c.add(label);
c.add(text);
global.add(c);
//Hostname Field
JPanel c1 = new JPanel();
c1.setSize(100, 1);
c1.setPreferredSize(new Dimension(100,1));
c1.setLayout(new FlowLayout());
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20,25));
c1.add(label2);
c1.add(text1);
global.add(c1);
BoxLayout is a pretty aggressive LayoutManager and doesn't always honour the preferred size of components within it. Instead, we must set the maximum size of BoxLayout components to prevent them from being stretched. Additionally, we need to add a Box via Box.createVerticalGlue() - this is special component that gets stretched (rather than the other components).
Here is the rewritten code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500, 312));
global.setSize(500, 312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
// Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
JSeparator sep = new JSeparator();
sep.setMaximumSize(new Dimension((int) sep.getMaximumSize().getWidth(), 50));
global.add(sep);
// Name Field
JPanel c = new JPanel();
c.setMaximumSize(new Dimension((int) c.getMaximumSize().getWidth(), 50));
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20, 25));
c.add(label);
c.add(text);
global.add(c);
// Hostname Field
JPanel c1 = new JPanel();
c1.setMaximumSize(new Dimension((int) c1.getMaximumSize().getWidth(), 50));
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20, 25));
c1.add(label2);
c1.add(text1);
global.add(c1);
global.add(Box.createVerticalGlue());
There is no errors but when I Run it the content that I added into the JPanel won't appear, only the one not inside the JPanel appear.
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame
{
public static void main(String arg[])
{
SimpleGUI f = new SimpleGUI("GUI components");
f.setSize(600,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
SimpleGUI(String s)
{
setTitle(s);
setLayout(new GridLayout(3,2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel ("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel ("Enter age:");
JTextField tf2= new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold",true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5,20);
JList list = new JList(new Object[] {"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play",ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
}
}
I've updated your code. Have a look at this version:
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame {
public static void main(String arg[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SimpleGUI f = new SimpleGUI("GUI components");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public SimpleGUI(String s) {
setTitle(s);
setLayout(new GridLayout(3, 2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel("Enter age:");
JTextField tf2 = new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold", true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5, 20);
JList list = new JList(new Object[]{"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play", ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
/**
* Need to add the following lines
*/
this.add(p1);
this.add(p2);
this.add(p3);
this.pack();
this.setVisible(true);
}
}
A couple of pointers:
You need to add your components to your JFrame for them to actually show up.
Any updates to the user interface must happen on the event dispatch thread. Consequently you would notice that I've added a SwingUtilites.invokeLater() to the main. Have a look at this article to understand "Threading with Swing"
Where you you add your panels to the frame? Also, forgotten my java "rules and regulations": do you need to call "super()"?