fliping between a console program and a graphics program java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am attempting to write a simple program that simulates orbiting objects. The program starts out by asking the user for some variables, such as how many objects, there masses, velocities, initial positions, preferably all as a console program. Then once all the objects are established I would like for the program to pop over to a graphics program or some sort of java applet and just show the objects orbiting on the screen. All the math and animation stuff I can handle, I just don't know how to switch between console and graphics.

You can open a frame from a "console" application exactly the same way as from a GUI application.
For example:
import javax.swing.*;
import java.io.Console;
class Hello {
public static void main(String[] args) {
Console console = System.console();
String name = console.readLine("What is your name? ");
JLabel label = new JLabel("Hello "+name+"!");
JFrame frame = new JFrame("Hello!");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}

Related

Creating a Button in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I am fairly new to programming as I am only in a Computer Science class in high school. I have thought of the idea of creating a chess program that basically simulates a game of chess. To do this I figured I would have the users click on the spot that they would want to move to.
I have looked all over the internet, including java's API's, and I have found some very useful information. After all of this however, I am still very confused about all of the different methods and classes as well as interfaces to use to create and use Buttons in Java. Although this isn't a question about code(Sorry), I was wondering if anybody knew of any tutorials that would be suitable for my situation. All I am looking for is something that can show me how to create and use a simple, one function button. Preferably, it would be nice if it describes all of the methods so that I actually understand what I am doing.
Again, sorry this isnt a question about code. I couldn't think of a better place to ask this question, than Stack Overflow so please do not down-vote this "question".
Thanks
Here is a totally self contained example (with some code comments to help you understand what is going on):
//Here I am using Swing and AWT (a rather standard way to manage UI elements in Java though technically not the only way)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
//Here is a base class that extends JFrame, JFrames are containers for Swing UI widgets that are represented as windows when executed
public class ButtonExample extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
//Creatign the frame
ButtonExample frame = new ButtonExample();
//Creating the button with the label "Click me!"
JButton button = new JButton("Click me!");
//Adding an action listener so we can assign some logic to be executed when this button is clicked on (this is using an anonymous inner class in future versions of Java this will be replaced by the MUCH cleaner Lamba approach, keep an eye out for that)
button.addActionListener(new ActionListener() {
//Keep a variable to store how many times the button is clicked. This shows that the action listener stays running in between clicks)
private int count = 0;
//While technically optional thew #Override annotation helps if you update interfaces, get into the habit of doing this to make future work easier, things like Eclipse will insert it for you
#Override
public void actionPerformed(ActionEvent e) {
//Bump up the count variable
this.count++;
//And print it to System.out
System.out.println("Pressed "+this.count + " times");
}
});
//Add the button (with it's listener) to the frame
frame.add(button);
//Tell Swing to resize the frame to fit the requested size of all of it's contained widgets
frame.pack();
//Tell Swing to show the frame
frame.setVisible(true);
}
}
Try:
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Do what you like here after button is clicked, for example:
System.out.println("Button clicked");
}
});
someJPanel.add(button);
To make such application - you will need lot's of knowledge. Also it should look nice, so lot's of work with graphics in Java..
Try to read this documentation: Here You Are
There, you can download, launch the example, so you can see, try every button..

How do I create a Java Swing component that contains only an image and a JLabel? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to make a simple component that does nothing other than draw an arbitrary image, and then directly beneath it place a JLabel for displaying arbitrary text.
How can I achieve this? I'm brand new to Swing and I'm trying to learn as I go, but I don't currently understand how I would go about doing this. I know it's a basic question, and I appreciate any help I can get.
JLabel is your friend:
JLabel label = new JLabel("Your text here");
label.setHorizontalTextPosition(SwingConstants.CENTER);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setIcon(new ImageIcon(this.getClass().getResource("/path/to/image/image.jpg")));

Making a multi-tiered program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on an assignment in which I need to combine two programs that I have created into one functioning program. The end result I am hoping for is a program that once launched, opens a log in window, then once logged in, the user gets to play a tic tac toe game. Basically I just was wonder how to have a window within which when you click a button, a new window opens that can run extensive code.
If you're using Swing framework, Create a second JFrame and set its visibility to false, and when the button is clicked, set it visibility to true.
public class MyFrame extends JFrame {
private JButton jbt = new JButton("Open Window");
private AnotherFrame jfrm = new AnotherFrame();
public MyFrame(){
add(jbt);
jfrm.setVisibility(false);
add(jfrm);
jbt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jfrm.setVisibility(true);
}
});
}
private AnotherFrame extends JFrame {
public AnotherFrame(){
}
}
}

Opening a JFrame within a JFrame [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a main JFrame with a button that generates another JFrame. However, when I close the 2nd JFrame the main JFrame is also closed which is not what I want. What am I doing wrong?
I assume you're setting the default closing action as:
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
The JFrame.EXIT_ON_CLOSE constant terminates the whole program when you click the x button. You probably want to use DISPOSE_ON_CLOSE or HIDE_ON_CLOSE instead. I recommend to have a look at the JavaDoc of JFrame.

How to add title in Netbeans (java)? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Just can't figure this out in my GUI, I know it's probably simple but thought I'd ask.
I assume I need something in this code:
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new myGUI().setVisible(true);
}
});
}
Thanks.
Edit - for some reason public static etc. isn't showing up above - but you probably don't need that.
You're not showing us the whole code, so this is only a guess, but probably a good one: I assume that myGUI is a subclass of JFrame, and you want to call setTitle() on it. Something like
myGUI m = new myGUI();
m.setTitle("The Title");
m.setVisible(true);
You'd put these three lines in place of the one new myGUI().setVisible(true); .
If you are in Netbeans do you have a screen/form in which you have been designing your Frame?
If so open the navigator toolbar and right click on your frame, select properties and look for the one called title, and type in your text there.
Otherwise you will need to add something like
myGui.setTitle("A title") // assuming its a JFrame

Categories