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);
}
}
Related
I'm using the Java Swing UI Designer in IntelliJ :( I designed something in the designer using multiple panels and spacers with 1 parent panel. When I add the main panel, the first one inside it shows, but the others don't.
Frame structure:
Panel1
GradientPanel
Panel
Spacers
What I designed
What I get
import keeptoo.KGradientPanel;
import javax.swing.*;
public class LogIn extends JFrame{
private KGradientPanel KGradientPanel1; //Automatically added by the designer
private JPanel panel1; //Automatically added by the designer
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("CarbonTec Dashboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(1800,1000);
frame.setContentPane(new LogIn().panel1);
frame.setVisible(true);
ImageIcon imageIcon = new ImageIcon("Icon.png");
frame.setIconImage(imageIcon.getImage());
}
}
First you need to know that your class is a JFrame, but in the main method you create a new JFrame.
Better would be to have a class Program that has the main method. In this main method you make a new instance of LogIn.
The Program class can look like this:
public class MainProgram {
public static void main(String[] args) {
LogIn logIn = new LogIn();
}
}
The LogIn class should then look like this:
import keeptoo.KGradientPanel;
import javax.swing.*;
public class LogIn extends JFrame{
private KGradientPanel KGradientPanel1 = new KGradientPanel(); //Automatically added by the designer
private JPanel panel1 = new JPanel(); //Automatically added by the designer
// This is the constructor.
public LogIn {
setTitle("CarbonTec Dashboard");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(1800,1000);
setContentPane(panel1);
ImageIcon imageIcon = new ImageIcon("Icon.png");
setIconImage(imageIcon.getImage());
// Here you can add the gradient panel to panel1.
panel1.add(KGradientPanel1); // The name should be written in lower case.
setVisible(true);
}
But I don't know why you need the panel1, you could add the KGradientPanel directly.
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.
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);
}
}
I have never actually worked with GUI's before when it comes to Java. I am trying to add a simple button to the JFrame, but it doesn't add. This is the way that I have been seeing online can someone point out what I'm doing wrong?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.event.*;
public class WavPlayer
{
public void go()
{
JFrame frame = new JFrame("Wav Player");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JButton play = new JButton("test");
play = new JButton("Test");
frame.setSize(500, 500);
add(play);
}
public static void main(String [] args)
{
WavPlayer player = new WavPlayer();
player.go();
}
}
You have to specifiy where you want your button to be added.
use frame.add(play) instead of add(play)
You also have several other errors in this code, you have to state the type of "play".
To actually see something, you have to set the visibility of your Frame.
Here is my Code for your Problem (I renamed the class, you have to Change it):
public void go(){
JFrame frame = new JFrame("Wav Player");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton play = new JButton("Test");
frame.setSize(500, 500);
frame.add(play);
frame.setVisible(true);
}
public static void main(String [] args)
{
Main player = new Main();
player.go();
}
I want to run this code that will create a window with a simple button on it. The program will run in Netbeans on a Mac but the problem is that it does not work. Here is the code below.
import javax.swing.JFrame;
public class Test {
public static JButton button(){
JButton button = new JButton("random button");
}
public static void main(String[] args) {
button();
new JFrame();
}
}
Please help me figure this out soon please. Thank you.
You're not adding the button to anything or displaying the JFrame. Your method returns a JButton object, but you're not doing anything with this object.
Create a JPanel
Add the JButton to the JPanel
Add the JPanel to the JFrame
Display the JFrame by calling setVisible(true)
Most important: Making up code and hoping it will magically work is not a successful heuristic for learning to program. Instead read the Swing tutorials which you can find here.
For example
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JButton button = new JButton("Button");
JPanel panel = new JPanel();
panel.add(button);
JFrame frame = new JFrame("foo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}