Dynamic scaling in nested layout - java

I want to have arbitrary JPanels containing a JLabel sacling dynamically in a container (JPanel) with a fixed size. So something like this:
__________________________________
| _____________ _____________ |
| | Test Label| | Test Label| |
| |___________| |___________| |
|________________________________|
scaling to this
__________________________________
| ________ ________ ________ |
| |Tes...| |Tes...| |Tes...| |
| |______| |______| |______| |
|________________________________|
I tried to use a BoxLayout (X-Orientation), setting a maximum size for each contained JPanel:
OuterContainer.Width / NumberOfContainedJPanels
without success. The containers around the labels never fall below a specific width.
Further more using a GridBagLayout does not work good, because i want to dynamically add more containers so it wouldn't be a good way to generate the GridBagLayout constraints on each adding.
Is there a good solution for this problem? The following is a SSCCE for my problem:
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import java.awt.Dimension;
public class DynScalingExample extends JFrame {
public DynScalingExample() {
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
//---Panel 1
JPanel panel = new JPanel();
panel.setMaximumSize(new Dimension(20, 32767));
getContentPane().add(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Test label");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
panel.add(lblNewLabel, gbc_lblNewLabel);
//---Panel 2
JPanel panel_1 = new JPanel();
getContentPane().add(panel_1);
GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[]{0, 0};
gbl_panel_1.rowHeights = new int[]{0, 0};
gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel_1.setLayout(gbl_panel_1);
JLabel label = new JLabel("Test label");
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.anchor = GridBagConstraints.WEST;
gbc_label.gridx = 0;
gbc_label.gridy = 0;
panel_1.add(label, gbc_label);
//---Panel 3
JPanel panel_2 = new JPanel();
getContentPane().add(panel_2);
GridBagLayout gbl_panel_2 = new GridBagLayout();
gbl_panel_2.columnWidths = new int[]{0, 0};
gbl_panel_2.rowHeights = new int[]{0, 0};
gbl_panel_2.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_2.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel_2.setLayout(gbl_panel_2);
JLabel label_1 = new JLabel("Test label");
GridBagConstraints gbc_label_1 = new GridBagConstraints();
gbc_label_1.anchor = GridBagConstraints.WEST;
gbc_label_1.gridx = 0;
gbc_label_1.gridy = 0;
panel_2.add(label_1, gbc_label_1);
}
}

As #Andrew Thompson suggested, a GridLayout works perfectly for this purpose. As example, see the following:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DynScalingExample extends JFrame {
public DynScalingExample() {
getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
//---Panel 1
JPanel panel = new JPanel();
getContentPane().add(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Test label");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
panel.add(lblNewLabel, gbc_lblNewLabel);
//---Panel 2
JPanel panel_1 = new JPanel();
getContentPane().add(panel_1);
GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[]{0, 0};
gbl_panel_1.rowHeights = new int[]{0, 0};
gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel_1.setLayout(gbl_panel_1);
JLabel label = new JLabel("Test label");
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.anchor = GridBagConstraints.WEST;
gbc_label.gridx = 0;
gbc_label.gridy = 0;
panel_1.add(label, gbc_label);
//---Panel 3
JPanel panel_2 = new JPanel();
getContentPane().add(panel_2);
GridBagLayout gbl_panel_2 = new GridBagLayout();
gbl_panel_2.columnWidths = new int[]{0, 0};
gbl_panel_2.rowHeights = new int[]{0, 0};
gbl_panel_2.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_2.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel_2.setLayout(gbl_panel_2);
JLabel label_1 = new JLabel("Test label");
GridBagConstraints gbc_label_1 = new GridBagConstraints();
gbc_label_1.anchor = GridBagConstraints.WEST;
gbc_label_1.gridx = 0;
gbc_label_1.gridy = 0;
panel_2.add(label_1, gbc_label_1);
}
}

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..

How to avoid components overlapping and make them smaller

I am designing a window with GridBagLayout but when I run it the components of the window are quite big when I have a ScrollPane. When I don't have scroll and window is smaller the components overlap.
This is the code I have:
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{922, 0};
gridBagLayout.rowHeights = new int[]{148, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JPanel panel_2 = new JPanel();
GridBagConstraints gbc_panel_2 = new GridBagConstraints();
gbc_panel_2.fill = GridBagConstraints.BOTH;
gbc_panel_2.gridx = 0;
gbc_panel_2.gridy = 0;
add(panel_2, gbc_panel_2);
GridBagLayout gbl_panel_2 = new GridBagLayout();
gbl_panel_2.columnWidths = new int[] {0};
gbl_panel_2.rowHeights = new int[] {0, 0};
gbl_panel_2.columnWeights = new double[]{1.0, 1.0};
gbl_panel_2.rowWeights = new double[]{1.0, 1.0};
panel_2.setLayout(gbl_panel_2);
JPanel panel_11 = new JPanel();
GridBagConstraints gbc_panel_11 = new GridBagConstraints();
gbc_panel_11.weightx = 1.0;
gbc_panel_11.weighty = 1.0;
gbc_panel_11.gridwidth = 2;
gbc_panel_11.fill = GridBagConstraints.BOTH;
gbc_panel_11.gridx = 0;
gbc_panel_11.gridy = 1;
panel_2.add(panel_11, gbc_panel_11);
GridBagLayout gbl_panel_11 = new GridBagLayout();
gbl_panel_11.columnWidths = new int[] {0};
gbl_panel_11.rowHeights = new int[] {0};
gbl_panel_11.columnWeights = new double[]{1.0};
gbl_panel_11.rowWeights = new double[]{1.0};
panel_11.setLayout(gbl_panel_11);
JPanel panel_8 = new JPanel();
panel_8.setBorder(new LineBorder(new Color(0, 0, 0)));
GridBagConstraints gbc_panel_8 = new GridBagConstraints();
gbc_panel_8.weightx = 1.0;
gbc_panel_8.weighty = 1.0;
gbc_panel_8.fill = GridBagConstraints.BOTH;
gbc_panel_8.gridx = 0;
gbc_panel_8.gridy = 0;
panel_11.add(panel_8, gbc_panel_8);
GridBagLayout gbl_panel_8 = new GridBagLayout();
gbl_panel_8.columnWidths = new int[] {0};
gbl_panel_8.rowHeights = new int[] {0, 0};
gbl_panel_8.columnWeights = new double[]{1.0};
gbl_panel_8.rowWeights = new double[]{1.0, 1.0};
panel_8.setLayout(gbl_panel_8);
JPanel panel_14 = new JPanel();
GridBagConstraints gbc_panel_14 = new GridBagConstraints();
gbc_panel_14.weighty = 0.2;
gbc_panel_14.fill = GridBagConstraints.BOTH;
gbc_panel_14.gridx = 0;
gbc_panel_14.gridy = 0;
panel_8.add(panel_14, gbc_panel_14);
GridBagLayout gbl_panel_14 = new GridBagLayout();
gbl_panel_14.columnWidths = new int[] {0, 0, 0, 0, 0};
gbl_panel_14.rowHeights = new int[]{0, 0, 0};
gbl_panel_14.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel_14.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
panel_14.setLayout(gbl_panel_14);
Label label_7 = new Label("Family");
GridBagConstraints gbc_label_7 = new GridBagConstraints();
gbc_label_7.insets = new Insets(0, 40, 0, 5);
gbc_label_7.gridx = 0;
gbc_label_7.gridy = 1;
panel_14.add(label_7, gbc_label_7);
JPanel panel_16 = new JPanel();
GridBagConstraints gbc_panel_16 = new GridBagConstraints();
gbc_panel_16.weighty = 0.8;
gbc_panel_16.fill = GridBagConstraints.BOTH;
gbc_panel_16.gridx = 0;
gbc_panel_16.gridy = 1;
panel_8.add(panel_16, gbc_panel_16);
GridBagLayout gbl_panel_16 = new GridBagLayout();
gbl_panel_16.columnWidths = new int[] {0, 0, 0, 0};
gbl_panel_16.rowHeights = new int[] {0, 0};
gbl_panel_16.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel_16.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel_16.setLayout(gbl_panel_16);
TextArea textArea_2 = new TextArea();
GridBagConstraints gbc_textArea_2 = new GridBagConstraints();
gbc_textArea_2.weighty = 0.5;
gbc_textArea_2.weightx = 0.5;
gbc_textArea_2.fill = GridBagConstraints.BOTH;
gbc_textArea_2.insets = new Insets(0, 0, 0, 5);
gbc_textArea_2.gridx = 0;
gbc_textArea_2.gridy = 0;
panel_16.add(textArea_2, gbc_textArea_2);
textArea_2.setText("TEXT...");
Label label_9 = new Label("Language");
GridBagConstraints gbc_label_9 = new GridBagConstraints();
gbc_label_9.weightx = 0.25;
gbc_label_9.fill = GridBagConstraints.BOTH;
gbc_label_9.insets = new Insets(0, 0, 0, 5);
gbc_label_9.gridx = 1;
gbc_label_9.gridy = 0;
panel_16.add(label_9, gbc_label_9);
Choice choice_13 = new Choice();
GridBagConstraints gbc_choice_13 = new GridBagConstraints();
gbc_choice_13.weightx = 0.25;
gbc_choice_13.fill = GridBagConstraints.HORIZONTAL;
gbc_choice_13.gridx = 2;
gbc_choice_13.gridy = 0;
panel_16.add(choice_13, gbc_choice_13);
And the result is:
Overlapping components
What can I do to stop the components overlapping?
How can I make the components smaller?

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

Repeat WindowBuilder JLabel using Grid Layout

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.

Categories