I'm developing an app for computer, and i have a JFrame with a lot of JPanel on it, and when i click on a button, i want another JPanel to popup.
Example: When i click on this button
http://i62.tinypic.com/c2fzr.jpg
I want this window to popup
http://i62.tinypic.com/2qi0in7.jpg
I already tried making a popup menu, but i don't want a menu, i want a window, and i can't seen to find out how to do it :(
It's probably easy, but i don't have enough knowledge in java
Any help? thanks guys!
Ok so,for this you will need 2 JFrames. First one is where the buttons and everything is and the second one is the one that will popup. You will have 3 classes: Main, classWhere1stJframeis, ClassWhere2ndJframeis.
This is main:
package proba;
import javax.swing.JFrame;
public class mejn {
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame1 frejm = new Frame1();
frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frejm.setVisible(true);
frejm.setSize(250, 300);
}
}
This is Frame1:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame1 extends JFrame {
JFrame Frame = new JFrame();
JButton Button1 = new JButton();
public Frame1()
{
super("The title");
Frame = new JFrame();
Button1 = new JButton();
Frame.add(Button1);
thehandler handler = new thehandler();
Button1.addActionListener(handler);
}
private class thehandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==Button1)
{
Frejm2 frejm = new Frejm2();
frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frejm.setVisible(true);
}
}
}
}
This is Frame2:
import javax.swing.JFrame;
public class Frejm2 extends JFrame {
JFrame Frame2 = new JFrame();
public Frejm2()
{
super("Title");
}
}
that is not just a panel you want to pop up that would be considered a whole other frame. I would suggest making a different JFrame class that when the button is clicked instantiates the other frame.
Related
Hey I am a beginner and I have wrote the following code in java, but I can´t click on the JButtons. The program includes three clases - Main, Frame and Actionhandler. My goal was to create a Frame with two buttons: Singleplayer and Mulitplayer. I wanted to test if they work, but I can´t click them. Can anyone help me please?
This is the Main class:
public class Main {
public static void main (String [] args) {
new Frame ();
}
}
This is the Frame class:
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame extends JFrame {
public static Object multi;
public static Object single;
Frame() {
// Frame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setLocationRelativeTo(null);
//Layout in Frame
this.setLayout(new GridLayout(2,1));
this.setVisible(true);
// Buttons in Main Menu
JButton single = new JButton("Singleplayer");
JButton multi = new JButton("Multiplayer");
// specify single button
single.setBounds(200,100,250,80);
single.setForeground(Color.GREEN);
single.setBackground(Color.LIGHT_GRAY);
single.setOpaque(true);
single.setBorder(BorderFactory.createLineBorder(Color.BLACK));
single.setFont(new Font("Comic Sans",Font.BOLD,25));
single.addActionListener(new ActionHandler());
//specify multi button
multi.setBounds(800,100,250,80);
multi.setForeground(Color.GREEN);
multi.setBackground(Color.GRAY);
multi.setOpaque(true);
multi.setFont(new Font("Comic Sans",Font.BOLD,25));
multi.setBorder(BorderFactory.createLineBorder(Color.BLACK));
multi.addActionListener(new ActionHandler());
// add Buttons to Frame
this.add(single);
this.add(multi);
}
}
This is the ActionHandler class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == Frame.multi) {
System.out.println("You have clicked on Singleplayer");
if(e.getSource() == Frame.single) {
System.out.println("You have clicked on Multiplayer");
}
}};
}
You can click on the buttons fine. They just won't do anything because of how you've wired the program:
public class Frame extends JFrame {
public static Object multi; // this is null
public static Object single; // and so is this
Frame() {
// Frame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setLocationRelativeTo(null);
//Layout in Frame
this.setLayout(new GridLayout(2,1));
this.setVisible(true);
// Buttons in Main Menu
JButton single = new JButton("Singleplayer"); // this is a new *local* variable
JButton multi = new JButton("Multiplayer"); // and so is this:
You are initializing local variables that have the same name as your static class fields, and you're leaving the same static class fields null, a situation known as "variable shadowing", and so in your listeners, you check if the source is the null static field. Which won't work.
So in your listener:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == Frame.multi) {
You're testing if a null variable is the button that was pressed, and this will not work.
One simple solution is to not re-declare the multi and single variables, to assign your JButtons to these public static fields by changing this:
JButton single = new JButton("Singleplayer");
JButton multi = new JButton("Multiplayer");
to this:
single = new JButton("Singleplayer");
multi = new JButton("Multiplayer");
This would sort-of work. You'd have do do some casting to add these JButton objects to the container since the variables are Object, not JButton. But this would be a bad idea because you'd be throwing out the OOPs baby with the bathwater, discarding encapsulation completely.
Best not to throw out OOPs rules with public static (non-constant) fields and instead work with them. Better to use constant Strings to be passed into your JButtons and then test for them using the ActionEvent's actionCommand property:
public class Frame extends JFrame {
public static String SINGLE_PLAYER = "Single Player";
public static String MULTI_PLAYER = "Multi Player";
Frame() {
// Frame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setLocationRelativeTo(null);
//Layout in Frame
this.setLayout(new GridLayout(2,1));
this.setVisible(true);
// Buttons in Main Menu
JButton single = new JButton(SINGLE_PLAYER); // this is a new *local* variable
JButton multi = new JButton(MULTI_PLAYER); // and so is this:
in the listener:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionHandler implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(Frame.MULTI_PLAYER)) {
System.out.println("You have clicked on Multi Player");
} else {
// ...
}
}};
}
Other problems with your code include:
Don't name your class Frame since this clashes with the name of class in the core Java library, java.awt.Frame. Name it something unique to avoid confusion
Avoid setting bounds, sizes and such. Let the GUI, its layout managers and component preferred sizes do the sizing by calling pack() on the top-level window (JFrame, JDialog,...) after adding components
Call .setVisible(true) on the top-level window after adding all components.
This looks like it will display as a sub-window or dialog window, and you might want to show this portion of the GUI in a modal JDialog, not in a JFrame.
So I'm making a simple program that jumps from panel to panel and am using an actionlistener Button to make the jump. What kind of method or operation do I use to jump from panel to panel?
I tried to use setVisible(true); under the action listener, but I get just a blanks screen. Tried using setContentPane(differentPanel); but that doesn't work.
ackage Com.conebind.Characters;
import Com.conebind.Tech.TechA16;
import Com.conebind.Overviews.OverviewA16;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Char_A16 extends JFrame {
private JButton combosButton16;
private JButton techButton16;
private JButton overviewButton16;
private JLabel Image16;
private JPanel panel16;
private JPanel panelOverviewA16;
public Char_A16() {
overviewButton16.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
OverviewA16 overview16 = new OverviewA16();
overview16.setVisible(true);
overview16.pack();
overview16.setContentPane(new Char_A16().panelOverviewA16);
}
});
techButton16.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Todo
}
});
}
private void createUIComponents(){
Image16 = new JLabel(new ImageIcon("Android 16.png"));
}
public static void main (String[] args){
JFrame frame = new JFrame("Android 16");
frame.setContentPane(new Char_A16().panel16);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);}
}
The setContentPane(OverviewA16) doesn't work because there's not an object that defines the panel.
Please check this demo project showing how to use CardLayout with IntelliJ IDEA GUI Designer.
The main form has a method that switches between 2 forms displayed inside it:
public void showPanel(String id) {
final CardLayout cl = (CardLayout) cardPanel.getLayout();
cl.show(cardPanel, id);
}
Both forms are added to the card layout during the main form initialization:
FormOne one = new FormOne();
one.setParentForm(this);
cardPanel.add(one.getPanel(), FORM_ONE);
FormTwo two = new FormTwo();
two.setParentForm(this);
cardPanel.add(two.getPanel(), FORM_TWO);
final CardLayout cl = (CardLayout) cardPanel.getLayout();
cl.show(cardPanel, FORM_ONE);
A reference to the main parent form is passed to these 2 forms using setParentForm() method so that FormOne and FormTwo classes can access the showPanel() method of the MainForm.
In a more basic case you may have a button or some other control that switches the forms
located directly on the MainForm, then you may not need passing the main form reference to the subforms, but it can be still useful depending on your app logic.
I am new to java and I want to make a simple program with 3 radioButtons, with only one button selected at a time.
I made the same program with the actionListener in the same class and it worked, but whe I moved the actionListener it in a different class I got stuck.
Here is the class where I created the window:
import javax.swing.JRadioButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;;
public class window extends JFrame{
public JRadioButton radio1= new JRadioButton("Salam1");
public JRadioButton radio2= new JRadioButton("Salam2");
public JRadioButton radio3= new JRadioButton("Salam3");
public window(){
super("Title");
setLayout(new FlowLayout());
add(radio1);
add(radio2);
add(radio3);
action acc = new action();
radio1.addActionListener(acc);
radio2.addActionListener(acc);
radio3.addActionListener(acc);
}
}
And this is my ActionListener Class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class action implements ActionListener{
window sarma = new window();
public void actionPerformed(ActionEvent event){
if(sarma.radio1.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio2.isSelected()){
sarma.radio1.setSelected(false);
sarma.radio3.setSelected(false);
}
if(sarma.radio3.isSelected()){
sarma.radio2.setSelected(false);
sarma.radio1.setSelected(false);
}
}
}
The main class
import javax.swing.JFrame;
public class first{
public static void main(String args[]) {
window salam = new window();
salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
salam.setSize(500,150);
salam.setResizable(false);
salam.setVisible(true);
}
}
After I created the window object (named sarma) in the action class, the window won't open when I try to run the program.
So, how could I make this program work?
The current problem with the code is down to the fact that the action listener has no reference to the original window, and instead creates an entirely separate instance that is never set visible. (As detailed by D.G).
But the action listener is not needed. The effect can be achieved using a ButtonGroup, like this:
import javax.swing.*;
import java.awt.*;
public class RadioButtonWindow extends JFrame{
public JRadioButton radio1= new JRadioButton("Salam1");
public JRadioButton radio2= new JRadioButton("Salam2");
public JRadioButton radio3= new JRadioButton("Salam3");
public RadioButtonWindow(){
super("Title");
setLayout(new FlowLayout());
add(radio1);
add(radio2);
add(radio3);
// Only one button in this group can be selected at a time!
ButtonGroup bg = new ButtonGroup();
bg.add(radio1);
bg.add(radio2);
bg.add(radio3);
}
public static void main(String args[]) {
RadioButtonWindow salam = new RadioButtonWindow ();
salam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Wrong way to size a GUI!
//salam.setSize(500,150);
salam.setResizable(false);
// Correct way to size a GUI
salam.pack();
salam.setVisible(true);
}
}
I have two java forms: NewJFrame i NewJFrame1. I have button on NewJFrame, so when I click on that button to open NewJFrame1 and close NewJFrame. It can open NewJFrame1, but it cannot close NewJFrame.
This:
NewJFrame frame = new NewJframe();
frame.setVisible(false);
doesn't work. Also, frame.dispose(); doesnt work. CAn someone help me to soleve problem, how can I close NewJFrame by clicking on button in it (NewJFrame).
In your code
NewJFrame frame = new NewJFrame();
creates a new (second) instance of NewJFrame. If you want to close the original one, you need a reference to this instance. Depending on your code, the reference could be this, so
this.dispose();
could work.
Check if is frame visible before u trying to close it...Maybe u are trying to close wrong instance of frame... if u have NewJFrame frame = new NewJframe()
then this same frame need to be closed .
frame.setVisible(false);
or
frame.dispose();
Just do dispose on original instance do not do JFrame frame = new JFrame()twice.
Try this one.. Hope, it will work.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
I'm not really sure to understand why you are doing this, but I provided you a working sample :
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Tester implements ActionListener {
private static final String SHOW = "show";
private final JButton displayer = new JButton(SHOW);
private final JButton hider = new JButton("hide");
private final JFrame f;
private final JFrame f1;
Tester(){
displayer.addActionListener(this);
hider.addActionListener(this);
f = new JFrame();
f.setLayout(new FlowLayout());
f.setSize(500, 500);
f.add(displayer);
f.add(hider);
f.setVisible(true);
f1 = new JFrame();
f1.setSize(500, 500);
f1.setLocationRelativeTo(null);
f1.add(new JLabel("empty frame"));
}
public static void main(String[] args) {
new Tester();
}
#Override
public void actionPerformed(ActionEvent arg0) {
f1.setVisible(arg0.getActionCommand().equals(SHOW));
}
}
I want to run this code that will create a window with a simple button on it. The program will run in Netbeans on a Mac but the problem is that it does not work. Here is the code below.
import javax.swing.JFrame;
public class Test {
public static JButton button(){
JButton button = new JButton("random button");
}
public static void main(String[] args) {
button();
new JFrame();
}
}
Please help me figure this out soon please. Thank you.
You're not adding the button to anything or displaying the JFrame. Your method returns a JButton object, but you're not doing anything with this object.
Create a JPanel
Add the JButton to the JPanel
Add the JPanel to the JFrame
Display the JFrame by calling setVisible(true)
Most important: Making up code and hoping it will magically work is not a successful heuristic for learning to program. Instead read the Swing tutorials which you can find here.
For example
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JButton button = new JButton("Button");
JPanel panel = new JPanel();
panel.add(button);
JFrame frame = new JFrame("foo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}