test.java
import javax.swing.JFrame;
public class test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
}
}
My Other java File test2.java
import javax.swing.JButton;
public class test2 {
public static void main(String[] args) {
JButton Button = new JButton();
frame.add(Button);
}
}
am trying to call frame to test2.java
The reason you are getting this problem:
When you run a java application, the application's main function will be called. Therefore you should really only have one main function per application.
In your scenario you had 2 main functions. Think of this as 2 different applications. The following scenarios were happening:
When you run the Test class, your application was creating a new JFrame object. That's pretty much it, it ended there. It had no idea that the Test2 class existed.
When you run the Test2 class, your application was creating a new JButton object. Although, your Test2 class had no reference to the frame variable (that is why you were getting an error). It didn't even know there was a Test class.
In order to fix this in your situation, try this:
Test.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
Test2.java
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
A more OOP approach:
Here, I made a Driver class that would connect the Test2 and MyFrame classes together.
Driver.java
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
MyFrame.java
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
Test2.java
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
I assume you're trying to add Button to the JFrame frame you created in test To do this, you'll need to make frame visible to what is essentially the global scope, as such:
import javax.swing.JFrame;
public class test {
public static JFrame frame;
public static void main(String[] args) {
frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
test2.main(args)
}
}
and then, to add the button in test2, you need to access test by name
import javax.swing.JButton;
public class test2 {
public static void main(String[] args) {
JButton Button = new JButton();
test.frame.add(Button);
}
}
Related
My main class with a static CardLayout JPanel which includes a JPanel from the Home class.
import java.awt.*;
import javax.swing.*;
public class Runner1 extends JPanel{
public Runner1() {
initComponents();
}
private void initComponents() {
setLayout(new CardLayout());
pnlHome = new Home();
pnlSignIn = new SignIn();
add(pnlHome, "Home");
add(pnlSignIn, "SignIn");
}
public static void showJPanel(String s) {
CardLayout cl = (CardLayout) (pnlRunner.getLayout());
cl.show(pnlRunner, s);
}
public static void createAndShowGUI(){
pnlRunner = new Runner1();
JFrame frame = new JFrame();
frame.setTitle("Delivery System");
frame.setSize(new Dimension(500, 400));
frame.getContentPane().add(pnlRunner);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static Runner1 pnlRunner;
private JPanel pnlHome;
private JPanel pnlSignIn;
}
My other class where the actionlistener for the JButton doesn't get triggered, when debugging, the btnNewOrderActionPerformed doesn't get executed.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Home extends JPanel {
public Home() {
initComponents();
}
private void initComponents(){
setLayout(new BorderLayout());
add(new TextArea("Active Orders"),BorderLayout.CENTER);
JButton btnNewOrder1 = new JButton("New Order");
btnNewOrder1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
btnNewOrderActionPerformed(e);
}
});
add(btnNewOrder1, BorderLayout.PAGE_END);
}
private void btnNewOrderActionPerformed(ActionEvent e){
System.out.println("test");
}
private OrderMap[] JOrders; //Lists of JPanel of orders
private JButton btnNewOrder;
}
Another question regarding static implementation of the CardLayout JPanel, is there a non-static way of accomplishing the same thing (where the shown JPanel can be controlled by components from external classes)?
My other class where the actionlistener for the JButton doesn't get triggered
Works fine for me, after fixing up the code so it compiles. See comment of your initial question.
is there a non-static way of accomplishing the same thing (where the shown JPanel can be controlled by components from external classes)?
First, get rid of the static keyword on the method.
Then you have a couple of options:
pass a reference of the Runner1 class to each child panel.
in the child panel you can use the getParent() method to get a reference to the Runner1 class.
Once you have a reference you can then reference any method in the Runner1 class.
Does anyone know why my image is not loading? I tried many things and so far nothing, first of all the folder "IMGFiles" is already like Source Folder.
package Main;
import javax.swing.*;
public class Menu extends JFrame {
public Menu()
{
ImageIcon imagem = new ImageIcon(Menu.class.getResource("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png"));
JLabel logo = new JLabel();
logo.setIcon(imagem);
}
public static void main(String[] args) {
//new Menu();
JFrame janela = new JFrame();
janela.setSize(816, 419);
janela.setUndecorated(true);
janela.setVisible(true);
janela.setLocationRelativeTo(null);
}
}
I'm assuming you are using UNIX (for the shape of the path you are using).
Here is an approach for you:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import com.apple.eawt.Application;
public class Menu extends JFrame {
public Menu(){ }
public static void main(String[] args) {
Application.getApplication().setDockIconImage(new ImageIcon("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png").getImage());
//new Menu();
JFrame janela = new JFrame();
janela.setSize(816, 419);
janela.setUndecorated(true);
janela.setVisible(true);
janela.setLocationRelativeTo(null);
}
}
Output:
This happens because you prepare the JLabel inside Menu constructor and you do not initiate any instance of Menu class. Also, you do not add the JLabel into the frame (content pane).
Check this sample:
public class Menu extends JFrame {
public Menu()
{
ImageIcon imagem = new ImageIcon(Menu.class.getResource("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png"));
JLabel logo = new JLabel();
logo.setIcon(imagem);
setSize(816, 419);
setUndecorated(true);
setLocationRelativeTo(null);
getContentPane().add(logo); //Add the label to the content pane
}
public static void main(String[] args) {
SwingUtilities.invokeLater(()->{
new Menu().setVisible(true);
});
}
}
I bought a Mac, I I download netbeans for my java.
package gui;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
public void Gui(){
setTitle("Gui");
setSize(640,320);
setVisible(true);
}
public static void main(String[] args) {
new Gui();
}
}
It is very easy code and I didn't find any problem with it, but somehow the GUI is not showing up.
is GUI no suppose to show up on a Mac?
Somehow, the program didn't go through the Gui method, I tried
System.out.println("Hello");
didn't show up.
You think you're using a constructor but you are not! The constructor is what makes the app become a JFrame. This line:
public void Gui() {
should be:
public Gui() {
Also, nice to add a setMinimumSize(new Dimension(640,320));
I think the problem is you have a empty container, but I make an example for you:
import javax.swing.*;
public class Main {
private static JPanel panel1;
private static JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame( "Main");
panel1 = new JPanel();
button = new JButton("Button");
panel1.add(button);
frame.setContentPane(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setTitle("Gui");
frame.setSize(640,320);
frame.setVisible(true);
}
}
I have a class which has a main method. Now, I want to create an object of the JFrame class in that class, but as a global entity and not inside the main function, how can I do it? It gives me some compilation error when I just cut the instantiation code from inside the main and paste it outside.
public class Driver {
JFrame frame = new JFrame("Frame Heading");
public static void main(String[] args) {
frame.setSize(1000,500);;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You can use a combination of a static variable and static method:
public class YourClass {
private static JFrame frame = createFrame();
private static JFrame createFrame() {
frame = new JFrame();
// ... more code ...
}
public static void main(String[] args) {
// make use of frame
frame.show();
}
}
You can also use a combination of a static variable and static initializer:
public class YourClass {
private static JFrame frame;
static {
frame = new JFrame();
// ... more code ...
}
public static void main(String[] args) {
// make use of frame
frame.show();
}
}
How would i be able to run this function from my main() to build the gui, and then use code from elsewhere to handle the button click and retrieve input from the text field?
package main;
import javax.swing.*;
import java.awt.*;
public class Gui {
public static void mainGUI() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
java.net.URL imgApp = ClassLoader.getSystemResource("res/app.png");
JFrame mainWin = new JFrame("jIRC");
mainWin.setSize(1024, 720);
mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWin.setIconImage(new ImageIcon(imgApp).getImage());
Container panel = mainWin.getContentPane();
panel.setLayout(null);
JTextArea inputBox = new JTextArea();
inputBox.setSize(300, 100);
inputBox.setLocation(500, 250);
JButton sendButton = new JButton();
sendButton.setText("Send");
sendButton.setFont(new Font("Helvetica", Font.ITALIC, 16));
sendButton.setSize(72, 32);
sendButton.setLocation(500, 500);
panel.add(inputBox);
panel.add(sendButton);
mainWin.setVisible(true);
}
}
Here's my class with the main function:
public class Run{
public static void main(String[] args) {
main.Debug.startupDebug();
main.Gui.mainGUI();
}
}
How would I go about placing some of my code in a non-static field?
You've got everything in a static method, and that won't allow you to use any of the power of object-oriented programming. Consider creating OOP-compliant classes non-static fields and methods and with public getter and setter methods, and this way other classes can affect the behavior of this class.
Edit
You posted:
public class Run{
public static void main(String[] args) {
main.Debug.startupDebug();
main.Gui.mainGUI();
}
}
But what you need to do instead is something like:
public class Run{
public static void main(String[] args) {
GUI gui = new GUI();
Debug debug = new Debug();
debug.setGui(gui);
gui.setDebug(debug);
gui.startGui();
}
}
Or something similar. Again avoid using static anything.