-
GUI new frame
hi there im quite new to this and i am trying to find out how to create a new frame with a third button, by pressing on the second button in my original frame my code is shown below any help would be greatly appreciated. thanks
// GuiDemo4
// Simple example of 2 JButtons
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiDemo4 extends JFrame {
private JButton myFirstButton;
private JButton mySecondButton;
// Constructor for a new frame
public GuiDemo4() {
super("My First Button Program");
myFirstButton = new JButton("PRESS LEFT");
myFirstButton.setFont(new Font( "Arial", Font.BOLD, 18));
myFirstButton.setBackground(Color.red);
mySecondButton = new JButton("PRESS RIGHT");
mySecondButton.setFont(new Font( "Arial", Font.BOLD, 18));
mySecondButton.setBackground(Color.green);
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add (myFirstButton);
c.add (mySecondButton);
ButtonHandler handler = new ButtonHandler();
myFirstButton.addActionListener(handler);
mySecondButton.addActionListener(handler);
setSize(400, 300);
setVisible(true);
}
public static void main(String [] args) {
// Make frame
GuiDemo4 f = new GuiDemo4();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// This closes the window and terminates the
// Java Virtual Machine in the event that the
// Frame is closed by clicking on X.
System.out.println("Exit via windowClosing.");
System.exit(0);
}
}
);
} // end of main
// inner class for button event handling
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == myFirstButton) {
System.out.println(
"Left Button has been pressed.");
}
if (e.getSource() == mySecondButton) {
System.out.println(
"Right Button has been pressed.");
}
}
} // end of inner class
} // end of outer class
-
I prefer to create JDialogs if I am trying to create secondary windows rather than new JFrames. The JDialogs behave better (imo) and can be easily set to modal or not. You create new JFrames or JDialogs in an action listener just as you currently do in your program -- there's no magic to it. For instance:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiDemo4 extends JFrame
{
private JButton myFirstButton;
private JButton mySecondButton;
private JButton button3;
public GuiDemo4()
{
super("My First Button Program");
myFirstButton = new JButton("PRESS LEFT");
myFirstButton.setFont(new Font("Arial", Font.BOLD, 18));
myFirstButton.setBackground(Color.red);
mySecondButton = new JButton("PRESS RIGHT");
mySecondButton.setFont(new Font("Arial", Font.BOLD, 18));
mySecondButton.setBackground(Color.green);
button3 = new JButton("New Window");
button3.setFont(mySecondButton.getFont());
button3.setBackground(new Color(180, 100, 255));
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add(myFirstButton);
c.add(mySecondButton);
c.add(button3);
ButtonHandler handler = new ButtonHandler();
myFirstButton.addActionListener(handler);
mySecondButton.addActionListener(handler);
button3.addActionListener(handler);
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args)
{
GuiDemo4 f = new GuiDemo4();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("Exit via windowClosing.");
System.exit(0);
}
});
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == myFirstButton)
{
System.out.println("Left Button has been pressed.");
}
if (e.getSource() == mySecondButton)
{
System.out.println("Right Button has been pressed.");
}
if (e.getSource() == button3)
{
JPanel pane = new JPanel(new GridLayout(0, 3, 5, 5));
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
final String btnString = "[" + i + ", " + j + "]";
JButton btn = new JButton(btnString);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("button " + btnString + " pressed");
}
});
pane.add(btn);
}
}
JDialog dialog = new JDialog(GuiDemo4.this, "My Dialog", true);
dialog.getContentPane().add(pane);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
}
}
}
-
Gui demo
thanks very much that has helped me out, but instead of the grid how do i get just a button in the new dialog box? thank you for your help.
-
hm, delete the code that makes a grid to the JPanel, and instead add a single JButton. You'll probably not want to have the pane JPanel use GridLayout though.
Have you gone are are you currently going through the Sun Swing tutorials? I highly recommend them.
-
Gui new frame
Which bits do i need to edit in particular?? because when i try to add the new button i get a lot of error messages which makes me think i have deleted something that i shouldnt have. i have been going through some tutorials in a book i have but they do not explain it very well.
-
Hmm, I think that for you to get any more from this forum, you are going to need to do more studying. Please reread the Sun Swing tutorial section on JPanels and buttons.
If you have further questions, then lets see your code, your error messages and comments telling where the error is being thrown.
Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Code:
[code]
// your code goes here
// notice how the top and bottom tags are different
[/code]
Luck.
-
gui new frame
I have added the button but when i compile it says that it has run succesfully but it sill runs the old code, any help would be greatly appreciated.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiDemo4 extends JFrame
{
private JButton myFirstButton;
private JButton mySecondButton;
public GuiDemo4()
{
super("My First Button Program");
myFirstButton = new JButton("PRESS LEFT");
myFirstButton.setFont(new Font("Arial", Font.BOLD, 18));
myFirstButton.setBackground(Color.red);
mySecondButton = new JButton("PRESS RIGHT");
mySecondButton.setFont(new Font("Arial", Font.BOLD, 18));
mySecondButton.setBackground(Color.green);
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add(myFirstButton);
c.add(mySecondButton);
ButtonHandler handler = new ButtonHandler();
myFirstButton.addActionListener(handler);
mySecondButton.addActionListener(handler);
button3.addActionListener(handler);
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args)
{
GuiDemo4 f = new GuiDemo4();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("Exit via windowClosing.");
System.exit(0);
}
});
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == myFirstButton)
{
System.out.println("Left Button has been pressed.");
}
if (e.getSource() == mySecondButton)
{
JFrame pane = new JFrame(new JButton());
myThirdButton = new JButton("PRESS RIGHT");
myThirdButton.setFont(new Font("Arial", Font.BOLD, 18));
myThirdButton.setBackground(Color.green);
{
final String btnString = "[" + i + ", " + j + "]";
JButton btn = new JButton(btnString);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("button " + btnString + " pressed");
}
});
pane.add(btn);
}
}
JDialog dialog = new JDialog(GuiDemo4.this, "My Dialog", true);
dialog.getContentPane().add(button);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
}
}
}
-
Some errors that are present:
- button3 is being added an action listener, but is never declared, never used.
- myThirdButton object is used but never declared, never placed into a root container (JFrame, JDialog)
- A JFrame is being constructed with a JButton parameter (this constructor doesn't exist)
- The JFrame is never rendered, never shown.
- left-over code from my previous JDialog example is kept in the code without removing it.
I recommend that you get rid of the current second button's actionPerformed code and recode it from scratch, getting rid of the debris from previous code. You really really need to study the tutorials. The things you are trying to do just won't fly until you gain the knowledge to be found there. Trust me, you won't regret this study.
-
gui new frame
I have studied the tutorials and i have made another attempt i get one error though that reads:
E:\Web site\GuiDemo4.java:78: cannot find symbol
symbol : variable myFourthButton
location: class GuiDemo4.ButtonHandler
c.add(myFourthButton); }
The new frame is created but the button does not appear. any help would again would be greatly appreciated
^
1 error
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiDemo4 extends JFrame
{
private JButton myFirstButton;
private JButton button3;
private JButton myFourthbutton;
public GuiDemo4()
{
super("My First Button Program");
myFirstButton = new JButton("FIRST BUTTON");
myFirstButton.setFont(new Font("Arial", Font.BOLD, 18));
myFirstButton.setBackground(Color.red);
button3 = new JButton("NEW FRAME");
button3.setFont(myFirstButton.getFont());
button3.setBackground(new Color(180, 100, 255));
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add(myFirstButton);
c.add(button3);
ButtonHandler handler = new ButtonHandler();
myFirstButton.addActionListener(handler);
button3.addActionListener(handler);
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args)
{
GuiDemo4 f = new GuiDemo4();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("Exit via windowClosing.");
System.exit(0);
}
});
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == myFirstButton)
{
System.out.println("Left Button has been pressed.");
}
if (e.getSource() == button3)
{
JFrame frame = new JFrame("New Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add(myFourthButton); }
ButtonHandler handler = new ButtonHandler();
myFirstButton.addActionListener(handler);
button3.addActionListener(handler);
setSize(400, 300);
setVisible(true);
}
}
}
-
Java is case sensitive, so cApItAlIzAtIoN matters. Check out the button variable closely and you'll see what I'm referring to. Good luck.
edit:
you'll also need to initialize this 4th button variable before you use it just as you would any JButton / any object. Also make sure that you're adding an actionlistener to the right button (look at the place where you create your new JFrame).
Also, I again recommend that you not create a JFrame here but instead a JDialog unless there's a pressing reason to create a JFrame. If so, please post that reason so I can stop mentioning this.
edit: also, you'll need to check to make sure that you're adding a new button to the correct container. I'm guessing that you want to add it to the contentPane of the new Window that you are creating, not the old window that launches the new window....
edit: and, you'll not want to set the new window visible until after you've added components such as JButtons. This is well spelled out in the tutorials
-
Please see the multiple comments above.
-
I do apologise in advance if this sounds a bit cheeky below is another attempt of mine that is slightly unsuccesfull as i am printing the new button in the same frame and i dont know how to correct, as this is just a hobbie of mine i would be greatful if you could post the solution as it has been niggling at me all day. thanks for your support.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiDemo4 extends JFrame
{
private JButton myFirstButton;
private JButton button3;
private JButton myFourthbutton;
public GuiDemo4()
{
super("My First Button Program");
myFirstButton = new JButton("FIRST BUTTON");
myFirstButton.setFont(new Font("Arial", Font.BOLD, 18));
myFirstButton.setBackground(Color.red);
button3 = new JButton("NEW FRAME");
button3.setFont(myFirstButton.getFont());
button3.setBackground(new Color(180, 100, 255));
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add(myFirstButton);
c.add(button3);
ButtonHandler handler = new ButtonHandler();
myFirstButton.addActionListener(handler);
button3.addActionListener(handler);
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args)
{
GuiDemo4 f = new GuiDemo4();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("Exit via windowClosing.");
System.exit(0);
}
});
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == myFirstButton)
{
System.out.println("Left Button has been pressed.");
}
if (e.getSource() == button3)
{
JFrame frame = new JFrame("New Frame");
frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
myFourthbutton = new JButton ("Fourth");
myFourthbutton.setFont(myFirstButton.getFont());
myFourthbutton.setBackground(new Color(180, 100, 255));
Container c = getContentPane();
FlowLayout f1 = new FlowLayout(FlowLayout.LEFT);
c.setLayout(f1);
c.add(myFourthbutton); }
ButtonHandler handler = new ButtonHandler();
myFirstButton.addActionListener(handler);
button3.addActionListener(handler);
myFourthbutton.addActionListener(handler);
setSize(400, 300);
setVisible(true);
}
}
}
-
It compiles, and that's good, and it shows the fourth button on the new JFrame, also good. What's not working. BTW, this is a hobby of mine too. It is fun, but takes a lot of work.
-
thats strange because when i run it the fourth button appears on the original frame below the "first button" and "new frame".. i know it does take a lot of work lol :-D another query is how do i make the second frame a bigger size than it is at the minute. thanks again, you have really been a big help today
-
read the tutorial on using layout managers in the Swing tutorials. There you'll see that most layout managers use the preferred size when trying to set the size of a component.
So I sometimes set the preferred size of key components in my display, sometimes the main JPanel or contentPane. Be sure to call "pack()" on the JFrame or JDialog before calling setVisible(true) as this will then tell the layout managers of the app to layout the components properly.
-
HEY IAM NEW TO JAVA WOULD KNOW HOW TO CREATE THIS PROGRAM AS A START TO THE END, PLEASE HELP ME.
Import java.util.Scanner;
public class triangles
{
public static void main(String [] args
1. Suppose we define a class Isosceles, which can represent an isosceles triangle whose base is aligned with the horizontal directions of the display, using four parameters: x, y, b, h, where (x,y) are the coordinates of the center of the base, b is the length of the base, and h is the height. Suppose we define the relationship "C contains A" to indicate that all of the area or perimeter of Isosceles A is in or on the area or perimeter of Isosceles C.