ActionListener in JFrames - java

There is an issue with the code where the ActionListener will not pick up on any action when i call upon it with the buttons. I have done this before but this is my first time attempting a GridBagLayout and it doesn't seem to be working correctly. The GUI is fully functional. What could I do to get the ActionListener to work? Here is the entire class.
package body;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class mainFrame extends JFrame implements ActionListener{
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
static String string = "default";
static JButton one, two, three, four, five, six, seven, eight, nine, zero;
static JButton plus, minus, multiply, divide, equals;
public void init() {
one.setActionCommand("one");
one.addActionListener(this);
two.setActionCommand("two");
two.addActionListener(this);
three.setActionCommand("three");
three.addActionListener(this);
four.setActionCommand("four");
four.addActionListener(this);
five.setActionCommand("five");
five.addActionListener(this);
six.setActionCommand("six");
six.addActionListener(this);
seven.setActionCommand("seven");
seven.addActionListener(this);
eight.setActionCommand("eight");
eight.addActionListener(this);
nine.setActionCommand("nine");
nine.addActionListener(this);
zero.setActionCommand("zero");
zero.addActionListener(this);
plus.setActionCommand("plus");
plus.addActionListener(this);
minus.setActionCommand("minus");
minus.addActionListener(this);
multiply.setActionCommand("multiply");
multiply.addActionListener(this);
divide.setActionCommand("divide");
divide.addActionListener(this);
equals.setActionCommand("equals");
equals.addActionListener(this);
}
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
JLabel display = new JLabel(string);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(display, c);
one = new JButton("1");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
pane.add(one, c);
two = new JButton("2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(two, c);
three = new JButton("3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 1;
pane.add(three, c);
plus = new JButton("+");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 1;
pane.add(plus, c);
four = new JButton("4");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
pane.add(four, c);
five = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
pane.add(five, c);
six = new JButton("6");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 2;
pane.add(six, c);
minus = new JButton("-");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 2;
pane.add(minus, c);
seven = new JButton("7");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
pane.add(seven, c);
eight = new JButton("8");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
pane.add(eight, c);
nine = new JButton("9");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 3;
pane.add(nine, c);
multiply = new JButton("*");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 3;
pane.add(multiply, c);
zero = new JButton("0");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 4;
pane.add(zero, c);
equals = new JButton("Equals");
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1.0;
c.gridx = 1;
c.gridwidth = 2;
c.gridy = 4;
pane.add(equals, c);
divide = new JButton("/");
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_END;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 4;
pane.add(divide, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "one") {
System.out.println("hello");
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Besides the == and equals I pointed out, you never call init(), which initializes your action commands.
Also note, you class is already a JFrame. Either use the class JFrame or use the instance JFrame.
You should fix the above problem so that you can properly call your init() method.
Try this
private static void createAndShowGUI() {
mainFrame frame = new mainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
addComponentsToPane(frame.getContentPane());
frame.init(); <<<<<================== Don't forget about meeeee!
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if ("one".equals(e.getActionCommand())) {
System.out.println("hello");
}
}
Also you should be following Java naming convention. Class names begin with capital letters. So mainFrame → MainFrame

Related

JFrame buttons act weird when declaring it to a static variable

So, I am having some really weird stuff going on here.
So, my entire class is this:
public class Test extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10,0,0,0);
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 3;
pane.add(button, c);
}
private JFrame createAndShowGUI(JFrame frame) {
frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
return frame;
}
JFrame frame = null;
public Test() {
createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
}
So anyway, I am going to focus on this bit:
JFrame frame = null;
public Test() {
createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
Which, produces this result (which works perfectly fine).
However, when I use this code:
JFrame frame = null;
public Test() {
frame = createAndShowGUI(frame);
addComponentsToPane(frame.getContentPane());
}
Which results in buttons 4 and 5 somehow duplicating themselves with a set size at the top of the screen. Also, buttons 4 and 5 appear to have different sizes.
Like this:
(And when I make the window smaller)
What is causing this? All I am doing is setting a variable from null or a JFrame, which should do nothing.
The first method throws a NullPointerException at the addComponentsToPane method because the frame attribute is never initialized and the second method adds the buttons twice to the frame. I have rewritten the code below and it works fine. You should also have a look at variable shadowing , because your frame attribute gets shadowed by the frame local variable in the addComponentsToPane method whick is rarely a good idea.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (true) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10, 0, 0, 0);
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 3;
pane.add(button, c);
}
private void createAndShowGUI() {
frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
JFrame frame = null;
public Test() {
createAndShowGUI();
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}

Cannot access data from another class

my name is Gab.
I'm making a program that will make vanish one page for an other by clicking a button. The thing is, I can't pass the appropriate datas from my two classes. It may sounds weird like this, but here's my code:
My first class (class1):
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
public class class1 {
public static void main(String[] args) {
JPanel panel = new JPanel();
JFrame frame = new JFrame();
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contenu.setLayout(gridBag);
boolean start = false;
JLabel label1 = new JLabel(
"<html><p><span style = 'font-size: 18px; font-color: blue'>The Number's Genius </span>(version 1.4)</p></html>");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 4;
c.ipadx = 500;
panel.add(label1, c);
JLabel xxx = new JLabel("");
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipadx = 550;
panel.add(xxx, c);
JButton start = new JButton("<html><p><b>Start</b></p></html>");
c.gridx = 3;
c.gridy = 4;
c.gridwidth = 1;
c.ipadx = 1;
c.anchor = GridBagConstraints.LINE_END;
panel.add(start, c);
JLabel msg1 = new JLabel(
"<html><p style = 'background-color: white; font-size: 10px'>Click on Start to begin.</p></html>");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 4;
panel.add(msg1, c);
JLabel msg2 = new JLabel(
"<html><p style = 'background-color: white; font-size: 10px'>Ok, let's begin.</p><p style = 'background-color: white; font-size: 10px'>First question: Is your number even?</html>");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 4;
panel.add(msg1, c);
Moteur moteur = new Moteur();
start.addActionListener(moteur);
frame.setContentPane(contenu);
frame.setSize(700, 300);
frame.setVisible(true);
frame.setResizable(false);
}
}
You might notice that the second class has some text in french, but don't worry, it's not important. Here's my second class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Moteur implements ActionListener {
public void actionPerformed(ActionEvent e) {
msg1.setVisible(false);
msg2.setVisible(true);
}
}
In fact, I want the program to make 'msg1' visibility to false and 'msg2' visibility to true, but I can't make the program work because the second class don't know what is 'msg1' and 'msg2'. Please, help me!
Regards, -Gab
For a method to be able to use an object, it needs a reference to this object. You're creating a Moteur instance, but you don't pass any object to this Moteur instance, so it doesn't have a reference to any object except itself.
For the Moteur class to be able to call methods of msg1 and msg2, you need to pass a reference to these two objects to Moteur:
public class Moteur implements ActionListener {
private JLabel messageToHide;
private JLabel messageToShow;
public Moteur(JLabel messageToHide, JLabel messageToShow) {
this.messageToHide = messageToHide;
this.messageToShow = messageToShow;
}
public void actionPerformed(ActionEvent e) {
messageToHide.setVisible(false);
messageToShow.setVisible(true);
}
}
And then, when you create the Moteur, you give them the two labels to hide and show:
Moteur moteur = new Moteur(msg1, msg2);
Use getters and setters for msg's.

Gridbag layout resizes when I add components

I am new to gridbag layout. While doing testing, I noticed that my grid bag layout does not retain specific size (grid size) when I add components.
I want my application to be compatible with various screensizes, so I can not hardcode the size in advance.
What could I be doing wrong. Am I not understanding the way grids should be designed?
Update: All I had to do was to set the preferred Size of the component. It they stopped growing arbitrarily.
code:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Main extends JFrame {
// Drawing panels
private JPanel panelLeft;
private JPanel panelCenter;
private JPanel panelRight;
private JPanel panelTopCenter, panelTopLeft, panelTopRight;
private JPanel storyPanel;
// Layout
private GridBagLayout gridBadLayout;
private static final String DEFAULT_GAME_WORLD_NAME = "DefaultWorldName";
// Constructor
public Main() {
initializePanels();
}
/*
* Initializes look and feel of the window. No components should be
* initalized in this because controller would like to lazily initialize
* them once data is avialable.
*/
private void initializePanels() {
this.setTitle("World Maker");
this.setBounds(55, 5, 600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
gridBadLayout = new GridBagLayout();
panel.setLayout(gridBadLayout);
GridBagConstraints c = new GridBagConstraints();
this.add(panel);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.3;
c.weighty = 0.1;
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.BOTH;
panelTopLeft = new JPanel();
panelTopLeft.setBackground(Color.WHITE);
panelTopLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopLeft, c);
c.gridx = 1;
c.gridwidth = 3;
panelTopCenter = new JPanel();
panelTopCenter.setBackground(Color.WHITE);
panelTopCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopCenter, c);
c.gridx = 4;
c.gridwidth = 1;
panelTopRight = new JPanel();
panelTopRight.setBackground(Color.WHITE);
panelTopRight.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopRight, c);
c.gridx = 0;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 1;
c.weightx = 0.3;
c.weighty = 1.0;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
panelLeft = new JPanel();
panelLeft.setBackground(Color.WHITE);
panelLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelLeft, c);
c.gridx = 1;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 3;
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
panelCenter = new JPanel();
panelCenter.setBackground(Color.WHITE);
panelCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelCenter, c);
c.gridx = 4;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 1;
c.weightx = 0.3;
c.weighty = 1.0;
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.BOTH;
panelRight = new JPanel();
panelRight.setBackground(Color.WHITE);
panelRight.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelRight, c);
addRandomComponents();
this.setVisible(true);
panel.setOpaque(true);
this.validate();
}
private void addRandomComponents() {
//add JButton to panelTopLeft
panelTopLeft.add(new JButton("TEST1"));
panelTopLeft.add(new JButton("TEST2"));
panelTopLeft.add(new JButton("TEST3"));
//add combo box to panelRight
panelRight.add(new JComboBox(Arrays.asList("TEST1", "TEST2").toArray()));
}
public static void main(String[] args) {
Main main = new Main();
}
}
Screenshots:
1) Before I add any components
2) After I add components (see the left top panel)
This happens because there is no preferred size set for the components. Once set, components do not grow larger than the size.
panelTopLeft.setPreferredSize(new Dimension(100, 0));
Complete fixed code.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Main extends JFrame {
// Drawing panels
private JPanel panelLeft;
private JPanel panelCenter;
private JPanel panelRight;
private JPanel panelTopCenter, panelTopLeft, panelTopRight;
private JPanel storyPanel;
// Layout
private GridBagLayout gridBadLayout;
private static final String DEFAULT_GAME_WORLD_NAME = "DefaultWorldName";
// Constructor
public Main() {
initializePanels();
}
/*
* Initializes look and feel of the window. No components should be
* initalized in this because controller would like to lazily initialize
* them once data is avialable.
*/
private void initializePanels() {
this.setTitle("World Maker");
this.setBounds(55, 5, 600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
gridBadLayout = new GridBagLayout();
panel.setLayout(gridBadLayout);
GridBagConstraints c = new GridBagConstraints();
this.add(panel);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.3;
c.weighty = 0.1;
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.BOTH;
panelTopLeft = new JPanel();
panelTopLeft.setBackground(Color.WHITE);
panelTopLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopLeft, c);
c.gridx = 1;
c.gridwidth = 3;
panelTopCenter = new JPanel();
panelTopCenter.setBackground(Color.WHITE);
panelTopCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopCenter, c);
c.gridx = 4;
c.gridwidth = 1;
panelTopRight = new JPanel();
panelTopRight.setBackground(Color.WHITE);
panelTopRight.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopRight, c);
c.gridx = 0;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 1;
c.weightx = 0.3;
c.weighty = 1.0;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
panelLeft = new JPanel();
panelLeft.setBackground(Color.WHITE);
panelLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelLeft, c);
c.gridx = 1;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 3;
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
panelCenter = new JPanel();
panelCenter.setBackground(Color.WHITE);
panelCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelCenter, c);
c.gridx = 4;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 1;
c.weightx = 0.3;
c.weighty = 1.0;
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.BOTH;
panelRight = new JPanel();
panelRight.setBackground(Color.WHITE);
panelRight.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelRight, c);
addRandomComponents();
this.setVisible(true);
panel.setOpaque(true);
this.validate();
}
private void addRandomComponents() {
//add JButton to panelTopLeft
panelTopLeft.add(new JButton("TEST1"));
panelTopLeft.add(new JButton("TEST2"));
panelTopLeft.add(new JButton("TEST3"));
panelTopLeft.setPreferredSize(new Dimension(100, 0));
//add combo box to panelRight
panelRight.add(new JComboBox(Arrays.asList("TEST1", "TEST2").toArray()));
}
public static void main(String[] args) {
Main main = new Main();
}
}

Vertical filling of GridBagLayout in Java does not work

I am having troubles getting the layout right.
I have a Class which extends JFrame and has a JPanel which has a the GridBagLayout as layout manager. I want 4 buttons which should be layout in this way
Somehow the vertical filling does not work.
I tried various ways but can't figure out how to make this work.
Sorry if this is a noob question, but tried it for more than an hour without any change :/
public class ButtonPanel extends JFrame {
private static final long serialVersionUID = 1L;
public ButtonPanel() {
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
frame();
}
JButton objectsBtn = new JButton("Objekte"); //TODO Strings einfügen
JButton tenantBtn = new JButton("Mieter"); //TODO Strings einfügen
JButton ownerBtn = new JButton("Eigentümer"); //TODO Strings einfügen
JButton optionsBtn = new JButton("Einstellungen"); //TODO Strings einfügen
JPanel panel = new JPanel();
public void frame(){
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.RELATIVE;
c.gridheight = GridBagConstraints.RELATIVE;
//Elemente
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
panel.add(objectsBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 1;
c.gridy = 0;
panel.add(optionsBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 1;
panel.add(ownerBtn, c);
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 1;
c.gridy = 1;
panel.add(tenantBtn, c);
this.add(panel);
objectsBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
new Object();
}
});
ownerBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//TODO Eigentümer erstellen
}
});
}
}
greets
THE-E
EDIT: Found the bug
I used Seaglass as Look and Feel in the main-method
//Set Theme
try {
UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
It works fine with the default Look and Feel Theme.
I have added the answer in the first post. It was a bug caused by the LookAndFeel skin. :/
greets
THE-E

Java Swing - realising a layout with LayeredPane

I have a question regarding making a specific Layout, first I'll show examples then I will add some extra clarification.
Layout when Friends and Messages are closed:
Layout when Friends and Messages are opened:
I intend to make this layout with Java Swing.
My intention is to firstly have the Frame divided in three areas, the top menu row, the main panel and the bottom menu row.
I was thinking of using a BorderLayout for this part.
Then the Friends and Messages buttons should be toggle button's, and on toggle they should show an overlay on top of the mainpanel (or whatever is there), containing a friend list and a message area. I realised I need to use a LayeredPane somehow for this.
Another important part is that the Layout should be viewable in any size, that is the user may resize the application and it will be used on a various amount of resolutions, so I don't really want anything with a fixed width and height.
But I am really lost as how to combine this, so therefore I ask your help.
Hopefully I have explained enough about the situation.
Regards.
this could be about overlay, because JPanel can contains other JComponents
use JLayer (Java7) based on JXLayer(Java6),
use GlassPane with JComponents layed to rellative to....
easiest could be to use JDialog(undecorated) layed to Point (setLocation(int, int)), setVisible() must be wrapped into invokeLater
I will use gridBagLayout.
Here is small example including button which hide your yellow panels:
package Core;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Panel;
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;
public class GridBagLayoutDemo {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
add1row(pane);
addmainRow(pane);
addLastRow(pane);
}
private static void addLastRow(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
c.anchor = GridBagConstraints.PAGE_END;
JPanel bottonPanel = new JPanel();
bottonPanel.setBackground(Color.BLUE);
bottonPanel.setLayout(new GridBagLayout());
pane.add(bottonPanel, c);
JPanel messagePanel = new JPanel();
messagePanel.setBackground(Color.GRAY);
messagePanel.add(new JLabel("MESSAGES"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
bottonPanel.add(messagePanel, c);
}
private static void addmainRow(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.CENTER;
JPanel mainManel = new JPanel();
mainManel.setBackground(Color.GREEN);
mainManel.setLayout(new GridBagLayout());
pane.add(mainManel, c);
final JPanel friendListPanel = new JPanel();
friendListPanel.setBackground(Color.YELLOW);
friendListPanel.add(new JLabel("FRIEND LIST"));
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.FIRST_LINE_END;
mainManel.add(friendListPanel, c);
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 1;
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.CENTER;
JButton button = new JButton("On/Off");
mainManel.add(button, c);
final JPanel messageAreaPanel = new JPanel();
messageAreaPanel.setBackground(Color.YELLOW);
messageAreaPanel.add(new JLabel("MESSAGE PANEL"));
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 2;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.LAST_LINE_END;
mainManel.add(messageAreaPanel, c);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
friendListPanel.setVisible(!friendListPanel.isVisible());
messageAreaPanel.setVisible(!messageAreaPanel.isVisible());
}
});
}
private static void add1row(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.PAGE_START;
Panel headerPanel = new Panel();
headerPanel.setLayout(new GridBagLayout());
headerPanel.setBackground(Color.BLUE);
pane.add(headerPanel, c);
JPanel quitPanel = new JPanel();
quitPanel.setBackground(Color.GRAY);
quitPanel.add(new JLabel("QUIT"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
headerPanel.add(quitPanel, c);
JPanel friendsPanel = new JPanel();
friendsPanel.setBackground(Color.GRAY);
friendsPanel.add(new JLabel("FRIENDS"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.gridx = 1;
c.gridy = 0;
headerPanel.add(friendsPanel, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame);
// uncoment to use full screen
// frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setSize(new Dimension(400, 400));
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Categories