'How do i add another button in JButton
I have 1 button i. but i need another. i added another JFrame and made a new class making the button. whenever i do
frame.add(new TestButton());
it never works. i already have
frame.add(new TestPanel());
which works. but its the only working one. here is my TestButton code
package App.Gui.Buttons;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import App.Gui.Event.ExitEvent;
public class TestButton extends JPanel {
public TestButton() {
setLayout(new GridBagLayout());
JButton button = new JButton();
button.setBackground(new Color(0, 0, 0, 0));
button.setForeground(new Color(0, 0, 0, 0));
button.setBorderPainted(false);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ExitEvent.exit();
}
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.insets = new Insets(20, 50, 20, 50);
add(button, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
JPanel contentPane = new JPanel(new GridBagLayout());
contentPane.setBackground(Color.GREEN);
contentPane.add(new JLabel("Books app"));
add(contentPane, gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(1200, 750);
}
public static void addButton() {
new TestButton();
}
}
i need to add another button to do more stuff like you would need in a software. but it either never starts or, it bugs the buttons. pls help.
Solution 1:[1]
A JPanel can't be shown in of itself. It needs to be added to a container hierarchy which is backed by a window based class, like JFrame.
This is a pretty basic concept, which suggests that you might be better off spending some time going through Creating a GUI With Swing, especially How to Make Frames (Main Windows)
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestButton());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Also, JFrame uses a BorderLayout by default, so you'll only be able to present a single component at each of it's five available positions.
So, if instead, you used a GridLayout, you could get multiple instances of TestButton on the window at the same time, for example
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(2, 1));
frame.add(new TestButton());
frame.add(new TestButton());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Now, Swing components also don't support alpha based colors, they are either opaque or they are not (no translucency - you can fake it, but that's beyond the scope)
So, I'd modify you code to look more like...
JButton button = new JButton();
//button.setBackground(new Color(0, 0, 0, 0));
//button.setForeground(new Color(0, 0, 0, 0));
button.setBorderPainted(false);
button.setOpaque(false);
button.setContentAreaFilled(false);
Also, good luck on been able to click that button by the way
Full code...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestButton extends JPanel {
public TestButton() {
setLayout(new GridBagLayout());
JButton button = new JButton();
// button.setBackground(new Color(0, 0, 0, 0));
// button.setForeground(new Color(0, 0, 0, 0));
button.setBorderPainted(false);
button.setOpaque(false);
button.setContentAreaFilled(false);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//ExitEvent.exit();
}
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.insets = new Insets(20, 50, 20, 50);
add(button, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
JPanel contentPane = new JPanel(new GridBagLayout());
contentPane.setBackground(Color.GREEN);
contentPane.add(new JLabel("Books app"));
add(contentPane, gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(1200, 750);
}
}
There are also issues with updating a realised window (adding/removing components once the window is visible on the screen). Swing is lazy and you are required to request a layout and paint pass on the container you've modified (invalidate and repaint).
But you might find CardLayout more suitable to your needs instead - but since we don't have a runnable example, it's hard to know
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
