I seem to not be able to find a way to get my code to work.
I am making a program and until now everything was working, i have some buttons and they do what they should.
But now i added a button that when a user click it, it should close the current GUI and open a new one.
I also want to point out that i created a new class for this new GUI.
The other GUI class that i want to call is the GuiCrafting, in that class the GUI is also all coded, and works if i call it on the Main.
My question is what do i type here (I tried a lot of things like dispose() etc but i just get error messages) :
public void actionPerformed(ActionEvent event) {
if( str.equals("Crafting")){
//insert code to call the GuiCrafting class and open his GUI
}
Thanks in advance and if you need something more please let me know.
Multiple JFrames are frowned upon as you can read about here and here
Perhaps what you want to use is a CardLayout which manages two or more components (usually JPanel instances) that share the same display space.
After clicking the button "Goto Card 2"
TestApp.java:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestApp {
final static String CARD1 = "Card1";
final static String CARD2 = "Card2";
public TestApp() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TestApp::new);
}
private void initComponents() {
JFrame frame = new JFrame("TestApp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create the panel that contains the "cards".
JPanel cards = new JPanel(new CardLayout());
// card 1 components
JButton buttonGotoCard2 = new JButton("Goto Card 2");
buttonGotoCard2.addActionListener((ActionEvent e) -> {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, CARD2);
});
// create card 1
JPanel card1 = new JPanel();
card1.add(new JLabel("Card 1"));
card1.add(buttonGotoCard2);
// card 2 components
JButton buttonGotoCard1 = new JButton("Goto Card 1");
buttonGotoCard1.addActionListener((ActionEvent e) -> {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, CARD1);
});
// create card 2
JPanel card2 = new JPanel();
card2.add(new JLabel("Card 2"));
card2.add(buttonGotoCard1);
// add cards to cards panel
cards.add(card1, CARD1);
cards.add(card2, CARD2);
frame.getContentPane().add(cards, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
There is also a JDialog which could be what you want.
HOWEVER
You can easily do something like that (Open a JFrame from another If you must):
TestApp.java:
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class TestApp {
public TestApp() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TestApp::new);
}
private void initComponents() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel label = new JLabel("JFrame 1");
JButton button = new JButton("Open JFrame 2");
button.addActionListener((ActionEvent e) -> {
this.showNewJFrame(new WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent e) {
// here we listen for the second JFrame being closed so we can bring back the main JFrame
mainFrame.setVisible(true);
}
});
// hide the main JFrame
mainFrame.setVisible(false);
});
panel.add(label);
panel.add(button);
mainFrame.add(panel);
mainFrame.pack();
mainFrame.setVisible(true);
}
private void showNewJFrame(WindowAdapter windowAdapter) {
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // we dont wnat to exit when this JFrame is closed
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel label2 = new JLabel("JFrame 2");
panel2.add(label2);
frame2.add(panel2);
frame2.addWindowListener(windowAdapter);
frame2.pack();
frame2.setVisible(true);
}
}
This produces:
and when the "Open JFrame 2" is clicked:
and when JFrame 2 is closed it brings back the main JFrame via the WindowAdapter#windowClosing.
Related
I want to be able to detect when my button, ever so conveniently named button is clicked, I am having trouble making it be that way. here is some code
import java.awt.GridLayout;
import java.awt.event;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class Main implements ActionListener
//im having immense problems implementing the action listener.
{
public static void main(String[] args)
{
new GUI(); // calling in main.
System.out.print("test for bad wifi because my wifi hates me"); // I'm using a cloud based ide
}
JFrame frame1 = new JFrame();
JPanel panel1 = new JPanel();
JFrame frame2 = new JFrame(); //not in use yet
JPanel panel2 = new JPanel(); //""
public void GUI()
{
JButton button = new JButton("moment");
button.addActionListener(this);
panel1.setBorder(BorderFactory.createEmptyBorder( 30, 30, 30, 30 ));
panel1.setLayout(new GridLayout(0, 1));
panel1.add(button);
frame1.add(panel1, BorderLayout.CENTER); // frame is on the pannel or vice versa
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // funny close
frame1.setTitle("Final.");
frame1.pack();
frame1.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{
}
}
Any help is appreciated
Download and use an IDE.
Oracle has a helpful tutorial, Creating a GUI With JFC/Swing. Skip the Netbeans section.
The JFrame methods must be called in a specific order. This is the order I use for all my Swing applications.
Here's the complete runnable code I wound up with.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SimpleJButtonExample implements ActionListener {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new SimpleJButtonExample().createAndShowGUI(); // calling in main.
}
});
// I'm using a cloud based ide
System.out.println("test for bad wifi because my wifi hates me");
}
JFrame frame1 = new JFrame();
JPanel panel1 = new JPanel();
JFrame frame2 = new JFrame(); // not in use yet
JPanel panel2 = new JPanel(); // ""
public void createAndShowGUI() {
JButton button = new JButton("moment");
button.addActionListener(this);
panel1.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
panel1.setLayout(new GridLayout(0, 1));
panel1.add(button);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // funny close
frame1.setTitle("Final");
frame1.add(panel1, BorderLayout.CENTER); // panel is within the frame
frame1.pack();
frame1.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
}
I'm having trouble with this JApplet. At the moment I have a CardLayout JPanel which contains two BorderLayout JPanels. Whenever I run it, the components added to each 'card' (a JButton to go back to the other JPanel) don't display unless I use setVisible(true) for each LayoutManager. Furthermore, none of my ActionListeners work. I'm assuming because they only use show() and there's something else I have to do that's alluding me.
Must I use setVisible(true)? It seems from other questions that there's a way of doing this without that. Here's the code I'm having trouble with:
/*
*Java Version: 1.8.0_25
*Author: Peadar Ó Duinnín
*Student Number: R00095488
*/
package As1;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
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;
public class AUIJApplet extends JApplet implements ActionListener {
private final int WIDTH = 600;
private final int HEIGHT = 400;
private int highScore;
private int currentScore;
JPanel panelCont = new JPanel();
JPanel startPanel = new JPanel();
JPanel gamePanel = new JPanel();
JButton newGameButton = new JButton("New Game");
JButton endGameButton = new JButton("End Game");
JLabel highScoreLabel;
JLabel currentScoreLabel;
CardLayout cl = new CardLayout();
BorderLayout bl = new BorderLayout();
public AUIJApplet() {
highScore = 0;
}
#Override
public void init() {
setSize(WIDTH, HEIGHT);
panelCont.setLayout(cl);
startPanel.setLayout(bl);
gamePanel.setLayout(bl);
startPanel.add(newGameButton, BorderLayout.SOUTH);
gamePanel.add(endGameButton, BorderLayout.SOUTH);
startPanel.setBackground(Color.BLUE);
gamePanel.setBackground(Color.GREEN);
panelCont.add(startPanel, "Start Applet Screen");
panelCont.add(gamePanel, "New Game Screen");
newGameButton.addActionListener((e) -> {
newGame();
});
endGameButton.addActionListener((e) -> {
quitGame();
});
cl.show(panelCont, "Start Applet Screen");
this.add(panelCont);
}
public void newGame() {
cl.show(panelCont, "New Game Screen");
showScores(gamePanel);
}
public void quitGame() {
cl.show(panelCont, "Start Applet Screen");
if (currentScore > highScore) {
highScore = currentScore;
}
currentScore = 0;
}
public void showScores(JPanel currentPanel) {
currentPanel.add(new JLabel("High Score:") , BorderLayout.EAST);
currentPanel.add(highScoreLabel, BorderLayout.EAST);
currentPanel.add(new JLabel("Current Score:"), BorderLayout.EAST);
currentPanel.add(currentScoreLabel, BorderLayout.EAST);
}
#Override
public void actionPerformed(ActionEvent ae) {
}
}
I have made the a little similar code to perform same operation it works for me try to write the code from scratch. Here is my code.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.*;
public class Example extends JApplet {
JPanel panel1,panel2,mainPanel;
JButton start,stop;
CardLayout cl = new CardLayout();
#Override
public void init() {
panel1 = new JPanel();
panel1.setBackground(Color.red);
panel1.setLayout(new BorderLayout());
panel2 = new JPanel();
panel2.setBackground(Color.blue);
panel2.setLayout(new BorderLayout());
start = new JButton("Start");
stop = new JButton("stop");
panel1.add(start,BorderLayout.SOUTH);
panel2.add(stop,BorderLayout.SOUTH);
mainPanel = new JPanel();
mainPanel.setLayout(cl);
mainPanel.add(panel1,"First Panel");
mainPanel.add(panel2, "Second Panel");
start.addActionListener((ActionEvent e) -> {
newGame();
});
stop.addActionListener((ActionEvent e) ->{
endGame();
});
this.add(mainPanel);
}
public void newGame()
{
cl.show(mainPanel, "Second Panel");
}
public void endGame()
{
cl.show(mainPanel,"First Panel");
}
}
A while back I started a project that soon built up a shed load of code, most of the code was made up of components and their properties. All was going well until I hit an error. Off the top of head, the error was something on the line of exceeding the code limit of a constructor, roughly 65000 bytes.
This error has literally bought me and my project to halt. At the same time I have also found major problems in my understanding of SWING.
What I tried was to break my game code into logical sections, putting each section into a different class. For example, a jpanel which dealt with buying and selling would be its own .java file. Another jpanel that dealt with shipping would be in another .java file.
What I hoped to achieve was a JFrame that called each of these jpanels when the user requested it at the press of a jbutton. However, this didn't quite work as I wished, putting me in a position where I need help.
What I have done is simplified my problem by creating an example framework, hoping that somebody could point out what I need to be looking at, possibly even a solution.
I have created a JFrame which holds a panel called bg, which itself holds 2 JButtons (btn1 and btn2). In a different class file I have created a JPanel called panel1, and in another class again I have created another JPanel called panel2.
When the user opens the application they are presented with a frame and the option of two buttons, when any of these buttons are pressed I would like for either panel1 or
panel2 to open. How would this be done?
Any help would be greatly appreciated. Thanks in advance.
////////////// frame
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame implements ActionListener {
public JPanel bg;
public static JButton btn1, btn2;
public Frame(){
JFrame f = new JFrame();
f.setSize(308, 205);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
bg = new JPanel();
bg.setSize(300, 200);
bg.setLocation(0, 0);
bg.setLayout(null);
bg.setBackground(Color.black);
bg.setVisible(true);
btn1 = new JButton("open 1");
btn1.setSize(135, 30);
btn1.setLocation(10, 10);
btn1.addActionListener(this);
btn2 = new JButton("open 2");
btn2.setSize(135, 30);
btn2.setLocation(155, 10);
btn2.addActionListener(this);
bg.add(btn1);
bg.add(btn2);
f.add(bg);
}
public static void main(String[] args) {
new Frame();
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn1){
}
if (a.getSource() == btn2){
}
}
}
////////////////////// panel1
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class panel1 implements ActionListener {
public JButton btn3;
public panel1(){
JPanel a = new JPanel();
a.setSize(280, 110);
a.setLocation(155, 10);
a.setBackground(Color.red);
a.setVisible(true);
btn3 = new JButton("open bb");
btn3.setSize(100, 30);
btn3.setLocation(10, 10);
btn3.addActionListener(this);
a.add(btn3);
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn3){
}
}
}
//////////////////////////// panel2.java
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class panel2 implements ActionListener {
public JButton btn4;
public panel2(){
JPanel b = new JPanel();
b.setSize(280, 110);
b.setLocation(155, 10);
b.setBackground(Color.blue);
b.setVisible(true);
btn4 = new JButton("open");
btn4.setSize(100, 30);
btn4.setLocation(10, 10);
btn4.addActionListener(this);
b.add(btn4);
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn4){
}
}
}
You don't need to split your panels into different classes for something this simple. Try keeping everything together:
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame implements ActionListener {
public JPanel bg,panel1,panel2;
public static JButton btn1, btn2;
public Frame(){
JFrame f = new JFrame();
f.setSize(308, 205);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
bg = new JPanel();
bg.setSize(300, 200);
bg.setLocation(0, 0);
bg.setLayout(null);
bg.setBackground(Color.black);
bg.setVisible(true);
btn1 = new JButton("open 1");
btn1.setSize(135, 30);
btn1.setLocation(10, 10);
btn1.addActionListener(this);
btn2 = new JButton("open 2");
btn2.setSize(135, 30);
btn2.setLocation(155, 10);
btn2.addActionListener(this);
bg.add(btn1);
bg.add(btn2);
f.add(bg);
panel1 = new JPanel();
panel1.setSize(280, 110);
panel1.setLocation(155, 10);
panel1.setBackground(Color.red);
panel1.setVisible(false);
bg.add(panel1);
panel2 = new JPanel();
panel2.setSize(280, 110);
panel2.setLocation(155, 10);
panel2.setBackground(Color.blue);
panel2.setVisible(false);
bg.add(panel2);
}
public static void main(String[] args) {
new Frame();
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn1){
panel1.setVisible(true);panel2.setVisible(false);
}
if (a.getSource() == btn2){
panel1.setVisible(false);panel2.setVisible(true);
}
}
}
I'm currently writing an interface where a have a JFrame class and two JPanel classes. When the script is first executed, Panel A is shown. I have a JButton in Panel A which I would like, when clicked, to display Panel B instead of Panel A.
Is there any way I could do this?
Read tutorial for that.
You can use next() method of CardLayout for showing next card,
or you can use show(...); for showing specific card.
Simple example:
import java.awt.BorderLayout;
import java.awt.CardLayout;
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 Example {
public static void main(String[] args){
JFrame frame = new JFrame();
final JPanel panel = new JPanel(new CardLayout());
JLabel l1 = new JLabel("1");
JLabel l2 = new JLabel("2");
JLabel l3 = new JLabel("3");
panel.add(l1,"l1");
panel.add(l2,"l2");
panel.add(l3,"l3");
JButton btn = new JButton("next");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
CardLayout layout = (CardLayout) panel.getLayout();
layout.next(panel);
}
});
JButton btnSpec = new JButton("l3");
btnSpec.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
CardLayout layout = (CardLayout) panel.getLayout();
layout.show(panel, "l3");
}
});
frame.add(panel);
frame.add(btn,BorderLayout.SOUTH);
frame.add(btnSpec,BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I have problem with tabs in the JPanel. I know how to make new tabs in Mainframe, but I don't know how to make tabs into JPanel which is located in Mainframe.
Here are the pictures:
I have program looking like this -
http://www.bildites.lv/viewer.php?file=vklfhvfdfpwpcxllfqv.png
But I want to make it look like this -
http://www.bildites.lv/viewer.php?file=bvbrp4qfx2krn9bkx30j.png
And Here is my code of the blue JPanel:
package gui;
import java.awt.Color;
import javax.swing.JPanel;
public class CallsPanel extends JPanel {
private MainFrame frame;
Color color = new Color(99, 184, 255); // steelblue
public CallsPanel(MainFrame frame) {
this.frame = frame;
this.setLocation(0, 0);
this.setSize(300, 380);
this.setLayout(null);
this.setBackground(color);
this.initContent();
}
// -------------------------------------------------------------------------
// Declare New Things
private void initContent() {
// Add New Things
}
// -------------------------------------------------------------------------
}
Thanks a lot to people that will help!
JTabbedPane tabPane = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel label1 = new JLabel("Tab 1");
JLabel label2 = new JLabel("Tab 2");
panel1.add(label1);
panel2.add(label2);
tabPane.add("Tab 1", panel1);
tabPane.add("Tab 2", panel2);
this.add(tabPane);
Play around with the size/color/shape of the tabPane and see what works for you. But this is the basic of a tabPane.
See this simple runnable example
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MyPanel extends JPanel {
JButton button = new JButton("Button");
JTabbedPane tabPane = new JTabbedPane();
public MyPanel(){
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
tabPane.add("Panel 1", panel1);
tabPane.add("Panel 2", panel2);
tabPane.setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout());
add(tabPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setSize(300, 300);
frame.setVisible(true);
}
});
}
}