I'm using the Intellij idea platform.
I have the following code:
package GUI.test;
import javax.swing.*;
public class Ramka extends JFrame{
Ramka(){
setVisible(true);
setSize(100,100);
}
public void main (String[] args){
new Ramka();
}
}
I expected to see a JFrame after compiling this code, but was nothing appeared. What kind of problem can it be?
Also must admit, that I haven't possibility to run method "main". InteligIdea propose me only to compile Ramka.java. After compiling IntelijIdeay says, that compilation completed successfully, but thats all and is nothing happened. In my previous exercises I always ran method "main".
You should do something like this:
public static void main (String[] args){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ramka().setVisible(true);
}
});
}
Besides missing the static identifier at main, you also have to make sure that your frame runs in the right thread check "concurrency" for swing
You would see your Ramka if you actually ran main. Main should always be declared public static void main(String[] args) where you forgot the static. If you change it to:
public static void main (String[] args){
new Ramka();
}
it should run.
Related
I am using Java 11 on Debian 4. I am trying to build a very basic Java GUI. To start with I have the following code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class BasicSwing extends JFrame {
JPanel p = new JPanel();
JButton b = new JButton("Hello");
public static void main (String[] args) {
new BasicSwing();
}
public BasicSwing() {
super("Basic Swing");
setSize(400,300);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p.add(b);
add(p);
setVisible(true);
}
}
I have the X11 server running. The code does not fail but the GUI does not show up. I am not using Netbeans and I compile and run the code just as I would run and compile any other java code, ie with javac and java commands.The code does not stop and does not throw any error. Am I missing something very basic? I have seen a lot of discussion on the GUI not showing up but I am unable to find a solution to this problem given my specific development environment.
Instead of calling the setVisible method inside of your JFrame extended class's constructor, You should make a call on it in your main function.
Do it this way:
public static void main (String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BasicSwing mySwingApp = new BasicSwing();
mySwingApp.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Please read more about why I use a java.awt.EventQueue here.
Update:
It's not good practice to directly create a class that extends JFrame. Also, please read this too, for more clarification.
I am trying to run this program in Java:
import java.awt.*;
import javax.swing.*;
public class PanelTest extends JPanel
{public void paintComponent(Graphics g)
{g.setColor(Color.green);
g.drawString("Hello World",30,100);
}
}
When I run it shows a box in which it's written:
No main methods,applets or MIDlets found in file.
I don't understand what do I have to do.
Can someone explain this to me. Any help would be appreciated.Thank you!
Every Java program requires a public static void main (String[] args) as main method. This method is the program's entry point. Everything starts from there.
You can then instantiate the Panel in that main method.
Something like this:
public static void main (String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new PanelTest());
frame.setVisible();
}
As specified in the error text, the problem is that you do not have a main method for your program. All Java programs must have a main method, since that is the starting point for Java Applications.
A typical main class for your simple application would be:
public class Main {
public static void main(String args[]){
PanelTest pt = new PanelTest();
pt.paintComponent(..);
}
}
In the above, replace the '..' with the actual method arguments. I would definitely going through the basic concepts of Java. There are some great tutorials out there.
Every Java program should be started with a main method:-
public static void main(String[] args) {
// Starting code here
}
I'm trying to initialize the MPJ inside a function but I cant make the MPI.Init(args) work. I tried dragging along the args like this
import mpi.*
public class Test {
String [] args
public static void main(String [] args){
Test.args = args;
t.testFunction();
}
public void testFunction() {
MPI.Init(args);
}
}
but I get an exception like this
Exception in thread "AWT-EventQueue-0" mpi.MPIException: Usage: java
MPI conf_file can be,
../conf/xdev.conf OR
http://holly.dsg.port.ac.uk:15000/xdev.conf
at mpi.MPI.Init(MPI.java:232)
The problem is my program works with an action listener on timer interrupts so I cant put it all inside the main method.
Thanks in advance for any solutions or even ideas.
I've seen similar questions answered but could not find an answer to my question. I have a Main Class, which has it's own JFrame. However, I've created a different Class where I've created another JFrame that prompts the user for some data. The Main Class is the main app. The secondary class is supposed to pop up before the main class GUI runs. I've created 2 different packages for each one of the Classes.
So, I'm trying to call an Object of the secondary Class from Main Class but the interface does not appear. I do not get any errors in the code and the App runs as if the Object of secondary Class is not being called at all. I am new to Java and would appreciate some lights on this.
My code is as follows:
Main Class
public class TempConverter extends javax.swing.JFrame {
public TempConverter() {
initComponents();
}
// More code
public static void main(String args[]) {
DemoUserData test = new DemoUserData();
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
test.setVisible(true);
new TempConverter().setVisible(true);
}
});
}
Secondary Class
public class DemoUserData extends javax.swing.JPanel {
public DemoUserData() {
initComponents();
}
}
Your JFrame is the main window. Before it is shown at the very early start a splash screen maybe shown, normally a small rectange with a logo.
It however seems, you want some input dialog, like say a login. That cannot be a JPanel, but must be a top-level window: JFrame or JDialog. Or one of the JOptionPane dialogs (asking string input, or whatevever).
Maybe you should make a JFrame for your current JPanel, run that.
.
DemoUserDataFrame test = new DemoUserDataFrame(this);
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
test.setVisible(true);
}
});
public class DemoUserDataFrame extends JFrame {
//private final JFrame tempConverter;
public DemoUserDataFrame(final JFrame tempConverter) {
//this.tempConverter = tempConverter;
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
tempConverter.setVisible(true);
}
});
}
...
}
In the above, closing test, will make the main JFrame visible.
In order to have a better overview, have the classes not refer one to another, you might look into the Model-View-Controller concept. Then there is one global "Controller" class as intermediator for all business logic. It holds the data (Model), and so on.
So the program I am making uses 2 threads: One for the GUI and one to do the work.
I want updates from the work thread/class to print out on JTextArea in GUI class.
Everything I tried didn't seem to work. I added lines to print out text on the console right after lines to add text to the JTextArea to make sure it had got to the line but everytime console got text but no changes happened to JTextArea in the GUI.
public static void consoleText(String consoleUpdate){
GUI.console.append(consoleUpdate);
}
I tried this in the work class but nothing happened.
Anyone know how to fix my problem?
Edit:
MAIN.JAVA
public class main {
public static void main(String[] args) {
Thread t1 = new Thread(new GUI());
t1.start();
}
GUI.JAVA
public class GUI extends JFrame implements Runnable{
public static JTextArea console;
private final static String newline = "\n";
public void run(){
GUI go = new GUI();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(350, 340);
go.setVisible(true);
}
public GUI(){
setLayout(new FlowLayout());
console = new JTextArea(ConsoleContents, 15, 30);
add(console);
}
WORK.JAVA
...{
consoleText("\nI want this text on the JText Area");
}
public static void consoleText(String consoleUpdate){
GUI.console.append(consoleUpdate);
}
First, as has been said, your GUI should only run on the Event dispatch thread.
As it is written, your GUI class does two things : it's a frame, and a runnable, and both
are used completely independently. As a matter of fact, calling "run" on a your GUI object creates another, unrelated GUI object. That's probably the reason why you see nothing.
So I suggest making your main the following:
... main(...) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI gui= new GUI();
gui.setVisible(true); // and other stuff
}
});
}
(I would also suggest getting rid of all "static" fields BTW. It's probably the source
of your problems, along with the weird place of the "run" method).
Now, your "consoleText" method, which I assume you call from another thread, should not
modify the text directly, but call SwingUtilities.invokeLater() to do so :
public void consoleText(final String consoleUpdate){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
console.append(consoleUpdate);
}
});
}
(the "final" declaration is important, as it allows the Runnable to use the consoleUpdate variable).