简体   繁体   中英

Java Swing Frame navigate to another Frame

This is my main class.

package pomsystem;

public class POMSystem {

    public static void main(String[] args) {
        new ItemList();
    }
}

This is the second class frame that I want to navigate.

package pomsystem;

import java.awt.Button;
import java.awt.Color;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

class UI extends JFrame{
    TextField txtID, txtItem, txtStock, txtSupplierID;
    Label lblID, lblItem, lblStock, lblSupplierID;
    Button btnSearch, btnClear, btnBack;
}

public class ItemList extends UI {
    private String ID;
    private int Stock;

    public ItemList(String ID, int Stock) {
        setSize(600, 400);
        setLocation(380, 120);
        setLayout(null);
        setTitle("Item Entry");
        setVisible(true);
        setBackground(Color.LIGHT_GRAY);
    }
}

It shown me a error with Constructor in class cannot be applied to given types, I know the error is from the parameters of second frame. Is that any approach to solve the problem. I'm new to Java OOP sorry.

You declared a custom constructor:

    public ItemList(String ID, int Stock){


    setSize(600,400);
    setLocation(380,120);
    setLayout(null);
    setTitle("Item Entry");
    setVisible(true);
    setBackground(Color.LIGHT_GRAY);}

overwriting the standard empty Java Object constructor, which is:

public ItemList(){}

Just add the constructor without arguments again in your code as an alternative constructor:

 public ItemList(){

    setSize(600,400);
    setLocation(380,120);
    setLayout(null);
    setTitle("Item Entry");
    setVisible(true);
    setBackground(Color.LIGHT_GRAY);}

}

Otherwise you could also call your custom constructor with values:

new ItemList("example", 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM