Java swing add Components from another Class - java

Im learning about Java Swing components and I want to do that when I push button, Java Swing would add label from another class into JFrame screen. Its just simple example for start.
I want to learn how to use and add swing components from another class.
There can be some stupid mistakes, but dont judge me, im new ^^
Frame class add button
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame extends JFrame{
private JButton btn;
private boolean regCompl = false;
public Frame(){
super("The title Macas");
setLayout(new FlowLayout());
btn = new JButton("Push for Registration");
btn.addActionListener(
new ActionListener(){
#Override
public void actionPerformed(ActionEvent event) {
regCompl = true;
}
}
);
add(btn);
if(regCompl == true){
RegComplete regObj = new RegComplete(this);
}
}// end of constructor
}
RegComplete Class add label to screen after button are pressed.
import javax.swing.JButton;
import javax.swing.JLabel;
public class RegComplete {
Frame frame;
private JLabel label;
public RegComplete(Frame fm){
this.frame = fm;
label = new JLabel("Hello world Mac4s");
fm.add(label);
}
}

You have to create Object inside the action Listener
btn = new JButton("Push for Registration");
btn.addActionListener(
new ActionListener(){
#Override
public void actionPerformed(ActionEvent event) {
RegComplete regObj = new RegComplete(Frame.this);
}
}
);

Related

Replace button with a filled circle?

How to replace button shown on code with a filled circled.The circle (e.g. red) must move away from cursor just as same it was with button.
Is it another possible way to implement this type of task?
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class NewSwingTemplate {
private Random d = new Random();
public NewSwingTemplate() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(createButton());
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JButton createButton() {
final JButton button = new JButton("Button");
button.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
int a = d.nextInt(200);
int b = d.nextInt(200);
button.setLocation(a, b);
}
});
return button;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NewSwingTemplate();
}
});
}
}
Create your own button.
That is, you derive from JComponent and overwrite the paint method. It needs to render your circle - the way you want it. Add some keyboard/mouse handling and maybe an ActionListener to the circle can react similar to a JButton.
Finally you use it just like a JButton - I guess you have the code that makes your button move somewhere already.

BorderLayout orientation

I'm trying to create the top buttons of a window. I have a JFrame and a JPanel with the differents buttons when I try to add the panel with the buttons to a JPanel on the frame, it doesn't show... digging and trying to find the solution, I realize that the issue is when I set the orientation to the panel with the buttons on the BorderLayout panel. I think that it might be something dumb that I haven't realize but I haven't found any issue like this.
The issue is here when I set the orientation:
contentPanel.add(buttons,BorderLayout.PAGE_START);
if I remove the:
BorderLayout.PAGE_START
it works
This is my Frame:
package view;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.CardLayout;
import java.awt.BorderLayout;
public class MainFrame extends JFrame{
private JPanel contentPanel, layOutPanel;
private CardLayout mainCardLayout;
private BorderLayout borderLayout;
private static MainFrame instance = null;
private FrameButtonsPanel buttons;
private MainFrame(){
setSize(1000,700);
//setUndecorated(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPanel = new JPanel();
borderLayout = new BorderLayout();
contentPanel.setLayout(borderLayout);
add(contentPanel);
buttons = new FrameButtonsPanel();
buttons.setBackground(Color.red);
contentPanel.add(buttons,BorderLayout.PAGE_START);
/*layOutPanel = new JPanel();
mainCardLayout = new CardLayout();
layOutPanel.setLayout(mainCardLayout);
layOutPanel.setBackground(Color.red);
contentPanel.add(layOutPanel,BorderLayout.SOUTH);*/
}
public static MainFrame getInstance(){
if (instance == null){
instance = new MainFrame();
}
return instance;
}
public static void main(String[] args) {
MainFrame.getInstance().setVisible(true);
}
}
and this is my panel with the buttons:
package view;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.Spring;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameButtonsPanel extends JPanel{
private Spring spring;
private JButton iconify, maximize, close;
public FrameButtonsPanel(){
SpringLayout mySpring = new SpringLayout();
setLayout(mySpring);
iconify = new JButton("-");
add(iconify);
maximize = new JButton("O");
add(maximize);
close = new JButton("X");
add(close);
spring = Spring.constant(850,850,2000);
mySpring.putConstraint(SpringLayout.WEST,iconify,spring,SpringLayout.WEST,this);
mySpring.putConstraint(SpringLayout.WEST,maximize,3,SpringLayout.EAST,iconify);
mySpring.putConstraint(SpringLayout.WEST,close,3,SpringLayout.EAST,maximize);
mySpring.putConstraint(SpringLayout.EAST,this,3,SpringLayout.EAST,close);
iconifyWindow();
maximizeWindow();
closeWindow();
}
private void iconifyWindow(){
iconify.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
MainFrame.getInstance().setExtendedState(JFrame.ICONIFIED);
}
});
}
private void maximizeWindow(){
maximize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(MainFrame.getInstance().getExtendedState() == JFrame.MAXIMIZED_BOTH){
MainFrame.getInstance().setExtendedState(JFrame.NORMAL);
}else{
MainFrame.getInstance().setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
});
}
private void closeWindow(){
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
}
I have no idea why you are trying to use a SpringLayout to display buttons.
Just use a JPanel with a right aligned FlowLayout.
Read the FlowLayout API for more information on how to right align the components added to the panel.

Replacing a button with another button

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class GUI extends JFrame {
private JButton reg;
private JButton custom;
private JButton custom2;
public GUI(){
super("Heartstone Arena Alpha 0.01");
setLayout(new FlowLayout());
reg = new JButton("Click Me");
add(reg);
Icon ACn = new ImageIcon(getClass().getResource("463.png"));
Icon ACg = new ImageIcon(getClass().getResource("463 (1).png"));
custom = new JButton("Custom", ACn);
custom.setRolloverIcon(ACg);
add(custom);
HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
Icon An = new ImageIcon(getClass().getResource("Alexstrasza(303).png"));
custom2 = new JButton(" ", An);
custom2.setIcon(An);
custom2.setRolloverIcon(An);
}
}
Here is the code, what I want to do is have custom2 replace custom when it's clicked.
How would I go on about this?
I tried using custom = null and then add(custom2); but it doesn't show up
PS: Ignore the reg button
You need to add an ActionListener to your button, which should make the first button invisible and the second button visible. So it does not really get destroyed, you just dont show it.
Like this:
public class YourClassName implements ActionListener {
private JButton button1;
private JButton button2;
public YourClassName() {
// Code Snippet ----------------
button1 = new JButton("Click to replace");
button1.addActionListener(this);
// implement code for resizing and positioning here
button2 = new JButton("I am new here");
button2.setVisible(false);
// implement code for resizing and positioning here
// ...
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
button1.setVisible(false);
button2.setvisible(true);
}
}
}
Note: The code is made up out of my head, there may be a mistakes in it. Also, it is just a snippet, feel free to comment for full code

How can I make a countdown in my applet?

I'm writing a game and need a 60 second countdown. I would like it to start counting down when I click the "Start" button. I can get it to countdown manually right now, but need it to do so automatically.
This is a Java Applet, not Javascript.
Is there a way I can have this timer go in the background while I use other buttons? I'm using JLabels and JButtons. Can I have two ActionListeners running at the same time?
Use javax.swing.Timer
Run this example. You will see that you can still perform other actions while the time is running. Click the yes or no button, while the time is going
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Test extends JApplet {
private JLabel label1 = new JLabel("60");
private JLabel label2 = new JLabel("Yes");
private JButton jbt1 = new JButton("Yes");
private JButton jbt2 = new JButton("No");
private int count = 60;
private Timer timer;
public Test() {
JPanel panel1 = new JPanel(new GridLayout(1, 2));
panel1.add(label1);
panel1.add(label2);
label1.setHorizontalAlignment(JLabel.CENTER);
label2.setHorizontalAlignment(JLabel.CENTER);
JPanel panel2 = new JPanel();
panel2.add(jbt1);
panel2.add(jbt2);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
timer = new Timer(1000, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
count--;
if (count == 0) timer.stop();
label1.setText(String.valueOf(count));
}
});
timer.start();
jbt1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
label2.setText("Yes");
}
});
jbt2.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
label2.setText("No");
}
});
}
}

Java how to add application from the other class into gui class

I've got my Keypad class separate and I want to run it from the other class (gui), so I can have whatever I want in my gui class (some btn etc.) and then in the bottom my Keypad.
When I try Keypad kp = new Keypad(); I get almost what I want but they're displayed in separate windows I want them to be in the same window.
That's the Keypad class:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class KeypadWork extends JFrame implements ActionListener {
private JButton buttonR = new JButton("Reset");
private JButton button0 = new JButton("0");
private JButton buttonE = new JButton("Enter");
public KeypadWork() {
setTitle("Keypad");
setLayout(new GridLayout(4, 3, 2, 2));
for (int i = 1; i < 10; i++) {
addButton(new JButton(String.valueOf(i)));
}
addButton(buttonR);
addButton(button0);
addButton(buttonE);
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
private void addButton(JButton button) {
button.addActionListener(this);
add(button);
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
That's the solution, thank you #Alderath
If you don't want the KeyPadWork instance to be in a separate window, then you shouldn't make it a JFrame. If you want it inside another window, extend JPanel instead and add the KeyPadWork instance to some other JFrame using the normal AWT Container.add(Component) method.
Thanks a lot!

Categories