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(){
}
}
}
Related
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..
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'm afraid I don't have any code because I don't really know what I'm doing. I'm trying to learn some Java by creating a basic game and want to make a menu. What I want to do is have a class with a custom JButton (basically a JButton of a certain size and a picture on the background, formatted specially, etc.) which can be called from another class and given custom text when it is called. My question is, how do I create the custom button which can be called externally?
create a class and extend it to JButton like below and there you can change all sort of things that has to do with JButton.
import javax.swing.JButton;
public class CustomJButton extends JButton {
public CustomJButton() {
this.setText("Custom JButton");
// initialize
}
// add your own methods or override JButton methods
public void myFunc(){
}
}
In the constructor for your custom JButton, you'll want to accept a parameter that specifies the text you want the button to contain, and you'll specify the other things that you don't want to change from instance to instance, such as the size or the background image.
import javax.swing.*;
class MyButton extends JButton{
public MyButton(String text){
super(text);
...set size, add background image, etc...
}
}
No this button is a JButton you can create a new one
like normally, MyButton btn = new MyButton(); You just added an extra method that will format it the way you want. If you want to learn more about this topic look at object inheritance.
public MyButton extends JButton{
public void doStuff(){
this.setBackground("yourBackground");
...
}
}
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);
}
}
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
In Java's swing package, I was wondering how to detect when a JButton is pressed. Is there a function that is called when the button is pressed? Thanks
Yes, when you deal with button pressing, you want to add what is known as an action listener. First, you must
import java.awt.event.ActionListener;
then you can do the following
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// this makes sure the button you are pressing is the button variable
if(e.getSource() == button) {
// do action necessary code
}
}
});
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