0

I'm currently learning the Java programming language and as part of my Java course I'm writing an application . My classes don't extend JFrame ,instead they extend EasyApp (http://ibcomp.fis.edu/Java/EasyApp.html , please scroll down to see the code ) . The problem , I 'm having now is that when I create a new button and then click it , a new window opens up :

(Here is part of my code : )

JButton stud = addButton1(io0, 35, 1223,901, 120, this);
    ImageIcon io1 = new ImageIcon(getClass().getResource("2.png"));

....

public MatchingGame() {
        setTitle("Matching game");
      stud.setBorder(BorderFactory.createEmptyBorder());
      stud.setContentAreaFilled(false);

..... public void actions(Object source,String command) {

  if (source == stud )
  { new Register();

..... When clicking the button stud a new window appears which I don't want ;I know this is because I'm creating an instance of the class Register but I have no idea how to resolve this issue . Thanks in advance for your answers !

luka0309
  • 1
  • 1
  • So, what do you want to happen instead? – kiheru May 17 '15 at 16:03
  • To somehow stay in the same window – luka0309 May 17 '15 at 16:05
  • why do you need new `Register` object? – m.cekiera May 17 '15 at 16:08
  • As in, you want to refer to the current `Register`, instead of creating a new one? I have no idea where `actions(Object source,String command)` is, so it's a bit guess work how to refer to the `Register` instance. If it's a method in `Register`, then you can refer to the current instance as `this`. If it's in an (non static) internal class of `Register`, then you can use `Register.this`. If it's a completely unrelated class, then the right way to do it depends on your design. – kiheru May 17 '15 at 16:10
  • So , when I open the application : there's a picture as a background and then 2 buttons : student login/register and teacher login/register . Then when you click for instance the stud button ( or student login/register ) another window appears and there are 3 buttons Login/Register/Go back to main menu and then when you click Register , there's a registration screen and so on . The problem is that on each click of the button , a new window appears which is quite annoying . – luka0309 May 17 '15 at 16:13
  • So , let's imagine a dictionary application having these words in its index aback abacus abrupt What my program does in this sense is that when you click one of these words a new window appears instead of staying in the same one . – luka0309 May 17 '15 at 16:15
  • actions(Object source,String command) is in EasyApp (http://ibcomp.fis.edu/Java/EasyApp.html , please scroll down to see the code ) . All my classes extend EasyApp – luka0309 May 17 '15 at 16:20
  • 1
    I can see it there, but I don't know what relation it has to `Register`. I don't know anything at all about your code, so don't assume I do. If you want switchable views in one window, use [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). If you simply want something to happen in the current window, see my previous comment. – kiheru May 17 '15 at 16:28
  • 1) `addButton1(io0, 35, 1223,901, 120, this);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson May 17 '15 at 22:24
  • .. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). Post it here, not at some external link. – Andrew Thompson May 17 '15 at 22:24

0 Answers0