Dock objects in the corner - MigLayout - java

How to dock three or more components in the corner eg. south-east using MigLayout ?
What I' ve tried:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
public class App {
public static void launchView(){
JFrame frame = new JFrame("Foo");
frame.setLayout(new MigLayout());
JButton b1 = new JButton("Sample1");
JButton b2 = new JButton("Sample2");
JButton b3 = new JButton("Sample3");
frame.add(b1, "dock south, east");
frame.add(b2);
frame.add(b3);
frame.setSize(new Dimension(600, 200));
frame.setVisible(true);
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
launchView();
}
});
}
}
In the picture you can see buttons showed in the left corner in the wrong order, but I would like to have them always in the south-west corner, one next to another (Button1 | Button2 | Button3).

Try using SceneBuilder, a plug in available for Eclipse and NetBeans.You can easily construct GUI's using that tool.
A potential solution for yourself is to use this:
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new MigLayout("", "[grow,right]", "[grow,bottom]"));
JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(btnNewButton, "flowx,cell 0 0");
JButton btnNewButton_1 = new JButton("New button");
frame.getContentPane().add(btnNewButton_1, "cell 0 0");
JButton btnNewButton_2 = new JButton("New button");
frame.getContentPane().add(btnNewButton_2, "cell 0 0");

Related

Problem with buttons exceeding the Panel (JAVA)

I'm having a problem with a layout I'm trying to do in java. I have 2 panels in a 800x600 frame. The first panel "gamePanel" is (600x600) and the second "menuPanel" is (200x600).
In the menuPanel there are 4 buttons that I tried to organize as a single column of 4 rows using gridLayout(which partially worked). The buttons appear to be in place but when hovering on them they expand occupying the other panel (gamePanel). I tried placing them using setBounds but they directly disappear.
This is how it works before hovering the buttons.
After hovering 2 buttons, but all 4 are displayed the same way
Here is the code:
public class Layout {
Point point = new Point();
public Layout() {
JFrame window = new JFrame();
ImageIcon icon = new ImageIcon("images/icon.jpg");
//JFRAME
window.setSize(800,600);
window.setLocationRelativeTo(null);
window.setTitle("Arkanoid");
window.setUndecorated(true);
window.setIconImage(icon.getImage());
//PANELS
JPanel gamePanel = new JPanel();
gamePanel.setBackground(Color.RED);
gamePanel.setBounds(0, 0, 600, 600);
JPanel menuPanel = new JPanel(new GridLayout(4,1));
menuPanel.setBackground(Color.BLACK);
menuPanel.setBounds(600,0,200,600);
menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
window.add(gamePanel);
window.add(menuPanel);
//Buttons
JButton closeButton = new JButton("Close Me");
closeButton.addActionListener(e -> System.exit(0));
menuPanel.add(closeButton);
JButton playButton = new JButton("Play");
menuPanel.add(playButton);
JButton Button1 = new JButton("Test1");
menuPanel.add(Button1);
JButton Button2 = new JButton("Test2");
menuPanel.add(Button2);
//Labels
//SHOW
window.setVisible(true);
}
}
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Laying Out Components Within a Container section.
A JFrame has a default BorderLayout, which I used to place the two JPanels.
I added a main method so I could run the GUI. I commented out the icon code, which doesn't make any sense for an undecorated JFrame. I moved the setLocationRelativeTo method to after the pack method, so the JFrame is actually centered.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ExampleLayout {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ExampleLayout();
}
});
}
Point point = new Point();
public ExampleLayout() {
JFrame window = new JFrame();
// ImageIcon icon = new ImageIcon("images/icon.jpg");
// JFRAME
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle("Arkanoid");
window.setUndecorated(true);
// window.setIconImage(icon.getImage());
// PANELS
JPanel gamePanel = new JPanel();
gamePanel.setBackground(Color.RED);
gamePanel.setPreferredSize(new Dimension(600, 600));
JPanel menuPanel = new JPanel(new GridLayout(0, 1));
menuPanel.setBackground(Color.BLACK);
menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
menuPanel.setPreferredSize(new Dimension(200, 600));
window.add(gamePanel, BorderLayout.CENTER);
window.add(menuPanel, BorderLayout.EAST);
// Buttons
JButton closeButton = new JButton("Close Me");
closeButton.addActionListener(e -> System.exit(0));
menuPanel.add(closeButton);
JButton playButton = new JButton("Play");
menuPanel.add(playButton);
JButton Button1 = new JButton("Test1");
menuPanel.add(Button1);
JButton Button2 = new JButton("Test2");
menuPanel.add(Button2);
// Labels
// SHOW
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}

Swing resizing elements

I'm just trying out swing for java developement and can't for the life of me work out how to make a button have a specific size, or at least size how I want it to...
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI implements ActionListener {
int count = 0;
JLabel label;
public GUI() {
JFrame frame = new JFrame("GUI");
JButton button = new JButton("CLick this");
button.addActionListener(this);
button.setPreferredSize(new Dimension(256, 256));
label = new JLabel("Number of clicks: 0");
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setPreferredSize(new Dimension(1000, 700));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
#Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number of clicks " + count);
}
}
This is my basic code - I want to make the button a specific size, or size to the width of the panel, how do I do this?

Can I use a for loop to create objects in java?

Can I initialize multiple objects in a single loop?
Here's what a snippet of my code looks like. As you can see, it becomes hard to look at from a glance and takes up too much space.
I'd like to be able to create the buttons in one for loop and then modify in another for loop.
public class MyFrame extends JFrame {
MyFrame() {
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
JButton button7 = new JButton();
JButton button8 = new JButton();
JButton button9 = new JButton();
JButton button0 = new JButton();
button1.setBounds(60, 60, 50, 50);
button2.setBounds(120,60,50,50);
button3.setBounds(180,60,50,50);
button4.setBounds(60,120,50,50);
button5.setBounds(120,120,50,50);
button6.setBounds(180,120,50,50);
button7.setBounds(60,180,50,50);
button8.setBounds(120,180,50,50);
button9.setBounds(180,180,50,50);
button0.setBounds(120,240,50,50);
}
}
Yes, you can use a for loop to create the buttons.
Here's a GUI I created.
It's not a good idea to use absolute positioning (setBounds) when creating a GUI. You should use the Swing layout managers.
I used a GridLayout to position the buttons.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TenButtonGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new TenButtonGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("Ten Button GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridLayout(0, 3, 5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
for (int i = 0; i < 11; i++) {
if (i == 9) {
JLabel label = new JLabel(" ");
panel.add(label);
} else {
JButton button = new JButton();
button.setPreferredSize(new Dimension(50, 50));
panel.add(button);
}
}
return panel;
}
}
You could do something like this:
List<JButton> buttons = new ArrayList<>();
int[][] bounds = {{60, 60, 50, 50}, {120,60,50,50}}; //add more bound quadruplets
// iterating over the values in the bounds array
Arrays.asList(bounds).forEach(v -> {
JButton button = new JButton();
button.setBounds(v[0], v[1], v[2], v[3]);
buttons.add(button);
});
In the end you will find your buttons in the buttons List.

Looking for a simple way to change text in a JTextField with a button click

I'm trying to get the action listener and button called French to change the text in the Output field to say Bonjour but many errors come up in the console when I click the button. I would like to do this in as few lines as possible.
import java.awt.BorderLayout;
import java.awt.Color;
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.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
public class TranslatorFrame {
public static void main(String[] args) {
JFrame Words = new JFrame("Greetings Translator");
Words.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Did some research to find the line so the program
//would appear in the center of the screen
Font Font1 = new Font("SansSerif",Font.BOLD,40);
JPanel WorkSpace = new JPanel();
JTextField Instructions = new JTextField("Enter \"Hello\" Here: ");
JTextField Input = new JTextField();
JPanel Center = new JPanel();
Center.setBackground(Color.lightGray);
Center.add(Instructions, BorderLayout.EAST);
Center.add(Input, BorderLayout.WEST);
JTextField Output = new JTextField("");
Output.setFont(Font1);
Output.setPreferredSize(new Dimension(195, 100));
JTextField Header = new JTextField();
Header.setPreferredSize(new Dimension(195, 100));
Input.setPreferredSize(new Dimension(150,50));
Input.setFont(Font1);
Instructions.setPreferredSize(new Dimension(375,50));
Instructions.setBackground(Color.cyan);
//set the back ground color of my Instructions field so that it wouldn't
//look so gray and boring. There don't seem to be many preset colors to choose from.
Instructions.setFont(Font1);
Instructions.setEditable(false);
JPanel ButtonsArea = new JPanel();
ButtonsArea.setBackground(Color.lightGray);
ButtonsArea.setPreferredSize(new Dimension(300,300));
JButton French = new JButton("French");
French.setPreferredSize(new Dimension(200, 150));
French.setFont(Font1);
French.addActionListener(new ChangeListener());
JButton German = new JButton("German");
German.setPreferredSize(new Dimension(200, 150));
German.setFont(Font1);
JButton Spanish = new JButton("Spanish");
Spanish.setPreferredSize(new Dimension(200, 150));
Spanish.setFont(Font1);
ButtonsArea.add(French, BorderLayout.EAST);
ButtonsArea.add(German, BorderLayout.CENTER);
ButtonsArea.add(Spanish, BorderLayout.WEST);
ButtonsArea.add(Output,BorderLayout.SOUTH);
//Tried many different things to get the buttons and textfields
//to have space between them but nothing seems to work.
//Header.setSize(100,100);
Header.setFont(Font1);
Header.setText("Welcome!");
Header.setEditable(false);
WorkSpace.setBackground(Color.LIGHT_GRAY);
class ChangeListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource()==French);
Output.setText("Bonjour!");
}}
Words.pack();
Words.setSize(new Dimension(1000, 600));
Words.setLocationRelativeTo(null);
WorkSpace.add(Header);
Words.add(WorkSpace, BorderLayout.NORTH);
Words.add(Center, BorderLayout.CENTER);
/*Words.add(Instructions, BorderLayout.CENTER);
Words.add(Input, BorderLayout.CENTER);*/
Words.add(ButtonsArea, BorderLayout.SOUTH);
//Words.add(Output, BorderLayout.SOUTH);
Words.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
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.JPanel;
import javax.swing.JTextField;
public class TranslatorFrame {
public static void main(String[] args) {
JFrame Words = new JFrame("Greetings Translator");
Words.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Did some research to find the line so the program
// would appear in the center of the screen
Font Font1 = new Font("SansSerif", Font.BOLD, 40);
JPanel WorkSpace = new JPanel();
JTextField Instructions = new JTextField("Enter \"Hello\" Here: ");
JTextField Input = new JTextField();
JPanel Center = new JPanel();
Center.setBackground(Color.lightGray);
Center.add(Instructions, BorderLayout.EAST);
Center.add(Input, BorderLayout.WEST);
final JTextField Output = new JTextField("");
Output.setFont(Font1);
Output.setPreferredSize(new Dimension(195, 100));
JTextField Header = new JTextField();
Header.setPreferredSize(new Dimension(195, 100));
Input.setPreferredSize(new Dimension(150, 50));
Input.setFont(Font1);
Instructions.setPreferredSize(new Dimension(375, 50));
Instructions.setBackground(Color.cyan);
// set the back ground color of my Instructions field so that it
// wouldn't
// look so gray and boring. There don't seem to be many preset colors to
// choose from.
Instructions.setFont(Font1);
Instructions.setEditable(false);
JPanel ButtonsArea = new JPanel();
ButtonsArea.setBackground(Color.lightGray);
ButtonsArea.setPreferredSize(new Dimension(300, 300));
final JButton French = new JButton("French");
French.setPreferredSize(new Dimension(200, 150));
French.setFont(Font1);
French.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == French)
;
Output.setText("Bonjour!");
}
});
JButton German = new JButton("German");
German.setPreferredSize(new Dimension(200, 150));
German.setFont(Font1);
JButton Spanish = new JButton("Spanish");
Spanish.setPreferredSize(new Dimension(200, 150));
Spanish.setFont(Font1);
ButtonsArea.add(French, BorderLayout.EAST);
ButtonsArea.add(German, BorderLayout.CENTER);
ButtonsArea.add(Spanish, BorderLayout.WEST);
ButtonsArea.add(Output, BorderLayout.SOUTH);
// Tried many different things to get the buttons and textfields
// to have space between them but nothing seems to work.
// Header.setSize(100,100);
Header.setFont(Font1);
Header.setText("Welcome!");
Header.setEditable(false);
WorkSpace.setBackground(Color.LIGHT_GRAY);
Words.pack();
Words.setSize(new Dimension(1000, 600));
Words.setLocationRelativeTo(null);
WorkSpace.add(Header);
Words.add(WorkSpace, BorderLayout.NORTH);
Words.add(Center, BorderLayout.CENTER);
/*
* Words.add(Instructions, BorderLayout.CENTER); Words.add(Input,
* BorderLayout.CENTER);
*/
Words.add(ButtonsArea, BorderLayout.SOUTH);
// Words.add(Output, BorderLayout.SOUTH);
Words.setVisible(true);
}
}

PictureCount does not change

the code is used to create an application which shows picture and if your anwser is correct your supposed to see the next picture but the pictureCount does not go up. all of the variables ar declared after the main class and i created an Actionlistener to check if the awnser is correct.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {new Main().test();}
public int pictureCount = 1;
JFrame frame = new JFrame();
JButton button1 = new JButton("Submit");
JTextField text = new JTextField();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JLabel label = new JLabel(new ImageIcon("C:\\Users\\Admin\\Desktop\\practicum 3\\" + pictureCount + ".jpg"));
void test(){
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(text.getText().equals("5")){
pictureCount++;
new Main().test();
}
}
});
panel1.add(button1);
panel2.add(text);
panel3.add(label);
text.setPreferredSize(new Dimension(100,50));
panel1.setPreferredSize(new Dimension(1000, 200));
panel2.setPreferredSize(new Dimension(1000, 100));
panel3.setPreferredSize(new Dimension(1000, 450));
frame.getContentPane().add(BorderLayout.SOUTH, panel1);
frame.getContentPane().add(BorderLayout.CENTER, panel2);
frame.getContentPane().add(BorderLayout.NORTH, panel3);
frame.setSize(1000,750);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Operation Screen");
frame.setLocationRelativeTo(null);
}
}
You need to read in all the pictures as ImageIcons into an array or ArrayList, say called imageIconArray and then display imageIconArray[0] in your JLabel when you start.
When the button is pressed, increment pictureCount, and then reset the JLabel's icon via its setIcon(...) method:
// in the ActionListener code:
pictureCount++;
label.setIcon(imageIconArray[pictureCount];
Whatever you do, don't create a new Main object, despite what others might say. Why create a new GUI when all you need to do is swap displayed images?

Categories