So how do i make a button in my JPanel i am really noob and can't seem to find it.
My Main class:
public static void main(String[] args) {
JFrame frame = new JFrame("Casino");
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setContentPane(new Paneel());
}
My JPanel
public class Paneel extends JPanel {
JPanel paneel = new JPanel();
JButton test = new JButton("test");
paneel.add(test);
}
i also have an basic question. The 'big people' do they make their layout directly from a .form file or do they actually write everything them self where to stand and such cz the drag drop seems easier or is it not the way to do it?. What i mean is this: Click here for the image
Related
I have a panel that I am drawing some stuff on and I want to have an interface on top of it. I created an interface as a JPanel on netbeans, visually. But interface is not displayed properly.
Here is my code
public static void main(String[] args) {
JFrame frame = new JFrame("WorldGen");
Interface inter = new Interface();
JLayeredPane lpane = new JLayeredPane();
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
lpane.setBounds(0, 0, 600, 400);
lpane.add(panel, new Integer(0), 0);
lpane.add(inter, new Integer(1), 0);
panel.setBounds(0,0,600,400);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main = new Main();
}
Panel is declared as a static JPanel.
static JPanel panel = new JPanel()
Here is my result:
This is the Interface class that is created in netbeans visually
When I add this line:
inter.setBounds(0,0,600,400);
inter.setOpaque(true);
this is what I get:
Just a blank screen. I don't expect it to be transparent since I set it to opaque myself but It seems I have another problem. The button is not showing whether I set it to opaque or not.
Why is the button not showing? I am hoping that the button will still be visible when I set opaque to false, after I resolve this problem.
I've solved it by creating a JFrame in netbeans visually, adding a JPanel to it. Then using that panel(by overriding the paint method) to draw my image.
I've finished my store management program with many classes (many JForm panels actually. Thanks to people in this forum whom helped me so much).
I just need to call JPanel Login when I click Run Project.
Any idea how to call it? what code I have to insert to main project?
You cannot display JPanel independently.
So you have to create a JFrame and then add this JPanel object to this JFrame and then display the JFrame.
write these lines in your main method.
JFrame frame = new JFrame("Title");
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // as per your requirement.
frame.add(new Login(), BorderLayout.CENTER);
frame.setVisible(true);
it will work properly.
JFrame myFrame = new JFrame("MyTitle");
myFrame.setSize(width, height);
myFrame.setLocation(x, y);
myFrame.setContentPane(new Login());
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// if you want add your jcomponent
myFrame.getContentPane().add(yourJComponent);
try it!
If you want add JComponent(JButton)
JButton myBtn = new JButton("btn name");
myBtn.setLocation(x, y);
myFrame.getContentPane().add(myBtn);
If you want add JComponent(JText)
JText myText = new JText("text");
myText.setLocation(x, y);
myFrame.getContentPane().add(myText);
JComponent [http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html]
I am trying to display a gui but cannot get the frame to show here is the code so far:
The idea behind this is that the string path (which is the path to an image) is calculated in another class, it is then passed to this class where the image is to be displayed.
I cannot get the frame to be displayed, my usual method would be:
new displayWindow();
but this does not work.
What would be the best method of displaying the gui?
public class displayWindow {
public displayWindow(String path) {
JLabel label = new JLabel();
ImageIcon icon = new ImageIcon(speed);
label.setIcon(icon);
label.setText(path);
JPanel panel = new JPanel();
panel.add(label);
JFrame frame = new JFrame("Speed Limit");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println(path);
frame.setLayout(new BorderLayout());
frame.setLayout(new FlowLayout());
frame.setSize(430, 430);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.repaint();
}
public static void displayWindow() {
new displayWindow();
}
}
With the code you provide, your code even compile cause you don't have a default constructor with no args. Your constructor takes one parameter.
So your method should be:
public static void displayWindow(String param) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
new displayWindow(param);
}
});
}
Using SwingUtilities.invokeLater(..) you ensure that will run in the EDT(Event Dispatch Thread.)
Is there a reason for this?
frame.setLayout(new BorderLayout());
frame.setLayout(new FlowLayout());
frame.setSize(430, 430);
In java by convention class names starts with upperCase so you class should be called DisplayWindow this is very important for readability.
And call setVisible(true) after you add components to your frame :)
I try to just use a add.method to add a button to a frame. But only the frame pops up.
I don't see any buttons.
import javax.swing.*;
public class okd {
public static void main() {
JFrame frame = new JFrame();
JButton b1 = new JButton();
frame.setSize(500,500);
frame.add(b1);
b1.setSize(400,400);
b1.setVisible(true);
frame.setVisible(true);
}
}
There is a button there. Add some text to it and it will magically appear.
public static void main(String[] args){
JFrame frame = new JFrame();
JButton b1 = new JButton();
frame.setSize(500,500);
b1.setSize(400,400);
b1.setVisible(true);
b1.setText("HelloWorld");
frame.add(b1);
frame.setVisible(true);
}//SSCCE1
Your button has been added to the frame. You'll notice a difference if you remove your frame.add() line. The 'problem' lies with the following.
You haven't specified a layout resulting in your frame using the
default BorderLayout manager.
You haven't specified a constraint in frame.add(). Because of this the
component has been added to whatever the default position is for the
layout which is BorderLayout.CENTER. Components added to the center
take up the much space as possible hence why your button is filling the entire frame.
Here's some tutorials on layout managers. You might want to have a read through these at some point.
To remove the Large appearance of the button, You need to add a layout Manager to the Code
Like this:
import javax.swing.*;
import java.awt.*;
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton b1 = new JButton();
frame.setSize(500,500);
b1.setVisible(true);
b1.setText("HelloWorld");
frame.setLayout(new FlowLayout());
frame.add(b1);
frame.setVisible(true);
}
I have the following code. Class KochSnowflakesMenu is a grid JPanel with three buttons. Class KochSnowflakesDraw currently draws a circle using drawOval:
import javax.swing.*;
import java.awt.*;
public class KochSnowflakes
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Koch Snowflakes");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0,0, 600, 425);
frame.setBackground(Color.BLACK);
frame.setResizable(false);
frame.setLayout(null);
// Create the button interface
frame.add(new KochSnowflakesMenu());
frame.add(new KochSnowflakesDraw());
frame.repaint();
}
}
This works if I comment out frame.setResizable(false). When I don't the buttons don't appear. Why is that? As you can see, I have tried using repaint(). I had previously the problem that the buttons would not show up until I manually resized the window...
Also, as a bonus question, if anyone can tell me how to get the dimensions of a JPanel that would be great. The reason why I can't use a resizable layout manager such as BorderLayout, which really is what I want to use, is that I can't figure out the dimension of the JPanel that occupies the center (and hence have no idea how to large to draw the things I'm drawing).
EDIT:
As requested, here is the KockSnowflakesMenu class:
import javax.swing.*;
import java.awt.*;
public class KochSnowflakesMenu extends JPanel
{
public KochSnowflakesMenu()
{
setLayout(new GridLayout(3,1));
setBounds(0,0,200,400);
JButton button_red = new JButton("Red");
JButton button_yellow = new JButton("Yellow");
JButton button_blue = new JButton("Blue");
add(button_red);
add(button_yellow);
add(button_blue);
}
}
And, just to be sure I didn't mess something up with KochSnowflakesDraw, here's that class as well:
import javax.swing.*;
import java.awt.*;
public class KochSnowflakesDraw extends JPanel
{
public KochSnowflakesDraw()
{
setLayout(null);
setBounds(200, 0, 400, 400);
}
public void paintComponent(Graphics g)
{
g.setColor(Color.RED);
g.drawOval(0,0,400, 400);
}
}
A general point, when using JFrame, you should be using the contentPane, rather than the JFrame itself, so to add items, try
frame.getContentPane().add(.....);
For your first question, try using pack on your JFrame.
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Window.html#pack()
For your bonus question, JComponent has a getWidth and getHeight method. This will tell you the current size of the JPanel.
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JComponent.html#getWidth()
Adding more info to the previous answers...
The size is random until the component is drawn so make sure you have a setVisible(true) on your frame. Here's your code w/some modifications that let you use the BorderLayout and get the size of your drawing panel. I substituted some fake buttons for your interfaces but you'll get the drift.
JFrame frame = new JFrame("Koch Snowflakes");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0,0, 600, 425);
frame.setBackground(Color.BLACK);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
JPanel buttons = new JPanel(new FlowLayout());
buttons.add(new JButton("MENU"));
buttons.add(new JButton("DRAW"));
frame.add(buttons, BorderLayout.SOUTH);
JPanel drawArea = new JPanel();
drawArea.setBackground(Color.BLUE);
frame.add(drawArea, BorderLayout.CENTER);
frame.setVisible(true);
Dimension drawAreaDim = drawArea.getSize();
System.out.println(drawAreaDim);
When you add components to a visible frame the code should be something like:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes required
Make sure you create your Swing objects on the Event Dispatch Thread (EDT). In your example you aren't, and that is often the candidate when you get weird, inconsistent behavior. Swing isn't thread-safe, and relies on creation and modification of Swing objects on the EDT.
To remedy, just wrap the contents of your main method in a SwingUtilities.invokeLater call like so:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
JFrame frame = new JFrame("Koch Snowflakes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0,0, 600, 425);
frame.setBackground(Color.BLACK);
frame.setResizable(false);
frame.setLayout(null);
// Create the button interface
frame.add(new KochSnowflakesMenu());
frame.add(new KochSnowflakesDraw());
frame.setVisible(true);
}
});
}
That will create your JFrame and other components on the EDT thread. Does that fix the inconsistent "it doesn't work most of the time" behavior?
Also, I prefer to call setVisible last...though it probably doesn't matter.