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
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Cheers, lads.
I am currently standing in front of a minor problem, but it really drives me insane, that I'm not able to fix it.
My very first mistake was to use my main-JFrame as main-class, as well.
You will see why this is (as far as I am able to judge) a problem later...
Now I am opening a new JFrame from my main-class-main-JFrame and I want to disable it as long as the new JFrame is opened.
I've already read much about using JDialog to do this, but I did not yet managed to find a solution without having to redesign my whole sub-JFrame.
Is there an easy way to just disable the mainJFrame as long as the subJFrame is opened?
Something like:
JFrame subframe = new GUI_subJFrame(<params>);
this.disable();
subframe.onClose(this.enable());
I know this is awful and not existent source code, but I wanted to make my thought clear, accurately.
I just changed the "subJFrame" from JFrame to JDialog and added the following line to the constructor:
this.setModalityType(DEFAULT_MODALITY_TYPE);
It works fine and is not as complicated as it seemed at first glance.
Thanks to everyone for their help.
Use frame.dispose(); to close the frame
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have written a simple code using Java Selenium.
After quitting the firefox i want a pop up message to be displayed in the desktop saying "script executed."
This is my Selenim code-
public class test1 {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://google.com");
driver.quit();
}
}
is there a way to do it??
if so, please guide me.
If you want it to display on the desktop you can look for an IDE with a GUI builder like Eclipse (with plugin), NetBeans or other.
Or if you want to code it take a look at this sample:
private void window()
{
JFrame jf = new JFrame();
jf.setSize(x,y);
jf.setTitle("Title");
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jf.setVisible(true);
}
The code above should be a good starting point, it creates a blank window. You can then look further into Java Swing and awt for more info.
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 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(){
}
}
}