Repeat WindowBuilder JLabel using Grid Layout - java

I want to repeat a JLabel "n" no. of times where "n" is given by user.
This implementation, using WindowBuilder's Grid layout, doesn't work, prints nothing at all.
What am I doing wrong?
JLabel lblNewLabel_1[]=new JLabel[20];
GridBagConstraints gbc_lblNewLabel_1[] = new GridBagConstraints[20];
for(int i=0;i<n;i++)
{
lblNewLabel_1[i] = new JLabel("Table #");
gbc_lblNewLabel_1[i].insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1[i].gridx = 0;
gbc_lblNewLabel_1[i].gridy = 4+i;
frame.getContentPane().add(lblNewLabel_1[i], gbc_lblNewLabel_1[i]);
}
Full Method, my content pane is grid layout.
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 507, 432);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{57, 377, 0};
gridBagLayout.rowHeights = new int[]{60, 37, 35, 20, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
textField = new JTextField();
textField.setToolTipText("Max: 20");
textField.setText("0");
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==textField)
n = Integer.parseInt(arg0.getActionCommand());
}
});
JLabel lblNewLabel = new JLabel("Enter The Number of Tables");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 30));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 1;
frame.getContentPane().add(lblNewLabel, gbc_lblNewLabel);
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 3;
frame.getContentPane().add(textField, gbc_textField);
textField.setColumns(10);
GridBagConstraints gbc_lblNewLabel_1[] = new GridBagConstraints[20];
for(int i=0;i<n;i++){
lblNewLabel_1[i] = new JLabel("Table #");
gbc_lblNewLabel_1[i].insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1[i].gridx = 0;
gbc_lblNewLabel_1[i].gridy = 4+i;
frame.getContentPane().add(lblNewLabel_1[i], gbc_lblNewLabel_1[i]);
}
}

The default layout for a JFrame is a BorderLayout.
If you want to use a GridBagLayout then you need to set the layout of the content pane to use a GridBagLayout.
I suggest you read the section from the Swing turial on How to Use GridBagLayout for more explanation and working examples.
If you want to use a GridLayout, then you also need to set the layout manager of the content pane to a GridLayout and you don't use any constraints. Again, read the tutorial for examples.

Related

Program runs good in IDE but not as an exported file [duplicate]

This question already has answers here:
I can run .jar files through cmd, but I cannot double click them [closed]
(7 answers)
Closed 2 years ago.
My MineSweeper project works perfectly in the IDE, but it doesn't work when I export is as "Runnable Jar File" in Eclipse and executing it afterwards.
My MANIFEST.MF
I also tried to navigate via cmd there and do javac Minesweeper.jar.
Console says it cant find the command "javac".
If i cd into my folder and just do java Minesweeper.jar it says: "Cant find or load Mainclass Minesweeper.jar"
This is my code in which the main method is located:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JSpinner;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import javax.swing.ImageIcon;
#SuppressWarnings("serial")
public class Anzeigen extends JFrame {
private JPanel contentPane;
int spaltenAnzahl,bombenAnzahl,reihenAnzahl;
Logik logik;
public Anzeigen() {
logik = new Logik();
reihenAnzahl = 2;
spaltenAnzahl = 2;
bombenAnzahl = 1;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(800, 300, 700, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JLabel lblReihenanzahl = new JLabel("ReihenAnzahl");
lblReihenanzahl.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblReihenanzahl = new GridBagConstraints();
gbc_lblReihenanzahl.insets = new Insets(0, 0, 5, 5);
gbc_lblReihenanzahl.gridx = 4;
gbc_lblReihenanzahl.gridy = 0;
contentPane.add(lblReihenanzahl, gbc_lblReihenanzahl);
JLabel lblSpaltenanzahl = new JLabel("SpaltenAnzahl");
lblSpaltenanzahl.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblSpaltenanzahl = new GridBagConstraints();
gbc_lblSpaltenanzahl.insets = new Insets(0, 0, 5, 5);
gbc_lblSpaltenanzahl.gridx = 7;
gbc_lblSpaltenanzahl.gridy = 0;
contentPane.add(lblSpaltenanzahl, gbc_lblSpaltenanzahl);
JLabel lblAnzahlminen = new JLabel("AnzahlMinen");
lblAnzahlminen.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblAnzahlminen = new GridBagConstraints();
gbc_lblAnzahlminen.insets = new Insets(0, 0, 5, 0);
gbc_lblAnzahlminen.gridx = 9;
gbc_lblAnzahlminen.gridy = 0;
contentPane.add(lblAnzahlminen, gbc_lblAnzahlminen);
JSpinner spinner_1 = new JSpinner();
spinner_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
GridBagConstraints gbc_spinner_1 = new GridBagConstraints();
gbc_spinner_1.gridheight = 4;
gbc_spinner_1.fill = GridBagConstraints.BOTH;
gbc_spinner_1.insets = new Insets(0, 0, 5, 5);
gbc_spinner_1.gridx = 7;
gbc_spinner_1.gridy = 1;
contentPane.add(spinner_1, gbc_spinner_1);
JSpinner spinner_2 = new JSpinner();
spinner_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
GridBagConstraints gbc_spinner_2 = new GridBagConstraints();
gbc_spinner_2.fill = GridBagConstraints.BOTH;
gbc_spinner_2.gridheight = 4;
gbc_spinner_2.insets = new Insets(0, 0, 5, 0);
gbc_spinner_2.gridx = 9;
gbc_spinner_2.gridy = 1;
contentPane.add(spinner_2, gbc_spinner_2);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon(Anzeigen.class.getResource("/javax/swing/plaf/metal/icons/Warn.gif")));
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.fill = GridBagConstraints.BOTH;
gbc_label.insets = new Insets(0, 0, 0, 5);
gbc_label.gridx = 6;
gbc_label.gridy = 9;
label.setVisible(false);
JSpinner spinner = new JSpinner();
spinner.setFont(new Font("Tahoma", Font.PLAIN, 20));
GridBagConstraints gbc_spinner = new GridBagConstraints();
gbc_spinner.fill = GridBagConstraints.BOTH;
gbc_spinner.gridheight = 4;
gbc_spinner.insets = new Insets(0, 0, 5, 5);
gbc_spinner.gridx = 4;
gbc_spinner.gridy = 1;
contentPane.add(spinner, gbc_spinner);
spinner.setValue(2);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.fill = GridBagConstraints.BOTH;
gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel.gridx = 7;
gbc_lblNewLabel.gridy = 9;
lblNewLabel.setVisible(false);
contentPane.add(lblNewLabel, gbc_lblNewLabel);
spinner_1.setValue(2);
spinner_2.setValue(1);
JButton btnStart = new JButton("Start");
btnStart.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnStart.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
lblNewLabel.setVisible(false);
spaltenAnzahl = (int) spinner_1.getValue();
reihenAnzahl = (int) spinner.getValue();
bombenAnzahl = (int) spinner_2.getValue();
System.out.println("SpaltenAnzahl: "+spaltenAnzahl);
System.out.println("ReihenAnzahl: "+reihenAnzahl);
System.out.println("BombenAnzahl: "+bombenAnzahl);
if(bombenAnzahl > spaltenAnzahl * reihenAnzahl) {
label.setVisible(true);
lblNewLabel.setVisible(true);
lblNewLabel.setText("Mehr Minen oder mehr Reihen/Spalten");
}
else if(reihenAnzahl * spaltenAnzahl >= 2500) {
lblNewLabel.setText("Zu Groß!");
lblNewLabel.setVisible(true);
}
else {
logik.inizialisieren(reihenAnzahl,spaltenAnzahl,bombenAnzahl);
setVisible(false);
}
}
});
JLabel lblDirektStarten = new JLabel("Direkt Starten:");
lblDirektStarten.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblDirektStarten = new GridBagConstraints();
gbc_lblDirektStarten.insets = new Insets(0, 0, 5, 5);
gbc_lblDirektStarten.gridx = 1;
gbc_lblDirektStarten.gridy = 7;
contentPane.add(lblDirektStarten, gbc_lblDirektStarten);
GridBagConstraints gbc_btnStart = new GridBagConstraints();
gbc_btnStart.insets = new Insets(0, 0, 5, 0);
gbc_btnStart.gridx = 9;
gbc_btnStart.gridy = 7;
contentPane.add(btnStart, gbc_btnStart);
JButton btnNewButton = new JButton("Einfach");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
logik.inizialisieren(9, 9, 10);
setVisible(false);
}
});
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.fill = GridBagConstraints.VERTICAL;
gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton.gridx = 0;
gbc_btnNewButton.gridy = 8;
contentPane.add(btnNewButton, gbc_btnNewButton);
JButton btnNewButton_1 = new JButton("Mittel");
btnNewButton_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
setVisible(false);
logik.inizialisieren(16, 16, 40);
}
});
GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
gbc_btnNewButton_1.fill = GridBagConstraints.VERTICAL;
gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_1.gridx = 1;
gbc_btnNewButton_1.gridy = 8;
contentPane.add(btnNewButton_1, gbc_btnNewButton_1);
JButton btnNewButton_2 = new JButton("Schwer");
btnNewButton_2.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
setVisible(false);
logik.inizialisieren(16, 30, 99);
}
});
GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints();
gbc_btnNewButton_2.fill = GridBagConstraints.BOTH;
gbc_btnNewButton_2.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_2.gridx = 2;
gbc_btnNewButton_2.gridy = 8;
contentPane.add(btnNewButton_2, gbc_btnNewButton_2);
contentPane.add(label, gbc_label);
}
public static void main(String[] args) {
Anzeigen frame = new Anzeigen();
frame.setVisible(true);
}
}
The way to run a runnable jar is with the -jar parameter, e.g. java -jar Minesweeper.jar . Otherwise it assumes you mean a class name on the CLASSPATH environment variable, which you shouldn't rely on.
Figured it out i had to change stuff in my registry
I can run .jar files through cmd, but I cannot double click them
sry for asking this another time when there is an answer already..

Adding an array of random values to a JList

So I'm trying to make it so that my JList consists of random values everytime I boot my program up (I was using a string array to randomize values from 2 different arrays). I have been struggling to get this working. The elements show when I use one array, but it doesn't work when I use the new array that consists of a string with 2 values from those 2 arrays. How can I make it so that the array with both random values from 2 arrays show up on my JList. I have tried everything starting with making a new array to using DeaultListModel. Any help is appreciated.
Here is my code:
public class game extends JFrame {
private JPanel contentPane;
private JTextField textField_1;
private JTextField textField_2;
//\u21BA is Skip, \u20E0 is Reverse. Action and colorName are the arrays I'm randomizing into the JList
private String[] action = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+2", "\u21BA", " \u20E0"};
private Color[] color = {Color.red, Color.blue, Color.green, Color.yellow};
private String[] colorName = {"Red", "Blue", "Green", "Yellow"};
private DefaultListModel deck;
/**
* Launch the application.
*/
public static void main (String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
game frame = new game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public game() {
setTitle("Uno");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JTextArea textArea = new JTextArea();
textArea.setRows(5);
contentPane.add(textArea, BorderLayout.SOUTH);
\\What I'm struggling with
deck = new DefaultListModel();
for (int i = 0; i <= 7; i++) {
Random r = new Random();
deck.addElement(colorName[r.nextInt(colorName.length)] + " " + action[r.nextInt(action.length)]);
}
JList list = new JList(deck);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setFont(UIManager.getFont("ColorChooser.font"));
contentPane.add(list, BorderLayout.WEST);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.EAST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] {44, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Player 1 Card Amount");
lblNewLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 3;
panel.add(lblNewLabel, gbc_lblNewLabel);
textField_1 = new JTextField();
textField_1.setEditable(false);
textField_1.setFont(new Font("Dialog", Font.PLAIN, 12));
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.insets = new Insets(0, 0, 5, 0);
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 0;
gbc_textField_1.gridy = 4;
panel.add(textField_1, gbc_textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("CPU Card Amount");
lblNewLabel_1.setFont(new Font("Dialog", Font.PLAIN, 12));
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 6;
panel.add(lblNewLabel_1, gbc_lblNewLabel_1);
textField_2 = new JTextField();
textField_2.setFont(new Font("Dialog", Font.PLAIN, 12));
textField_2.setEditable(false);
GridBagConstraints gbc_textField_2 = new GridBagConstraints();
gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_2.gridx = 0;
gbc_textField_2.gridy = 7;
panel.add(textField_2, gbc_textField_2);
textField_2.setColumns(10);
}
}
JList and DefaultListModel are both generic types. They should be JList<String> and DefaultListModel<String> in this case, although that isn't causing your problem. Also, the normal convention for Java is for class names to be capitalized (Game instead of game).
You certainly shouldn't be creating a new Random every time around the loop. It doesn't make sense to me to create random cards, here. They could all be Red 5's. I would create a deck, shuffle it with Fisher-Yates (Collections.shuffle), and take the first seven cards for the player's hand.
If that variable is meant to hold the player's hand, and not the deck, then calling it deck could definitely be confusing. It sounds like that's the player's hand. Better to call it hand.

How to use multiple layouts, without having blank space inbetween panels?

I'm trying to use multiple layouts (Border and Gridbag) to make a standalone visual application that shows the status of recovery rooms. I want my company logo to be in the top left corner. Directly to the right of the logo I want to have information shown in text format such as available beds, how many total occupants, ect...
Below that I am using the Grid bag layout to place bed icons as well as room numbers. There will be a lot more icons later on, I've only got two added at the moment just to show where they will start on the page.
I've provided an image of what it currently looks like with my source code.
TLDR: I need to beds shown in the image to be in the upper left part of the screen about half an inch from the bottom of the sample logo.
I tried to add an image, but I don't have enough reputation. here is a link to it if you need to see.
http://imgur.com/DViRSuJ
Source code for image above:
public static void showUI() {
int bedCount = getBedCount();
bedCount = bedCount - 5;
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
ImageIcon maleBed = new ImageIcon("Images/Male.png");
ImageIcon femaleBed = new ImageIcon("Images/Female.png");
ImageIcon emptyBed = new ImageIcon("Images/empty.png");
ImageIcon logo = new ImageIcon("Images/logo-sample.png");
ImageIcon mlogo = new ImageIcon("Images/Medicalistics_Logo.png");
JFrame window = new JFrame("Ft Lauderdale");
window.setVisible(true);
window.setSize(xSize, ySize);
JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
mainPanel.setBounds(900, 900, 900, 900);
mainPanel.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(mainPanel);
JPanel logoPane = new JPanel();
logoPane.setLayout(new BorderLayout());
logoPane.setBackground(Color.white);
mainPanel.add(logoPane, BorderLayout.NORTH);
JPanel bedsPane = new JPanel();
bedsPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel logoLabel = new JLabel(logo);
logoLabel.setHorizontalAlignment(JLabel.LEFT);
logoPane.add(logoLabel, BorderLayout.NORTH);
mainPanel.add(bedsPane, BorderLayout.CENTER);
JLabel bedLabel = new JLabel(emptyBed);
c.gridheight = 5;
c.gridwidth = 5;
c.gridx = 50;
c.gridy = 0;
bedsPane.add(bedLabel, c);
JLabel bedLabel2 = new JLabel(femaleBed);
c.gridx = 0;
c.gridy = 0;
bedsPane.add(bedLabel2, c);
}
Well for the blank around panels you probably need to set the insets
eg: c.insets = new Insets(0, 0, 0, 0);
I am not sure how you want to handle 2 layout but you can do the effect you want with simply GridBagLayout.
You can see my test code here :
package Main;
import java.awt.*;
import javax.swing.*;
public class dummyGUI {
private static JPanel infopane, bedsPane, logopane, mainPanel;
private static JLabel lmalebed, lfemalebed, logoLabel;
public dummyGUI() {
showUI();
}
public static void showUI() {
int bedCount = 7; // getBedCount();
bedCount = bedCount - 5;
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
ImageIcon maleBed = new ImageIcon("Images/Male.png");
ImageIcon femaleBed = new ImageIcon("Images/Female.png");
ImageIcon emptyBed = new ImageIcon("Images/empty.png");
ImageIcon logo = new ImageIcon("Images/logo-sample.png");
ImageIcon mlogo = new ImageIcon("Images/Medicalistics_Logo.png");
JFrame window = new JFrame("Ft Lauderdale");
window.setVisible(true);
window.setSize(xSize, ySize);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 897, 0 };
gridBagLayout.rowHeights = new int[] { 504, 0, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
window.getContentPane().setLayout(gridBagLayout);
mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
GridBagConstraints gbc_mainPanel = new GridBagConstraints();
gbc_mainPanel.insets = new Insets(0, 0, 5, 0);
gbc_mainPanel.fill = GridBagConstraints.BOTH;
gbc_mainPanel.gridx = 0;
gbc_mainPanel.gridy = 0;
window.getContentPane().add(mainPanel, gbc_mainPanel);
GridBagLayout gbl_mainPanel = new GridBagLayout();
gbl_mainPanel.columnWidths = new int[] { 286, 518, 0 };
gbl_mainPanel.rowHeights = new int[] { 101, 367, 0 };
gbl_mainPanel.columnWeights = new double[] { 1.0, 2.0, Double.MIN_VALUE };
gbl_mainPanel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
mainPanel.setLayout(gbl_mainPanel);
logopane = new JPanel();
logopane.setBackground(Color.WHITE);
GridBagConstraints gbc_logopane = new GridBagConstraints();
gbc_logopane.insets = new Insets(0, 0, 0, 0);
gbc_logopane.fill = GridBagConstraints.BOTH;
gbc_logopane.gridx = 0;
gbc_logopane.gridy = 0;
mainPanel.add(logopane, gbc_logopane);
logopane.setLayout(new BorderLayout());
logoLabel = new JLabel(logo);
logopane.add(logoLabel, BorderLayout.NORTH);
logoLabel.setHorizontalAlignment(JLabel.LEFT);
infopane = new JPanel();
infopane.setBackground(Color.WHITE);
GridBagConstraints gbc_infopane = new GridBagConstraints();
gbc_infopane.insets = new Insets(0, 0, 0, 0);
gbc_infopane.fill = GridBagConstraints.BOTH;
gbc_infopane.gridx = 1;
gbc_infopane.gridy = 0;
mainPanel.add(infopane, gbc_infopane);
infopane.setLayout(new BorderLayout());
bedsPane = new JPanel();
GridBagConstraints gbc_bedsPane = new GridBagConstraints();
gbc_bedsPane.gridwidth = 2;
gbc_bedsPane.fill = GridBagConstraints.BOTH;
gbc_bedsPane.gridx = 0;
gbc_bedsPane.gridy = 1;
mainPanel.add(bedsPane, gbc_bedsPane);
GridBagLayout gbl_bedsPane = new GridBagLayout();
gbl_bedsPane.columnWidths = new int[] { 1, 0, 0, 0, 0 };
gbl_bedsPane.rowHeights = new int[] { 1, 0, 0, 0 };
gbl_bedsPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
gbl_bedsPane.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
bedsPane.setLayout(gbl_bedsPane);
lmalebed = new JLabel(maleBed);
GridBagConstraints gbc_lmalebed = new GridBagConstraints();
gbc_lmalebed.insets = new Insets(0, 0, 0, 0);
gbc_lmalebed.gridx = 1;
gbc_lmalebed.gridy = 1;
bedsPane.add(lmalebed, gbc_lmalebed);
lfemalebed = new JLabel(femaleBed);
GridBagConstraints gbc_lfemalebed = new GridBagConstraints();
gbc_lfemalebed.gridx = 3;
gbc_lfemalebed.gridy = 1;
bedsPane.add(lfemalebed, gbc_lfemalebed);
}
public static void main(String[] args) {
new dummyGUI();
}
}
The part here
gbl_bedsPane.columnWidths = new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
gbl_bedsPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_bedsPane.rowHeights = new int[] { 1, 0, 0, 0, 0 };
gbl_bedsPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
is where you can tell the number of row and column. I did not set enough column that is why you see it appear on the right when you add more.
I also did a function like i said in the comment :
private static JLabel add_bed(String sex, int x, int y){
JLabel bed = null;
if (sex == "female"){
bed = new JLabel(femaleBed);
GridBagConstraints gbc_lfemalebed = new GridBagConstraints();
gbc_lfemalebed.insets = new Insets(0, 0, 0, 0);
gbc_lfemalebed.gridx = x;
gbc_lfemalebed.gridy = y;
bedsPane.add(lfemalebed, gbc_lfemalebed);
}else if (sex == "male"){
lmalebed = new JLabel(maleBed);
GridBagConstraints gbc_lmalebed = new GridBagConstraints();
gbc_lmalebed.insets = new Insets(0, 0, 0, 0);
gbc_lmalebed.gridx = x;
gbc_lmalebed.gridy = y;
bedsPane.add(lmalebed, gbc_lmalebed);
}
return bed;
}
Then i can just do
add_bed("female", 5 , 1);

Setting multiple JPanels to JFrame

I need to format my multiple panels into my main panel, however I'm troubled with which layout to use and more so on how to do it with a specific layout.
The layout I need is like this
So far the code I have is this:
public TDPanel(){
this.setPreferredSize(new Dimension(1150,800));
this.setBackground(Color.gray);
this.tdWorld = towerOfDefenses;
this.setLayout(new FlowLayout());
game = new JPanel();
game.setBackground(Color.blue);
game.setPreferredSize(new Dimension(300,300));
textBox = new JTextField();
textBox.setBackground(Color.ORANGE);
textBox.setPreferredSize(new Dimension(400,300));
menuPanel = new JPanel();
menuPanel.setPreferredSize(new Dimension(100,800));
menuPanel.setBackground(Color.red);
this.add(game);
this.add(textBox);
this.add(menuPanel);
}
I would appreciate any help given!
I would combine at least 3 BorderLayouts. Why? Because the component you put in CENTER will be maximized and for the others you can set a static width (or height) and don't have to do further configuration to get the desired behaviour.
+-------------------------+-----------------------+
| BorderLayout.CENTER (1) | BorderLayout.EAST (2) |
+-------------------------+-----------------------+
In (1) you put the game panel (3) and the "game controls" (4):
+-------------------------+
| BorderLayout.CENTER (3) |
+-------------------------+
| BorderLayout.SOUTH (4) |
+-------------------------+
If you want the text field and the button in (4) to have the same size and maximized (width) then use a GridLayout, othewise you can use FlowLayout to have them layed out with some space after it. But what I recommend doing here is the same as for the game and menu panel in (2): use a BorderLayout and put the component you want to be maximized in the center.
You could use more sophisticated LayoutManagers like BoxLayout or GridBagLayout but it is not really needed for this simple layout (guess it's a matter of taste).
First,I recommend you use Swing Desinger,it's a plug-in unit to visualize your operation.
I'm using GridBagLayout to format these panel,and here is an example.
This is the effect drawing adress
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 685, 485);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{175, 40, 180, 217, 0};
gbl_contentPane.rowHeights = new int[]{15, 58, 220, 49, 55, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JPanel gamepanel = new JPanel();
GridBagConstraints gbc_gamepanel = new GridBagConstraints();
gbc_gamepanel.fill = GridBagConstraints.BOTH;
gbc_gamepanel.insets = new Insets(0, 0, 5, 5);
gbc_gamepanel.gridheight = 2;
gbc_gamepanel.gridwidth = 3;
gbc_gamepanel.gridx = 0;
gbc_gamepanel.gridy = 1;
contentPane.add(gamepanel, gbc_gamepanel);
gamepanel.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane.gridx = 3;
gbc_scrollPane.gridy = 1;
contentPane.add(scrollPane, gbc_scrollPane);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridheight = 3;
gbc_panel.gridx = 3;
gbc_panel.gridy = 2;
contentPane.add(panel, gbc_panel);
panel.setLayout(null);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.BOTH;
gbc_textField.insets = new Insets(0, 0, 0, 5);
gbc_textField.gridx = 0;
gbc_textField.gridy = 4;
contentPane.add(textField, gbc_textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("New button");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.fill = GridBagConstraints.BOTH;
gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
gbc_btnNewButton.gridx = 2;
gbc_btnNewButton.gridy = 4;
contentPane.add(btnNewButton, gbc_btnNewButton);
}
If you want the GUI can be resizable,you can set the weghitx and weghity properties.

GUI won't display two seperate scrollpanes

As you can see in my code, there are two scroll panes at the bottom. I need my gui to show both of these scroll panes on the right side, the smaller one(displaying the current amount of money the player has, this one I need to stay one line thick) and the second one, which will be constantly spitting out information of the player's rolls on the bottom.
Also how do I make it so that my combo box listener will give every item a 10 to its output to start out with then when it is being sent out it gives each 1 more so that they have their unique number starting at 11 then 12, 13, and so on.
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class dicebot extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JComboBox combo;
private JButton btnConfirm;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dicebot frame = new dicebot();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public dicebot() {
setTitle("Dice Bot");
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.WEST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
//Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
JLabel userTag = new JLabel("Username:");
GridBagConstraints gbc_userTag = new GridBagConstraints();
gbc_userTag.insets = new Insets(0, 0, 0, 5);
gbc_userTag.anchor = GridBagConstraints.EAST;
gbc_userTag.gridx = 0;//Here are your x + y coords
gbc_userTag.gridy = 0;//Adding to x moves left, adding to y moves down
panel.add(userTag, gbc_userTag);
//Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel startTag = new JLabel("Starting Bid:");
GridBagConstraints gbc_startTag = new GridBagConstraints();
gbc_startTag.insets = new Insets(0, 0, 0, 5);
gbc_startTag.anchor = GridBagConstraints.EAST;
gbc_startTag.gridx = 0;
gbc_startTag.gridy = 2;
panel.add(startTag, gbc_startTag);
textField = new JTextField();
GridBagConstraints gbc_textFieldStartBid = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 2;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel multTag = new JLabel("Multiplier:");
GridBagConstraints gbc_multTag = new GridBagConstraints();
gbc_multTag.insets = new Insets(0, 0, 0, 5);
gbc_multTag.anchor = GridBagConstraints.EAST;
gbc_multTag.gridx = 0;
gbc_multTag.gridy = 3;
panel.add(multTag, gbc_multTag);
textField = new JTextField();
GridBagConstraints gbc_textFieldMultiplier = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 3;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel minTag = new JLabel("Min Remaining:");
GridBagConstraints gbc_minTag = new GridBagConstraints();
gbc_minTag.insets = new Insets(0, 0, 0, 5);
gbc_minTag.anchor = GridBagConstraints.EAST;
gbc_minTag.gridx = 0;
gbc_minTag.gridy = 4;
panel.add(minTag, gbc_minTag);
textField = new JTextField();
GridBagConstraints gbc_textFieldMinRemaining = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 4;
panel.add(textField, gbc_textField);
textField.setColumns(10);
textField = new JTextField();
GridBagConstraints gbc_textFieldPassword = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 1;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel passTag = new JLabel("Password:");
GridBagConstraints gbc_passTag = new GridBagConstraints();
gbc_passTag.insets = new Insets(0, 0, 0, 5);
gbc_passTag.anchor = GridBagConstraints.EAST;
gbc_passTag.gridx = 0;
gbc_passTag.gridy = 1;
panel.add(passTag, gbc_passTag);
textField = new JTextField();
GridBagConstraints gbc_textFieldOdds = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 5;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JLabel oddsTag = new JLabel("Odds %:");
GridBagConstraints gbc_oddsTag = new GridBagConstraints();
gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
gbc_oddsTag.anchor = GridBagConstraints.EAST;
gbc_oddsTag.gridx = 0;
gbc_oddsTag.gridy = 5;
panel.add(oddsTag, gbc_oddsTag);
textField = new JTextField();
GridBagConstraints gbc_textFieldMaxBet = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 6;
panel.add(textField, gbc_textField);
textField.setColumns(10);
//This is the Combo Box
combo = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
combo.addActionListener(this);
GridBagConstraints gbc_list = new GridBagConstraints();
gbc_list.fill = GridBagConstraints.HORIZONTAL;
gbc_list.gridx = 1;
gbc_list.gridy = 7;
panel.add(combo, gbc_list);
JLabel maxTag = new JLabel("MaxBet:");
GridBagConstraints gbc_maxTag = new GridBagConstraints();
gbc_maxTag.insets = new Insets(0, 0, 0, 5);
gbc_maxTag.anchor = GridBagConstraints.EAST;
gbc_maxTag.gridx = 0;
gbc_maxTag.gridy = 6;
panel.add(maxTag, gbc_maxTag);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
btnConfirm = new JButton("Turn Up");
btnConfirm.addActionListener(this);
panel_1.add(btnConfirm);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea("Current Balance");
textArea.setColumns(1);
scrollPane.setViewportView(textArea);
JScrollPane scrollPanel = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textAreal = new JTextArea("Input bot information here...");
textArea.setColumns(20);
scrollPane.setViewportView(textArea);
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == combo) {
System.out.println(combo.getSelectedIndex()+1);
}else if (e.getSource() == btnConfirm){
Dicebotcode dbc = new Dicebotcode();
dbc.dbc();
}
}
}
Two things...
One, if you've added the same scroll pane twice...
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JScrollPane scrollPanel = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
contentPane.add(scrollPane, BorderLayout.CENTER);
^--- Note no "l"
And, even if you have added the two scroll pane instances, you've added them to the same place, BorderLayout.CENTER
BorderLayout only allows a single component to be added to one of the available positions it supports, so, in this case, only the second scroll pane would be visible

Categories