There are a number of similar posts, notably:
Java Swing JLayeredPane not showing up
and
Top layer in JLayeredPane not displaying
But none of them seem to solve my specific problem.
I have the following code:
public class PlayScreen{
private JLayeredPane playScreen;
private Player player;
private JPanel towerButtons;
private JPanel detailPanel;
private JLabel map;
public PlayScreen(Player player){
this.player = player;
playScreen = new JLayeredPane();
playScreen.setLayout(new BorderLayout());
setTowerButtons();
playScreen.add(towerButtons, BorderLayout.EAST);
playScreen.setLayer(towerButtons, 0);
setDetailLabels();
playScreen.add(detailPanel, BorderLayout.WEST);
playScreen.setLayer(detailPanel, 0);
setMap(player.getNextMap());
playScreen.add(map, BorderLayout.CENTER);
playScreen.setLayer(map, 0);
JButton playButton = new JButton("play");
playScreen.add(playButton, BorderLayout.NORTH);
playScreen.setLayer(playButton, 0);
playButton.addActionListener(new playListener());
playScreen.setVisible(true);
}
where setTowerButtons(), setDetailLabels() and setMap() methods called in the constructor basically initialize the towerButtons, detailPanel and map fields.
When the "play" button is pressed, the following code is called:
public class playListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Image image = null;
try{
image = ImageIO.read(new File("mob1.bmp"));
}
catch (Exception e1){
System.out.println("image creation failed");
}
ImageIcon img = new ImageIcon(image);
JLabel mob1Button = new JLabel(img);
System.out.println("hi");
playScreen.add(mob1Button, BorderLayout.CENTER);
playScreen.setLayer(mob1Button, 1);
mob1Button.setVisible(true);
}
}
I know that this is definitely called, because "hi" appears on the output stream.
(The playScreen is a JLayeredPane that is constructed from this:)
public class View {
private JFrame frame;
private Container contentPane;
private Player player;
private LoadScreen loadScreen;
private PlayScreen playScreen;
public View(Player player){
frame = new JFrame("Display");
contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
this.player = player;
frame.setLocationRelativeTo(null); //puts frame in middle of screen
frame.pack();
frame.setVisible(true);
}
public void makeLoadScreen(){
loadScreen = new LoadScreen();
contentPane.add(loadScreen.getLoadScreen(), BorderLayout.CENTER);
frame.pack();
}
public void makePlayScreen(Player player){
contentPane.remove(loadScreen.getLoadScreen());;
playScreen = new PlayScreen(player);
contentPane.add(playScreen.getPlayScreen());
frame.pack();
}
public class LoadScreen{
private JPanel loadScreen;
public LoadScreen(){
loadScreen = new JPanel();
JButton play = new JButton("Play");
JButton load = new JButton("Load");
loadScreen.setLayout(new GridLayout(2,1,0,0));
loadScreen.add(play);
loadScreen.add(load);
play.addActionListener(new OpenActionListener());
load.addActionListener(new LoadActionListener());
}
public JPanel getLoadScreen(){
return loadScreen;
}
public class OpenActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
makePlayScreen(player);//might not be receiving correct player variable?
}
}
public class LoadActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
}
}
public GameManager(){
player = new Player();
player.setNextMap("map.bmp");
//Tower tower = new Tower1();
//player.addUnlockedTower(tower);
view = new View(player);
view.makeLoadScreen();
}
A GameManager Class is created. This creates the loadScreen, which has two buttons, "play" and "load." Clicking on "play" creates the playScreen, which is governed by a separate class.
Sorry for all the code, but I have no idea anymore where it is going wrong, I thought it was to do with not setting the sizes of the playScreen, towerButtons etc, as in the similar post, but that didn't seem to work when I added preferred /min / max sizes.
Currently an image of the map comes up, but the image of the "mob1" does not appear at all, and I would expect it to overlay the map image.
Related
I am making a Simon Says style game, there are four colored squares and the computer does a sequence, then you copy that, etc. and am at a point where I want to add some more advanced features. The current feature I am looking at is looking to change the color panels actual colors at the users will and being able to change them individually from one another.
How can I get the 'color panel(s)' to change to a new color via JColorChooser while keeping everything else set up?
At the moment I have it split up into a few different classes and am having issues getting them all to communicate and just work properly.
Main class(Only a snippet):
public class Simonish implements ActionListener, MouseListener {
private ColorPanel colorPanel[] = new ColorPanel[4];
private ScorePanel scorePanel = new ScorePanel();
private Menu menuBar = new Menu();
private JPanel gameBoard = new JPanel();
private Random rand = new Random();
private ArrayList<ColorPanel> compSeq = new ArrayList<ColorPanel>();
private Iterator<ColorPanel> iter;
private JFrame frame = new JFrame();
private boolean playerTurn = false;
private int speed = 500;
public Simonish(Color[] colors){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = (JPanel)frame.getContentPane();
pane.setLayout(new BorderLayout());
gameBoard.setLayout(new GridLayout(2,2));
gameBoard.setPreferredSize(new Dimension(400,400));
for (int i=0;i<colorPanel.length;i++){
colorPanel[i] = new ColorPanel(colors[i]);
colorPanel[i].addMouseListener(this);
gameBoard.add(colorPanel[i]);
}
scorePanel.addStartListener(this);
frame.setJMenuBar(menuBar);
pane.add(scorePanel, BorderLayout.NORTH);
pane.add(gameBoard, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
My Menu code (builds the menubar and implements the actions):
public class Menu extends JMenuBar {
private JMenuBar menuBar = new JMenuBar();
private JMenu settings = new JMenu("Settings");
private JMenu stats = new JMenu("Stats");
private JMenu help = new JMenu("Help");
private JMenuItem chooseColor = new JMenuItem(new ChooseColorAction("Choose Color"));
private JMenuItem colorMode = new JMenuItem(new ColorModeAction("Color Mode"));
private JMenuItem hScore = new JMenuItem("High Scores");
private JMenuItem history = new JMenuItem("History");
private JMenuItem about = new JMenuItem("About");
private JMenuItem rules = new JMenuItem("Rules");
public Menu(){
this.add(settings);
this.add(stats);
this.add(help);
settings.add(chooseColor);
settings.add(colorMode);
stats.add(hScore);
stats.add(history);
help.add(about);
help.add(rules);
}
}
Action class to house the color changing code:
public class ColorModeAction extends AbstractAction {
public ColorModeAction(String name){
super(name);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Color[] colors = {Color.CYAN, Color.BLACK, Color.WHITE, Color.GREEN};
//new Simonish(colors);
//JOptionPane.showMessageDialog(null, "Color Mode");
}
}
Use interfaces to communicate your classes. For example; ColorModeAction needs to change colors so it should take an interface as parameter which able to change colors:
public interface ColorChanger {
public void changeColor(int index, Color newColor);
}
Make Simonish implement that interface:
public class Simonish implements ActionListener, MouseListener, ColorChanger {
public void changeColor(int index, Color new Color) {
//Change the given panel's color
}
}
Pass Simonish as parameter to the menu, and move new ColorModeAction("Color Mode") to the constructor. Then pass ColorChanger to the ColorModeAction as a parameter.
public class Menu extends JMenuBar {
...
private JMenuItem colorMode;
...
public class Menu(ColorChanger colorChanger) {
colorMode = new JMenuItem(new ColorModeAction(colorChanger, "Color Mode"));
}
}
And the new ColorModeAction:
public class ColorModeAction extends AbstractAction {
private ColorChanger colorChanger;
public ColorModeAction(ColorChanger colorChanger, String name) {
super(name);
this.colorChanger = colorChanger;
}
#Override
public void actionPerformed(ActionEvent e) {
Color[] colors = { Color.CYAN, Color.BLACK, Color.WHITE, Color.GREEN };
colorChanger.changeColor(index, Color)
}
}
It is not fully working code but I think you got the idea.
I have been struggling with updating Jtextfield and Jbutton data in cardlayout from couple of days. I have created small demo to explain my problem.. When I click on "start" button it should show another panel and thats is working but when I return back to main page I want my Jtextfield and jbutton to be updated from "world" to "hello" but that is not working. Any help and suggestions would be appreciated.(Sorry about the indentation of code, I do not know why copy-paste did not work properly).
public class CardlayoutDemo {
public static String data = "world";
private static final String INTRO = "intro";
private static final String GAME = "game";
private CardLayout cardlayout = new CardLayout();
private JPanel mainPanel = new JPanel(cardlayout);
private IntroPanel introPanel = new IntroPanel();
private GamePanel gamePanel = new GamePanel();
public CardlayoutDemo() {
mainPanel.add(introPanel.getMainComponent(), INTRO);
mainPanel.add(gamePanel.getMainComponent(), GAME);
introPanel.addBazBtnActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardlayoutDemo.data = "hello";
mainPanel.repaint();
mainPanel.revalidate();
CardLayout cl = (CardLayout)mainPanel.getLayout();
cl.show(mainPanel, GAME);
}
});
gamePanel.addBackBtnActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)mainPanel.getLayout();
cl.show(mainPanel, INTRO);
}
});
}
private JComponent getMainComponent() {
return mainPanel;
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CardlayoutDemo().getMainComponent());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class IntroPanel {
private JPanel mainPanel = new JPanel();
public JButton start;
private JButton exit;
private JTextField lblData;
public IntroPanel() {
mainPanel.setLayout(new BorderLayout());
JPanel content = new JPanel();
start = new JButton("Start");
exit = new JButton(CardlayoutDemo.data);
lblData = new JTextField(CardlayoutDemo.data);
content.add(lblData);
content.add(start);
content.add(exit);
mainPanel.add(content, BorderLayout.CENTER);
exit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(mainPanel);
win.dispose();
}
});
}
public void addBazBtnActionListener(ActionListener listener) {
start.addActionListener(listener);
}
public JComponent getMainComponent() {
return mainPanel;
}
}
class GamePanel {
private static final Dimension MAIN_SIZE = new Dimension(400, 200);
private JPanel mainPanel = new JPanel();
private JButton back;
public GamePanel() {
back = new JButton("return to main menu");
mainPanel.add(back);
mainPanel.setPreferredSize(MAIN_SIZE);
}
public JComponent getMainComponent() {
return mainPanel;
}
public void addBackBtnActionListener(ActionListener listener) {
back.addActionListener(listener);
}
}
when this line gets executed inside your listener:
CardlayoutDemo.data = "hello";
You´re creating a new java.lang.String object and setting the field data to reference the new String you have just created. This has no effect over the internal state of the JTextField or the String object which was previously referenced by the variable.
To change the text of the JTextField you should call the setText(String) method of JTextField .
No, don't override the show method. Instead give your classes methods that allow other classes the ability to change their state. For instance, you could add this method to the IntroPanel class:
class IntroPanel {
// .....
// !! added!
public void lblDataSetText(String text) {
lblData.setText(text);
}
}
and then call it when you want to change the state of its lblData field:
introPanel.addBazBtnActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// !! CardlayoutDemo.data = "hello";
mainPanel.repaint();
mainPanel.revalidate();
CardLayout cl = (CardLayout) mainPanel.getLayout();
cl.show(mainPanel, GAME);
introPanel.lblDataSetText("Hello!"); // !!
}
});
Note that this is a quick and dirty solution. If you want a more robust solution that scales better in larger programs, then re-structure your program along an M-V-C design pattern, and have your view change the displayed text in response to a change in the state of a String in the model. This would require more work, and in a small "toy" program wouldn't be worth the effort, but in a large complex program is well worth the effort since it would help reduce coupling and thereby reduce complexity and risk of bugs.
I am trying to make a program that reads a file chosen by the user, and after reading the file - the suffix "txt" is changed to "gif" and the file is saved as a picture (which is in the same catalogue as the file). The thing is, this picture variable gets its value in the "actionPerformed-method" and after that I want to add it to a frame in another class- but it doesn't show. Here's the code in my OptionsPane-class:
public class OptionsPane extends JComponent implements ActionListener{
private JButton buttonOne = new JButton("Alt.1");
private JButton buttonTwo = new JButton("Alt.2");
private JButton buttonThree = new JButton("Alt.3");
private int option;
private JButton buttonChoose = new JButton("Choose file");
private FileHandler filehandler;
private String picture;
private JLabel picLabel;
public OptionsPane(){
JLabel label = new JLabel("Choose optimization method", SwingConstants.CENTER);
JPanel subPanel = new JPanel();
label.setForeground(Color.CYAN);
label.setFont(new Font("Tahoma", Font.BOLD, 15));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(label);
buttonOne.addActionListener(this);
buttonTwo.addActionListener(this);
buttonThree.addActionListener(this);
buttonChoose.addActionListener(this);
subPanel.setBackground(Color.DARK_GRAY);
subPanel.add(buttonOne);
subPanel.add(buttonTwo);
subPanel.add(buttonThree);
subPanel.add(buttonChoose);
this.add(subPanel);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonOne){
option = 1;
System.out.println("You clicked button 1!");
}else if(e.getSource() == buttonTwo){
option = 2;
System.out.println("You clicked button 2!");
}else if(e.getSource() == buttonThree){
option = 3;
System.out.println("You clicked button 3!");
}else if(e.getSource() == buttonChoose){
System.out.println("hello");
option = 4;
filehandler = new FileHandler();
filehandler.read();
picture = filehandler.getFilePath().replaceFirst("txt", "gif");
picLabel = new JLabel(new ImageIcon(picture));
this.add(picLabel);
}
}
}
The frame is in the "MainFrame"-class, which looks like this at the moment:
public class MainFrame extends JFrame{
private JFrame frame = new JFrame();
private String picture;
private JLabel picLabel;
public MainFrame(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1300, 800));
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
OptionsPane optionspane = new OptionsPane();
frame.add(optionspane);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
}
}
Why isn't the picture visible in the mainframe?
EDIT
It works now!
https://stackoverflow.com/a/22380387/3271504
Thank you for your help #arooaroo . I tried to write down some of what you wrote, but it still didn't work when I wanted to add an image based on what file the user had chosen (for example if the user chose file text1.txt i wanted the corresponding picture "text1.gif" to show up). With your help, the picture showed up when I typed a specific pathway with "/"-slashes, but when I chose a file and tried to load the picture from the file pathway, it didn't show and that is because it had backslashes in the pathways. This is how it should be (such an irritating problem):
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonOne){
option = 1;
System.out.println("You clicked button 1!");
}else if(e.getSource() == buttonTwo){
option = 2;
System.out.println("You clicked button 2!");
}else if(e.getSource() == buttonThree){
option = 3;
System.out.println("You clicked button 3!");
}else if(e.getSource() == buttonChoose){
filehandler = new FileHandler();
filehandler.read();
filepath = filehandler.getFilePath();
picture = filepath.replaceFirst("txt", "gif");
picture = picture.replaceAll("\\\\", "/");
ImageIcon icon = new ImageIcon(picture);
mainFrame.setPicture(icon);
}
Thank you for your help!
Once you separate your GUI code in to separate classes - which is a Good Thing - you will find the eternal challenge for GUI programming is allowing for clean communication between them where there are inter-dependencies.
In this instance perhaps the simplest approach is to pass in a reference of MainFrame into OptionsPane.
Let's assume you create an additional method in MainFrame for setting the picture:
public class MainFrame extends JFrame{
// all instance vars as before
public MainFrame() {
// same as before except for this line...
OptionsPane optionspane = new OptionsPane(this);
}
public void setPicture(JLabel pictureLabel) {
// add code here for adding the picture...
// That's an exercise for yourself, or another question ;)
}
}
Then in your OptionsPane class:
....
private MainFrame mainFrame; // add a new instance var
public OptionsPane(MainFrame mainFrame) {
this.mainFrame = mainFrame;
// ... rest of the code same as before
}
#Override
public void actionPerformed(ActionEvent e) {
//...
picture = filehandler.getFilePath().replaceFirst("txt", "gif");
picLabel = new JLabel(new ImageIcon(picture));
mainFrame.setPicture(picLabel); // <-- This is where you communicate with the mainFrame instance
//...
}
EDIT
Although my original answer provided a valid and correct solution, it's clear that the OP requires a fully working example, including the code to load display the resulting image. Here's a sample program.
public class OptionsPane extends JComponent implements ActionListener {
private JButton buttonOne = new JButton("Alt.1");
private JButton buttonTwo = new JButton("Alt.2");
private JButton buttonThree = new JButton("Alt.3");
private int option;
private JButton buttonChoose = new JButton("Choose file");
private String picture;
private JLabel picLabel;
private MainFrame mainFrame;
public OptionsPane(MainFrame mainFrame) {
this.mainFrame = mainFrame;
JLabel label = new JLabel("Choose optimization method", SwingConstants.CENTER);
JPanel subPanel = new JPanel();
label.setForeground(Color.CYAN);
label.setFont(new Font("Tahoma", Font.BOLD, 15));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(label);
buttonOne.addActionListener(this);
buttonTwo.addActionListener(this);
buttonThree.addActionListener(this);
buttonChoose.addActionListener(this);
subPanel.setBackground(Color.DARK_GRAY);
subPanel.add(buttonOne);
subPanel.add(buttonTwo);
subPanel.add(buttonThree);
subPanel.add(buttonChoose);
this.add(subPanel);
}
#Override
public void actionPerformed(ActionEvent e) {
// For sake of simplicity I'm ignoring the original button logic here
// and focussing on just getting an icon loaded in the parent frame...
ImageIcon icon = new ImageIcon("/path/to/test/image.png");
// Just pass the icon itself rather than a new label.
mainFrame.setPicture(icon);
}
}
public class MainFrame {
// No need to extend JFrame if you're using a JFrame instance variable
private JFrame frame = new JFrame();
private JLabel picLabel;
private JPanel mainPanel;
public MainFrame() {
mainPanel = new JPanel(new BorderLayout());
mainPanel.setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1300, 800));
OptionsPane optionspane = new OptionsPane(this);
mainPanel.add(optionspane, BorderLayout.NORTH);
picLabel = new JLabel();
picLabel.setHorizontalAlignment(JLabel.CENTER);
mainPanel.add(picLabel, BorderLayout.CENTER);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
}
public void setPicture(ImageIcon icon) {
picLabel.setIcon(icon);
}
public static void main(String[] args) {
new MainFrame();
}
}
Note I've done a couple of things differently. Personally I always create a JPanel and set that up as the primary layer rather and add that directly to the frame rather than messing with the rootPane. And I used the BorderLayout in this example as it's much simpler.
The other thing is to add the JLabel which is to hold the picture to the GUI in the initial set up. Then you'll see I'm only changing its icon in the setPicture() method rather than adding a new JLabel on each instance.
I've been trying to figure out CardLayout with action listeners on button
(so like - starts on a load-up page- and on a button click switches to a different "card"
my code won't even run right now i'm not entirely sure why - most implementations I can find use ItemListeners and Combo Boxes
The basic process I've done is create a master JPanel, but my cards JPanel onto the master JPanel, but my different cards into the cards JPanel, then add the master JPanel to the frame to display...
Also, for one of my cards I only need to display a picture - I previously implemented this by just creating a new pop-up window but It would be nice to be able to switch the frame to show it... I don't know why I can't figure this out
Here's my code:
import java.awt.*;
/**
* Game
* Main class that specifies the frame and widgets of the GUI
*/
public class Game implements Runnable {
public void run(){
final String ON_OPEN = "Welcome!"; //Opening frame
final String GAME = "Play!"; // Game Frame
final String STATS = "Stats"; // Post-Game Stat Frame
final String HELP = "Help"; //tutorial frame
JPanel cards = new JPanel();
JPanel master; // a panel for the card layout
final JFrame frame = new JFrame(ON_OPEN);
frame.setLocation(500,200);
//Create the master layout for the program
master = (JPanel) frame.getContentPane();
master.setLayout(new BorderLayout()); // creating master layout
//Create panel for all the cards in CardLayout
final CardLayout cLay = new CardLayout();
cards.setLayout(cLay);
// all the cards
final JPanel help = new JPanel();
final JPanel stats = new JPanel();
final JPanel game = new JPanel (new BorderLayout());
final JPanel open = new JPanel (new FlowLayout());
// setting up ON_OPEN layout - uses JPanel open
final ImageIcon img = new ImageIcon("Instructions.png", "My Instructions..."); // the image I want shown under HELP card
final JButton info = new JButton("Help");
info.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// cLay.show(help, HELP); // WHAT I NORMALLY SHOULD BE DOING, RATHER JUST MAKE A NEW FRAME FOR THIS THOUGH
// frame.pack();
final JFrame infoFrame = new JFrame("Tutorial");
infoFrame.setLocation(500,50);
JLabel tutorialImg = new JLabel(img);
// int w = img.getIconWidth();
// int h = img.getIconHeight();
//infoFrame.setSize(w, h);
infoFrame.pack();
infoFrame.add(tutorialImg);
infoFrame.setVisible(true);
}
});
open.add(info); // the open-tutorial button
//Add them to the cards JPanel
cards.add(open, ON_OPEN);
cards.add(help, HELP);
cards.add(stats, STATS);
cards.add(game, GAME);
//Add the cards panel to the Master layout panel
master.add(cards);
// This code is all commented out because I'm not sure what I'm doing here...
// frame.add(cards);
// cLay.show(cards, ON_OPEN);
// frame.add(open, BorderLayout.CENTER);
// Main playing area - I want this to be shown in the GAME card...
GridLayout tileGrid = new GridLayout(4,4);
final JPanel grid = new JPanel(tileGrid);
// game.add(grid, BorderLayout.CENTER);
// grid.setLayout(tileGrid);
// frame.add(grid, BorderLayout.CENTER);
// Input - holds typing box
// final JPanel status_panel = new JPanel();
// frame.add(cards, BorderLayout.CENTER);
// frame.add(open, BorderLayout.CENTER);
final JTextField typingArea = new JTextField();
typingArea.setFocusTraversalKeysEnabled(false);
typingArea.setEditable(true);
typingArea.setFocusable(true);
typingArea.requestFocus();
frame.add(typingArea, BorderLayout.SOUTH);
typingArea.addKeyListener(new KeyAdapter() {
public void keyPressed (KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) { // enter key is pressed
String userWord = typingArea.getText().toLowerCase();
typingArea.setText("");
}
}
});
final JLabel status = new JLabel("Running...");
// status_panel.add(status);
// Reset button
final JPanel control_panel = new JPanel();
frame.add(control_panel, BorderLayout.NORTH);
]
// Put the frame on the screen
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Game());
}
}
Problems:
Your code doesn't compile for us since we don't have the JLetterField class.
You're trying to add the JFrame's contentPane back on itself, and causes an exception and doesn't make sense.
Edit 1:
Your latest code shows you putting everything into one very large run() method but in doing so, you loose much and gain nothing.
I suggest getting rid of the Runnable interface, there's no need for it, and creating a true OOP compliant class, one with private fields and public and private methods.
Your actionPerformed method shows no attempt at changing the CardLayout's displayed card.
I suggest that you get rid of the code to show a new window and try to place card swapping code there.
Make your CardLayout and the card-displaying JPanel fields of the class so that other methods can access them and call their methods.
Edit 2:
For example the following code shows the swapping of cards using 3 JButtons. One to get the previous card, one to get the next card, and one to show how to get a specific card (here the 2nd):
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class CardLayoutEg extends JPanel {
private static final String[] CARD_LABELS = { "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten" };
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private CardLayout cardlayout = new CardLayout();
private JPanel cardHolder = new JPanel(cardlayout);
private Action[] actions = { new ShowPreviousAction(), new ShowNextAction(),
new ShowTwoCardAction() };
public CardLayoutEg() {
for (String cardLabelText : CARD_LABELS) {
JLabel cardLabel = new JLabel(cardLabelText, SwingConstants.CENTER);
cardHolder.add(cardLabel, cardLabelText);
}
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
for (Action action : actions) {
btnPanel.add(new JButton(action));
}
setLayout(new BorderLayout());
add(cardHolder, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private class ShowPreviousAction extends AbstractAction {
public ShowPreviousAction() {
super("Previous");
}
#Override
public void actionPerformed(ActionEvent e) {
cardlayout.previous(cardHolder);
}
}
private class ShowNextAction extends AbstractAction {
public ShowNextAction() {
super("Next");
}
#Override
public void actionPerformed(ActionEvent e) {
cardlayout.next(cardHolder);
}
}
private class ShowTwoCardAction extends AbstractAction {
public ShowTwoCardAction() {
super("Show Two");
}
#Override
public void actionPerformed(ActionEvent e) {
cardlayout.show(cardHolder, CARD_LABELS[1]);
}
}
private static void createAndShowGui() {
CardLayoutEg mainPanel = new CardLayoutEg();
JFrame frame = new JFrame("CardLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
I'm trying to create an animation (spinning text) by repeatedly changing the icon on a JLabel. The issue is the images are not of the same size, and when they are bigger than the size of the first image, they are clipped.
One way around this is to setPreferredSize for the JLabel so that all images fit - but I imagine there must be a way dinamically resize the JPanel containing the JLabel?
In the code bellow I've also tried removing the JLabel alltogether, creating a new one and then adding the new one, but to the same effect.
public class AnimationPanelv2 extends JPanel{
private JButton start = new JButton("Start Animation");
private JLabel img = new JLabel();
private JTextField animSpeed = new JTextField(10);
private JTextField filePrefix = new JTextField(10);
private JTextField noOfImg = new JTextField(10);
private JTextField audioFile = new JTextField(10);
private Timer timer;
private AudioClip clip;
private ArrayList<ImageIcon> icon = new ArrayList<>();
private int step=0;
public AnimationPanelv2() {
//button is for starting the animation
start.addActionListener(new Animatie());
this.setLayout(new BorderLayout());
add(start, BorderLayout.NORTH);
//showing the label with the first frame
Class metaObj = this.getClass();
URL url = metaObj.getResource("/image/L1.gif");
img.setIcon(new ImageIcon(url));
// img.setPreferredSize(new Dimension(500,550));
add(img, BorderLayout.CENTER);
//control panel
JPanel controls = new JPanel(new GridLayout(4,2));
controls.setBorder(new TitledBorder("Enter information for animation"));
controls.add(new JLabel("Animation speed in ms"));
controls.add(animSpeed);
controls.add(new JLabel("Image file prefix"));
controls.add(filePrefix);
controls.add(new JLabel("Number of images"));
controls.add(noOfImg);
controls.add(new JLabel("Audio file"));
controls.add(audioFile);
//
add(controls, BorderLayout.SOUTH);
}
private class TimerAnimation implements ActionListener {
public void actionPerformed(ActionEvent e) {
remove(img);
img = new JLabel(icon.get(step++));
img.setVisible(true);
add(img, BorderLayout.CENTER);
// img.revalidate();
// img.repaint();
validate();
repaint();
updateUI();
if (step==Integer.parseInt(noOfImg.getText())) step=0;
}
}
private class Animatie implements ActionListener {
public void actionPerformed(ActionEvent e) {
//getting data from the text fields
int ms = Integer.parseInt(animSpeed.getText());
String s = filePrefix.getText();
int nr = Integer.parseInt(noOfImg.getText());
String audioFilePath = audioFile.getText();
// clip
Class metaObj = this.getClass();
URL url = metaObj.getResource("/audio/"+audioFilePath);
clip = Applet.newAudioClip(url);
//image loading
for (int i=1; i<=nr; i++){
url = metaObj.getResource("/image/"+s+i+".gif");
System.out.println("/image/"+s+i+".gif");
icon.add(new ImageIcon(url));
}
//timer
timer = new Timer(ms, new TimerAnimation());
timer.start();
clip.loop();
}
}
public static void main(String[] args) {
JFrame jf = new JFrame("This class test");
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new AnimationPanelv2());
jf.pack();
jf.setVisible(true);
}
}
This whole panel will be used in an applet.
This is a screenshot: http://screencast.com/t/UmqQFZHJVy
The images that are supposed to be the frames, should be located in an /images/ sub-directory and if the user enters n for the number of frames and F for the image
prefix, then the files are F1, F2, and so on, to Fn (GIFs). The sound file should be in an /audio/ sub-directory, and the entire file name is given by the user.
You can try to create list of JLabels for each image, add them to a panel with CardLayout and swap cards.
Okay well a JLabel should size automatically to its given content, so to solve JPanel issue, simply override getPreferredSize() of JPanel containing the JLabel and return Dimensions according to the JLabel size.
public class MyPanel extends JPanel {
JLabel label=...;
#Override
public Dimension getPreferredSize() {
return new Dimension(label.getWidth(),label.getHeight());
}
}
also dont forget when you change Icon of JLabel call revalidate() and repaint() on JPanel instance in order for size changes to refelect.