Adding 2 JLists to a JPanel using BorderLayout - java

I need to add 2 JLists inside a JPanel for a crossword. The JPanel is located SOUTH and I'm using BorderLayout in the constructor to locate the JPanel.
The problem is, I can't see the 2 JLists inside the JPanel
package crossword;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame implements MouseListener {
Container container;
//this is the constructor
public Main(){
super("Crossword");
container = getContentPane();
container.setLayout(new BorderLayout(1, 1));
container.setBackground(new Color(91,119,162));
container.add(login(),BorderLayout.NORTH);
container.add(buttons(),BorderLayout.WEST);
container.add(clues(),BorderLayout.SOUTH);
container.add(createMatrix(crosswordPanel()),BorderLayout.CENTER);
container.add(eastPanel(),BorderLayout.EAST);
}
public JPanel login(){
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout(1, 15, 5));
loginPanel.setPreferredSize(new Dimension(8, 40));
loginPanel.setBackground(new Color(47,47,47));
loginPanel.setVisible(true);
JTextField inputUserName = new JTextField(15);
loginPanel.add(inputUserName);
JButton btnEnter = new JButton("Play!");
btnEnter.setPreferredSize(new Dimension(80,25));
loginPanel.add(btnEnter);
return loginPanel;
}
public JPanel buttons(){
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout(5, 45, 15));
buttonsPanel.setPreferredSize(new Dimension(250, 200));
buttonsPanel.setBackground(new Color(91,119,162));
buttonsPanel.setVisible(true);
JButton newGame = new JButton("New game");
newGame.setPreferredSize(new Dimension(130,27));
newGame.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(newGame);
JButton showChar = new JButton("Reveal letter");
showChar.setPreferredSize(new Dimension(130,27));
showChar.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(showChar);
JButton showWord = new JButton("Reveal word");
showWord.setPreferredSize(new Dimension(130,27));
showWord.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(showWord);
JButton verifyWord = new JButton("Verify");
verifyWord.setPreferredSize(new Dimension(130,27));
verifyWord.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(verifyWord);
JButton revealAll = new JButton("Solution");
revealAll.setPreferredSize(new Dimension(130,27));
revealAll.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(revealAll);
JLabel labelScore = new JLabel("Score:");
labelScore.setFont(new Font("Arial", Font.BOLD, 13));
buttonsPanel.add(labelScore);
return buttonsPanel;
}
public JPanel clues(){
JPanel clues = new JPanel();
clues.setLayout(new FlowLayout(3, 1, 1));
clues.setPreferredSize(new Dimension(200, 200));
clues.setBackground(new Color(91,119,162));
clues.setVisible(true);
JList horizontalList = new JList();
horizontalList.setBackground(Color.BLUE);
horizontalList.setBorder(BorderFactory.createEtchedBorder(1));
clues.add(horizontalList);
JList verticalList = new JList();
verticalList.setBackground(Color.ORANGE);
verticalList.setBorder(BorderFactory.createEtchedBorder(1));
clues.add(verticalList);
return clues;
}
public JPanel eastPanel(){
JPanel fill = new JPanel();
fill.setLayout(new FlowLayout(5, 25, 15));
fill.setPreferredSize(new Dimension(50, 200));
fill.setBackground(new Color(91,119,162));
fill.setVisible(true);
return fill;
}
public JPanel crosswordPanel(){
JPanel crosswordPanel = new JPanel();
crosswordPanel.setLayout(new GridLayout(10,10,0,0));
crosswordPanel.setBackground(Color.white);
//crosswordPanel.setPreferredSize(new Dimension(200, 200));
crosswordPanel.setBackground(new Color(91,119,162));
crosswordPanel.setVisible(true);
return crosswordPanel;
}
public JPanel createMatrix(JPanel crosswordPanel){
JPanel crosswordMatrix [][] = new JPanel [10][10];
for (int i = 0; i < crosswordMatrix.length ; i++) {
for (int j = 0; j < crosswordMatrix[0].length; j++) {
crosswordMatrix[i][j] = new JPanel();
crosswordMatrix[i][j].setBackground(Color.lightGray);
crosswordMatrix[i][j].setBorder(BorderFactory.createLineBorder(Color.black, 1));
crosswordMatrix[i][j].addMouseListener(this);
//crosswordMatrix[i][j].add(label);
crosswordPanel.add(crosswordMatrix[i][j]);
}
}
return crosswordPanel;
}
public static void main(String[] args) {
Main guiWindow = new Main();
Toolkit screenSize = Toolkit.getDefaultToolkit();
int width = screenSize.getScreenSize().width * 4/5;
int height = screenSize.getScreenSize().height * 4/5;
guiWindow.setSize(width, height);
guiWindow.setLocationRelativeTo(null);
guiWindow.setVisible(true);
guiWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Its not an MCVE, but based upon the limited code you posted: in the clues method a JPanel is constructed, yet nothing is ever added to it. Rather, you add the JLists directly to the object which contains the method (which I presume this extends JFrame). Perhaps try adding the components to the JPanel
public JPanel clues(){
JPanel clues = new JPanel();
...
clues.add(horizontalList);
....
clues.add(verticalList);
}

Related

JPanel gets compressed and disappears when trying to move to the bottom

I have to do a project in Java and thought a GUI Text Adventure would be cool. My Problem is that when I create a JPanel and move it further down on the screen, the panel first changes its size and then disappears completely at one point.
On the GameScreen there should be a panel for choice Options to be put on but it refuses to go further down than about half the size of the Screen.
Here's the code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Yeet {
JFrame epicOfYeet;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
JButton startButton;
JButton choice1;
JButton choice2;
JButton choice3;
JButton choice4;
JTextArea mainTextArea;
TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
new Yeet();
}
public Yeet() {
epicOfYeet = new JFrame();
epicOfYeet.setSize(1200, 1000);
epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epicOfYeet.getContentPane().setBackground(Color.black);
epicOfYeet.setLayout(null);
con = epicOfYeet.getContentPane();
titleNamePanel = new JPanel();
titleNamePanel.setBounds(190, 100, 800, 230);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("EPIC OF YEET");
titleNameLabel.setForeground(Color.red);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(400, 500, 400, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel);
con.add(startButtonPanel);
epicOfYeet.setVisible(true);
}
public void createGameScreen(){
titleNamePanel.setVisible(false);
startButtonPanel.setVisible(false);
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 1000, 400);
mainTextPanel.setBackground(Color.green);
con.add(mainTextPanel);
mainTextArea = new JTextArea("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
mainTextArea.setBounds(100, 100, 1000, 250);
mainTextArea.setBackground(Color.blue);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
choiceButtonPanel.setBounds(300, 500, 600, 550);
choiceButtonPanel.setBackground(Color.red);
con.add(choiceButtonPanel);
}
public class TitleScreenHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
}
Here is a basic implementation using layout mangers, avoiding the bad practice of null layout manager.
It is not an optimal one, but should get you started:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Yeet {
JFrame epicOfYeet;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
JLabel titleNameLabel;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
JButton startButton, coice1, choice2, choice3, choice4;
JTextArea mainTextArea;
TitleScreenHandler tsHandler = new TitleScreenHandler();
public static void main(String[] args) {
SwingUtilities.invokeLater(()->new Yeet());
}
public Yeet() {
epicOfYeet = new JFrame();
//epicOfYeet.setSize(1200, 1000); // do not set size. let layout manager calcualte it
epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
epicOfYeet.getContentPane().setBackground(Color.black);
//epicOfYeet.setLayout(null); //aviod null layouts Jframe uses BorderLayout by default
con = epicOfYeet.getContentPane();
titleNamePanel = new JPanel();
//titleNamePanel.setBounds(190, 100, 800, 230);
titleNamePanel.setPreferredSize(new Dimension(800, 230));//set preferred size to be used by layout manager
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("EPIC OF YEET");
titleNameLabel.setForeground(Color.red);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
//startButtonPanel.setBounds(400, 500, 400, 100);
startButtonPanel.setPreferredSize(new Dimension(400, 100));
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel, BorderLayout.PAGE_START); //set to top
con.add(startButtonPanel, BorderLayout.PAGE_END); //set to bottom
epicOfYeet.pack();
epicOfYeet.setVisible(true);
}
public void createGameScreen(){
//titleNamePanel.setVisible(false);
//startButtonPanel.setVisible(false);
con.remove(titleNamePanel);
con.remove(startButtonPanel);
mainTextPanel = new JPanel();
//mainTextPanel.setBounds(100, 100, 1000, 400);
mainTextPanel.setBackground(Color.green);
con.add(mainTextPanel); // added to center position of the BorderLayout (the default)
mainTextArea = new JTextArea(10, 20); //size in rows, cols
mainTextArea.setText("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
//mainTextArea.setBounds(100, 100, 1000, 250);
mainTextArea.setBackground(Color.blue);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
//choiceButtonPanel.setBounds(300, 500, 600, 550);
choiceButtonPanel.setPreferredSize(new Dimension(600, 550));
choiceButtonPanel.setBackground(Color.red);
con.add(choiceButtonPanel, BorderLayout.PAGE_END);//add to bottom
epicOfYeet.pack();
}
public class TitleScreenHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
}

Having a JTextField popup at a certain location after clicking a JButton

I have my three JButtons located where I want them (at the top center of the frame), and when the user clicks one, a JTextField pops up in the BoxLayout like wanted.
The problem is, when the JTextField shows up, it is to the left of the buttons, and it moves them.
I tried setting the alignment of the JTextField and using various glues, but the JTextField doesn't move.
If I want to have the JTextField pop up below my JButtons and in the center of the screen, what should I use?
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Library extends JFrame implements ActionListener {
private JFrame jf1;
private JPanel jp1;
private JTextField jtf1;
private JButton jb1;
private JButton jb2;
private JButton jb3;
public Library() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception q) {
q.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(false);
jf1 = new JFrame("Library");
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setSize(1080, 900);
jf1.setResizable(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);
jp1 = (JPanel) jf1.getContentPane();
jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));
jb1 = new JButton("Genre");
jb1.addActionListener(this);
jb1.setMinimumSize(new Dimension(140, 60));
jb1.setPreferredSize(new Dimension(150, 60));
jb1.setMaximumSize(new Dimension(150, 60));
jb1.setAlignmentY(-70.0f);
jb2 = new JButton("Author");
jb2.addActionListener(this);
jb2.setMinimumSize(new Dimension(140, 60));
jb2.setPreferredSize(new Dimension(150, 60));
jb2.setMaximumSize(new Dimension(150, 60));
jb2.setAlignmentY(-70.0f);
jb3 = new JButton("Title");
jb3.addActionListener(this);
jb3.setMinimumSize(new Dimension(140, 60));
jb3.setPreferredSize(new Dimension(150, 60));
jb3.setMaximumSize(new Dimension(150, 60));
jb3.setAlignmentY(-70.0f);
jp1.add(Box.createHorizontalGlue());
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
jp1.add(Box.createHorizontalGlue());
jf1.validate();
}
#Override
public void actionPerformed(ActionEvent e) {
Object code = e.getSource();
if (code == jb1) {
jtf1 = new JTextField("Enter Text");
jtf1.setPreferredSize(new Dimension(200,20));
jtf1.setMaximumSize(new Dimension(200,20));
jtf1.setMinimumSize(new Dimension(10,10));
jp1.add(jtf1);
jp1.validate();
}
else if (code == jb2) {
}
else if (code == jb3) {
}
}
public static void main(String[] args) {
Library shoe = new Library();
}
}
Suggestion: do not add/remove UI elements dynamically. Just add all of those things initially, and simply call setVisible(false) on your text field then.
(instead of adding/removing fields using your action listener)
A good and easy solution may be to use another JPanel to hold your dynamically created text fields. This may be what you wanted:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Library extends JFrame implements ActionListener {
private JFrame jf1;
private JPanel jp1;
private JPanel jp2;
private JTextField jtf1;
private JButton jb1;
private JButton jb2;
private JButton jb3;
public Library() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception q) {
q.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(false);
jf1 = new JFrame("Library");
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setSize(1080, 900);
jf1.setResizable(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);
jp1 = new JPanel();
jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));
jp2 = new JPanel();
jp2.setLayout(new BoxLayout(jp2, BoxLayout.LINE_AXIS));
jb1 = new JButton("Genre");
jb1.addActionListener(this);
jb1.setMinimumSize(new Dimension(140, 60));
jb1.setPreferredSize(new Dimension(150, 60));
jb1.setMaximumSize(new Dimension(150, 60));
jb1.setAlignmentY(-70.0f);
jb2 = new JButton("Author");
jb2.addActionListener(this);
jb2.setMinimumSize(new Dimension(140, 60));
jb2.setPreferredSize(new Dimension(150, 60));
jb2.setMaximumSize(new Dimension(150, 60));
jb2.setAlignmentY(-70.0f);
jb3 = new JButton("Title");
jb3.addActionListener(this);
jb3.setMinimumSize(new Dimension(140, 60));
jb3.setPreferredSize(new Dimension(150, 60));
jb3.setMaximumSize(new Dimension(150, 60));
jb3.setAlignmentY(-70.0f);
jp1.add(Box.createHorizontalGlue());
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
jp1.add(Box.createHorizontalGlue());
jf1.getContentPane().add(jp1);
jp1.setBounds(0, 0, 1080, 900);
jf1.getContentPane().add(jp2);
jp2.setBounds(0, 0, 1080, 900);
validate();
}
public void actionPerformed(ActionEvent e) {
Object code = e.getSource();
if (code == jb1) {
jtf1 = new JTextField("Enter Text");
jtf1.setPreferredSize(new Dimension(200,20));
jtf1.setMaximumSize(new Dimension(200,20));
jtf1.setMinimumSize(new Dimension(10,10));
jp2.add(Box.createHorizontalGlue());
jp2.add(jtf1);
jp2.add(Box.createHorizontalGlue());
jp2.validate();
}
else if (code == jb2) {
}
else if (code == jb3) {
}
}
public static void main(String[] args) {
Library shoe = new Library();
}
}
I gave it a quick try to solve the issue. Hope it helps. Do more tweaking in your code to perfectly match the layout.

When I use the WindowBuilder preview function, the code appears as it should, but not when I press "run"

When I use the WindowBuilder preview, the code appears as it should, but when I run the program, it just brings up a blank frame. It looks fine in the preview, but as soon as I run it, it just brings up a blank frame.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
public class Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel teamSelect;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame frame = new JFrame();
frame.setForeground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(new Point(100, 50));
frame.setSize(1800, 900);
frame.setTitle("Hockey");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
teamSelect = new JPanel();
teamSelect.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(teamSelect);
teamSelect.setLayout(new CardLayout(0, 0));
JPanel divisionGrid = new JPanel();
teamSelect.add(divisionGrid, "name_3885325667274");
divisionGrid.setLayout(new GridLayout(2, 2, 0, 0));
JPanel metroDivision = new JPanel();
divisionGrid.add(metroDivision);
metroDivision.setLayout(new BorderLayout(0, 0));
JLabel lblMetropolitanDivision = new JLabel("Metropolitan Division");
lblMetropolitanDivision.setHorizontalAlignment(SwingConstants.CENTER);
lblMetropolitanDivision.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
metroDivision.add(lblMetropolitanDivision, BorderLayout.NORTH);
JPanel atlanticDivision = new JPanel();
divisionGrid.add(atlanticDivision);
atlanticDivision.setLayout(new BorderLayout(0, 0));
JLabel lblAtlanticDivision = new JLabel("Atlantic Division");
lblAtlanticDivision.setHorizontalAlignment(SwingConstants.CENTER);
lblAtlanticDivision.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
atlanticDivision.add(lblAtlanticDivision, BorderLayout.NORTH);
JPanel centralDivision = new JPanel();
divisionGrid.add(centralDivision);
centralDivision.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel = new JLabel("Central Division");
lblNewLabel.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
centralDivision.add(lblNewLabel, BorderLayout.NORTH);
JPanel pacificDivision = new JPanel();
divisionGrid.add(pacificDivision);
pacificDivision.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel_1 = new JLabel("Pacific Division");
lblNewLabel_1.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
pacificDivision.add(lblNewLabel_1, BorderLayout.NORTH);
}
Replace
new JFrame();
by
new Frame();
And call pack() before setVisible(true).
Note that just adding a System.out.println() or using the debugger to step through your code would have allowed you to find this simple mistake. A debugger is a wonderful tool. use it.

How can I set a size for my label?

In this code I try to set a size for my label in line 70:
label1.setMinimumSize(new Dimension(150, 100));
But I don't know why the compiler ignores that code so help me to re size my labels in "box layout" and other layouts
This is my code!
package gui1.pkg2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
* #author Danial
*/
public class Frame extends JFrame {
public Frame() {
setPreferredSize(new Dimension(600, 500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.red); //---------------------------------------------------------------------------------->> karnemikone.
//-------Strat--- ADD sub Classes-------------------
//-------DownButtons Class----------
DownButtons dB = new DownButtons();
add(dB, BorderLayout.SOUTH);
//-------BlackAndWite Class----------
BlackAndWite bW = new BlackAndWite();
add(bW, BorderLayout.WEST);
//-------RadioButtons Class----------
radioButtons rB = new radioButtons();
add(rB, BorderLayout.EAST);
//-------End----ADD subClasses----------------------
setVisible(true);
pack();
//-----------------------------
}
//-------- a class for buttons.--------------
class DownButtons extends JPanel {
public DownButtons() {
setLayout(new FlowLayout());
JButton save = new JButton("Save");
save.setBackground(new java.awt.Color(150, 231, 19));
JButton exit = new JButton("Exit");
exit.setBackground(new java.awt.Color(150, 231, 19));
JButton cancel = new JButton("cancel");
cancel.setBackground(new java.awt.Color(150, 231, 19));
add(save);
add(exit);
add(cancel);
pack();
}
}
class BlackAndWite extends JPanel {
public BlackAndWite() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JLabel label1 = new JLabel(" ");
label1.setOpaque(true);
label1.setMinimumSize(new Dimension(150, 100));
label1.setBackground(Color.red);
add(label1);
}
}
class radioButtons extends JPanel {
public radioButtons() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
class Inner extends JPanel {
public Inner(String labl, String r1, String r2) {
setLayout(new FlowLayout());
JLabel label = new JLabel(labl);
label.setOpaque(true);
label.setBackground(Color.YELLOW);
add(label);
JRadioButton radio1 = new JRadioButton(r1 + "");
JRadioButton radio2 = new JRadioButton(r2);
add(radio1);
add(radio2);
}
}
Inner in = new Inner(gender, male , Female);
///add(in);
}
}
}

Resizing jpanel according to the components present in it

In the below example, in the west side of the border layout, there is a parent panel which has a BoxLayout and couple of panels inside. The problem is that the west panel covers the whole area from top to bottom. The FlowLayout used for child panels inside the parent panel consume a lot of area. Is it possible to compress each JPanel according to the components? Also, it should remain the same even when the window is maximized?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class Sample extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample frame = new Sample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.WEST);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);
textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
}
}
One approach is to add panel_1 to an enclosing panel. The default FlowLayout conforms itself to the preferred size of the enclosed components when you pack() the enclosing Window. I've added a gray panel to CENTER as a placeholder; resize the frame to see the effect.
JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
As tested:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class Sample extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Sample frame = new Sample();
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);
textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
contentPane.add(flowPanel, BorderLayout.WEST);
contentPane.add(new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
#Override
public Color getBackground() {
return Color.lightGray;
}
}, BorderLayout.CENTER);
}
}

Categories

Resources