K
K
Kirill Romanenko2014-07-12 02:29:55
Java
Kirill Romanenko, 2014-07-12 02:29:55

How to make me switch to a new window in another class?

I wrote the code in the main class, where I created a JFrame form with buttons, labels, background, everything is in order! But. This is all - the main menu of the game as planned and there is an OPTIONS button there.
So, I created the Option class nearby, in the project, I also registered JFrame form2 = new Jframe; indicated the dimensions, everything is as it should be. But how can I now call this window from main? I have been tormented all day, went through a bunch of options and tips, experimented myself - the only thing I could achieve - to my complete misunderstanding, the main main window was simply cloned and there were two of them, but OPTIONS did not start ...
Here is the code from MAIN which is supposed to run OPTIONS:
public class NeverMy
{
public static void main(String[] args)
{
JFrame frame = new ImageViewer();
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("data/pic/Im5.png"));//Set the icon to the title of the window
frame.setLocationRelativeTo(null);//Place the window in the center of the desktop
}
}
@SuppressWarnings ("serial")
class ImageViewer extends JFrame
{
private JLabel countLabel;
private JButton newGame;
(other buttons are listed here, it's not needed...)
private JButton options; (this is what I need)
ImageViewer ()
{
setTitle("Autumn Story");//game window title
setSize(1024, 768);//game window size
getContentPane().setBackground(Color.getHSBColor(0.20f, 0.55f, 1));
setResizable(false);//disable window resizing by mouse
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
countLabel = new JLabel(" Autumn Story");
add(count Label);
countLabel.setFont(Library.font4);
countLabel.setLocation(6, 10);
countLabel.setSize(500, 55);
countLabel.setBackground(Color.magenta);
//countLabel.setBorder(new LineBorder(Color.black, 1)); //Border around the Header
{
final JPanel grPan = (JPanel) getContentPane(); //pass control of the graphic window to the manual constructor to arbitrarily adjust the position of the components
final JPanel p = new JPanel();//the panel under the Header
p.setBorder(new BevelBorder(BevelBorder.RAISED));
p.setSize(700, 50);
p.setLocation(1, 11);
p.setBackground(Color.getHSBColor(0.10f, 0.55f, 1));
grPan.add(p);
}
(Library is another class where I listed the icons and fonts for the game, if anything..)
newGame = new JButton("New Game", Library.i1);
newGame.setPressedIcon(Library.i1press);
{
newGame.addActionListener(null);//don't know what to add here yet...
}
saveGame = new JButton(" >> Save", Library.i2);
(other buttons are listed...)
options = new JButton(" Options", Library.i4);
options.setPressedIcon(Library.i4press);//this button here OPTIONS
final JPanel buttonsPanel = new JPanel(new GridLayout(8, 1, 3, 3));
{
(more buttons...)
buttonsPanel.add(options);
(other buttons...)
options.setFont(Library.font2);
options.setToolTipText("Game Settings");
options.setBorder(new EtchedBorder(EtchedBorder.RAISED));
options.setFocusPainted(false);
options.addActionListener(new ActionListener()
{ public void actionPerformed
(final ActionEvent e) {
and
here I was tormented all day long ;(
}
}); must create a new frame on call: public class Options { @SuppressWarnings("serial") public class OptionList extends JFrame { { JFrame frame2 = new JFrame("Dfcz");
frame2.setIconImage(Toolkit.getDefaultToolkit().getImage("data/pic/Im5.png"));//set the icon to the window title
frame2.setLocationRelativeTo(null);//place the window in the center of the desktop
frame2.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
}
}
@SuppressWarnings("serial")
class ImageViewer extends JFrame
{
public void frame2 ()
{
setTitle("Options");//window title
(various appearance options...)
setVisible(true);
JLabel frame2 = new JLabel("dhehtjdtykl");
(too...)
frame2.setBackground(Color.cyan);
}
}
In my opinion, in the course of all my attempts, I messed something up here - I don’t know, I just made a mess and everything I managed to achieve. Maybe opening a new window from another class is generally a bad idea?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kirill Romanenko, 2014-07-15
@kiralis39

tormented for a couple of days, somehow, with my mind, I still caught up with how to put everything right! ))) I never found the right answer on the Internet, but I had to write the following in the button:
options.addActionListener(new ActionListener()
{
public void actionPerformed(final ActionEvent e)
{
Options OptWin = new Options();
OptWin. OptionList.setVisible(true);
}
});
and rebuild the skeleton of the Options class in some tricky way that I myself did not fully understand .. to be honest ..
Everything worked, although, of course, I think this problem will get in my way more than once, until someone finally explains what was wrong.
The code in the Options class now looks like
public class Options
{
public JFrame OptionList;
{
{
JFrame frame2 = new JFrame("Settings");
frame2.setIconImage(Toolkit.getDefaultToolkit().getImage("data/pic/Im5.png"));//set the icon to the window title
frame2.setLocationRelativeTo(null);//place the window in the center of the desktop
frame2.pack( );
frame2.setFont(Library.font3);
frame2.setLocation(4, 15);
frame2.setSize(400, 250);
frame2.setBackground(Color.cyan);
frame2.setVisible(true);
}
}
@SuppressWarnings("serial")
class ImageViewer extends JFrame
and so on. Further. If anyone understands what I should do next time right away, please let me know.

C
Calc, 2014-07-12
@Calc

What's the easiest way to call a new window inside a window?
Didn't it help?)

V
Vitaly, 2014-07-14
@vipuhoff

not strong in Java, but maybe something like this
var Wind = new Options();
WindShow();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question