I want to place two labels in the middle of the center of a window. I get it working with 1 label and the following code:
Screenshot: http://abload.de/img/scr1g6u0f.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("center1");
centerPanel.add(label, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Now I want another label next to the first label. I tried to use a flowlabel, but they are placed at the top of the BorderLayout.CENTER
Screenshot: http://abload.de/img/scr2a3u26.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Thanks!
Use a GridBagLayout without constraints:
JPanel centerPanel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel);
Related
I made a JPanel, which has 6 string JLabels, and one image JLabel. What I need is to be able to position the Jlabel strings somewhere inside of my image JLabel's bounds. I'm pretty sure my problem would be solved if I knew how to resize JLabels.
Some Code:
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("rainbow seizure");
frame.setResizable(false);
frame.setSize(500, 500);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.SOUTH);
JButton newCardButton = new JButton("New Card");
newCardButton.addActionListener(actionEvent -> {
JPanel panel2 = new JPanel();
panel2.setLayout(new OverlayLayout(panel2));
JLabel jImage = new JLabel(new ImageIcon("C:\\Users\\unkno\\java\\Card1.PNG"));
Dimension dimension = jImage.getPreferredSize(); // dimensions for me are 72x122
System.out.println(dimension.width);
System.out.println(dimension.height);
JLabel label1 = new JLabel("name");
label1.setBounds(new Rectangle(dimension.width, dimension.height)); // doesn't do anything :(
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label1.setAlignmentY(Component.TOP_ALIGNMENT);
JLabel label2 = new JLabel("cost");
JLabel label3 = new JLabel ("atk");
JLabel label4 = new JLabel("type");
JLabel label5 = new JLabel ("hp");
JLabel label6 = new JLabel("disc");
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);
panel2.add(label4);
panel2.add(label5);
panel2.add(label6);
panel2.add(jImage);
panel.add(panel2);
frame.revalidate();
});
frame.add(newCardButton, BorderLayout.NORTH);
}
}
My plan is to get a similiar output, but for some reason, I am only getting the south panel...
My logic is to have 1 Main panel with North Center South.
In the North I will puth the Jlabel and Textfield and align it to the right.
In the Center I wil leave it empty
In the South I will Add a BoxLayout y-axis in the first row another panel with centered boxlayout
Another BoxLayout in the second row of the South BoxLayour row, I will add another Boxlayout and align it to the left.
Here is my code:
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setSize(new Dimension(500,600));
JPanel MainPanel = new JPanel();
frame.add(MainPanel);
JPanel NorthPanel = new JPanel(); //upper panel to add boxx layout and inside it 2 panls
JPanel ToPanel = new JPanel(); //inside north
JPanel SubjectPanel = new JPanel(); //inside north
NorthPanel.setLayout(new BoxLayout(NorthPanel, BoxLayout.Y_AXIS));
MainPanel.add(NorthPanel, BorderLayout.NORTH);
JLabel SubjectLabel = new JLabel("Subject"); SubjectLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
JTextField SubjectTextField = new JTextField(20); SubjectTextField.setAlignmentX(Component.RIGHT_ALIGNMENT);
JLabel ToLabel = new JLabel("To"); ToLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
JTextField ToTextField = new JTextField(20); ToTextField.setAlignmentX(Component.RIGHT_ALIGNMENT);
ToPanel.add(ToLabel);
ToPanel.add(ToTextField);
ToPanel.add(SubjectLabel);
ToPanel.add(SubjectTextField);
NorthPanel.add(ToPanel);
JPanel CenterPanel = new JPanel(); //Center panel blank
MainPanel.add(CenterPanel, BorderLayout.CENTER);
///
JPanel SouthPanel = new JPanel();
NorthPanel.setLayout(new BoxLayout(NorthPanel, BoxLayout.Y_AXIS));
JPanel FontPanels = new JPanel();
FontPanels.setLayout(new BoxLayout(FontPanels, BoxLayout.PAGE_AXIS));
FontPanels.add(new JButton("Bold"));
FontPanels.add(new JButton("Italic"));
FontPanels.add(new JButton("Underlined"));
FontPanels.add(new JButton("Undo"));
FontPanels.add(new JButton("Redo"));
FontPanels.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel OptionPanel = new JPanel();
OptionPanel.setLayout(new BoxLayout(OptionPanel, BoxLayout.PAGE_AXIS));
FontPanels.setLayout(new BoxLayout(FontPanels, BoxLayout.PAGE_AXIS));
OptionPanel.add(new JButton("Send"));
OptionPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
SouthPanel.add(FontPanels);
SouthPanel.add(OptionPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
You haven't added anything to the JFrame - so naturally it is displaying a blank JFrame.
For each component to appear, you'll need to do frame.add(component);
I leave the layout manager of the frame up to you.
I'm trying to fit three JPanel (dataPanel, tablePanel and btnPanel), so this is how far I got:
The thing is that the middle panel (tablePanel) is cropped on the top and the header is not visible. Also I added a JScrollPane to it that doesn't appear either. I'm new to Java Swing, so here's my code:
JFrame frame = new JFrame("Crear pedido");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(500,500);
//frame.setSize(300, 150);
// or frame.pack() to "pack" all the components in this frame
frame.setVisible(true); // show it
frame.setResizable(false);
frame.setLocationRelativeTo(null);
JPanel dataPanel = new JPanel();
dataPanel.setLayout(new BoxLayout(dataPanel, BoxLayout.X_AXIS));
//frame.add((Component) BorderFactory.createEmptyBorder(30,0,0,0));
dataPanel.setBorder(new EmptyBorder(20,0,30,0));
frame.add(dataPanel, BorderLayout.NORTH);
JLabel id = new JLabel("ID:");
JLabel date = new JLabel("Fecha:");
JLabel dept = new JLabel("Departamento");
JTextField idInput = new JTextField (5);
idInput.setMaximumSize(new Dimension(70,25));
JFormattedTextField dateInput = new JFormattedTextField(df);
dateInput.setMaximumSize(new Dimension(70,25));
JComboBox deptSelect = new JComboBox(selectArray);
deptSelect.setMaximumSize(new Dimension(70,25));
id.setAlignmentX(Component.CENTER_ALIGNMENT);
date.setAlignmentX(Component.CENTER_ALIGNMENT);
dept.setAlignmentX(Component.CENTER_ALIGNMENT);
idInput.setAlignmentX(Component.CENTER_ALIGNMENT);
dateInput.setAlignmentX(Component.CENTER_ALIGNMENT);
deptSelect.setAlignmentX(Component.CENTER_ALIGNMENT);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(id);
dataPanel.add(idInput);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(date);
dataPanel.add(dateInput);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(dept);
dataPanel.add(deptSelect);
JPanel productPanel = new JPanel();
productPanel.setLayout(new BoxLayout(productPanel, BoxLayout.Y_AXIS));
frame.add(productPanel, BorderLayout.CENTER);
JTable table = new JTable(data, headerProductos);
table.setMaximumSize(new Dimension(500,250));
table.setAlignmentX(Component.CENTER_ALIGNMENT);
table.setAlignmentY(Component.CENTER_ALIGNMENT);
table.getTableHeader().setForeground(Color.blue);
table.setEnabled(false);
JScrollPane scroll = new JScrollPane(table);
productPanel.add(table);
table.add(scroll);
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
btnPanel.setBorder(new EmptyBorder(150,0,0,0));
frame.add(btnPanel, BorderLayout.SOUTH);
Replace lines:
productPanel.add(table);
table.add(scroll);
with:
productPanel.add(scroll);
How can I fix blank screen on jframe and set values of vgap and hgap from textfield. i am using borderlayout for this.
import java.awt.*;
import javax.swing.*;
public class d1{
public static void main(String[] args) {
JFrame f1 = new JFrame ("Border Layout") ;
f1.setLayout(new BorderLayout());
f1.setVisible(true);
f1.setSize(400,400);
f1.setLocationRelativeTo(null);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField t1 = new JTextField();
t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JTextField t2 = new JTextField();
t2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JPanel p1 = new JPanel ();
p1.setLayout(new BorderLayout());
p1.add(new JButton("East"), BorderLayout.EAST);
p1.add(new JButton("South"), BorderLayout.SOUTH);
p1.add(new JButton("West"), BorderLayout.WEST);
p1.add(new JButton("North"), BorderLayout.NORTH);
p1.add(new JButton("Center"), BorderLayout.CENTER);
JPanel p2 = new JPanel ();
p2.setLayout(new BorderLayout());
JPanel p3 = new JPanel ();
p3.setLayout(new BorderLayout());
p3.add(new JLabel("Vgap"), BorderLayout.WEST);
p3.add(t1, BorderLayout.CENTER);
JPanel p4 = new JPanel ();
p4.setLayout(new BorderLayout());
p4.add(new JLabel("Hgap"), BorderLayout.WEST);
p4.add(t2, BorderLayout.CENTER);
JPanel p5 = new JPanel();
p5.setLayout(new BorderLayout());
p5.add(new JLabel("Container of BorderLayout"));
JPanel p6 = new JPanel();
p6.setLayout(new BorderLayout());
p6.add(new JLabel("BorderLayout Properties"));
JPanel p7 = new JPanel ();
p7.setLayout(new BorderLayout());
p7.add(p6, BorderLayout.NORTH);
p7.add(p2, BorderLayout.SOUTH);
p2.add(p3, BorderLayout.NORTH);
p2.add(p4, BorderLayout.SOUTH);
f1.add(p1, BorderLayout.CENTER);
f1.add(p7, BorderLayout.SOUTH);
f1.add(p5, BorderLayout.NORTH);
}
}
after this code.
There should be space between north-south and center, north, south and center.
I can not fix blank screen problem when frame is opened.
Add your panels before f1.setVisible(True) on frame and f1.pack() the frame after it.
You don't need to set a fixed size for your frame. Your added components should take care of it.
To set your components, look at MigLayout. It's easy to use and set components the way you need it.
please check this link you can find it here but first you should add ActionListener into your textField objects so that they can take numbers from inside of themselves. Providing white space in a Swing GUI
something like that
t1 = new JTextField();
t1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText();
int numberhGap = Integer.parseInt(v);
}
});
t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
t2 = new JTextField();
t2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String h = t2.getText();
int numberhGap = Integer.parseInt(h);
}
});
and declare t1 and t2 outside main method.
like this
public class d1{
static JTextField t1;
static JTextField t2;
I have a JPanel and inside I use a GridLayout like this:
JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));
JPanel p1 = new JPanel(new FlowLayout());
JLabel label = new JLabel("SOMETHING");
JTextField tf = new JTextField(30);
JPanel p2 = new JPanel();
JTextArea txt = new JTextArea(6, 30);
JScrollPane sp = new JScrollPane(txt);
p1.add(label);
p1.add(tf);
p2.add(sp);
panel.add(p1);
panel.add(p2);
Unfortunately, the space between the JTextArea and the upper elements if very big.
What can I do to bring the JTextArea up?
http://img20.imageshack.us/img20/1086/screenshot1412201213550.png
Use BorderLayout and add the top panel to NORTH and the scroll pane to the CENTER.
Screenshot of the code below:
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new JPanel(new FlowLayout()) {{
add(new JLabel("something"));
add(new JTextField(30));
}}, BorderLayout.NORTH);
frame.add(new JScrollPane(new JTextArea(6, 30)), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}