I have a class that creates a frame.
public class GameDisplay{
....
public void createDisplay(){
frame=new JFrame(title);
canvas=new Canvas();
canvas.setPreferredSize(new Dimension(width,height));
canvas.setFocusable(false);
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
frame.add(canvas);
frame.pack();
}
public Canvas getCanvas(){
return this.canvas;
}
public JFrame getFrame(){
return frame;
}
If I have another class that would add Panels and Buttons to the frame, how can I add them?
I have tried:
GameDisplay g;
Container c;
c = g.getFrame().getContentPane();
But it returns NullPointer Error. Thus, I can't seem to add panels to it.
Attach your JFrame made in createDisplay() to a static variable. Then access that static variable from another class.
Like this
public static JFrame frame1;
Then in createDisplay()
GameDisplay.frame1 = frame;
In another class to get the content pane just do
c = GameDisplay.frame1.getContentPane();
Hope this helped!
Related
How can I make the inner size 500x500 pixel?
Or should I hardcode the 28px macOS top-bar for windows?
My simple code:
import javax.swing.JFrame;
public class Hello {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
Don't set the JFrame size. Use a JPanel and add that to the JFrame and set the size of the JPanel.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(500,500));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen.
frame.setVisible(true);
If you are extending JPanel it is best to set the size by overridding the following:
#Override
public Dimenison getPreferredSize() {
return new Dimension(500,500);
}
It is also considered best practice to do most layouts and especially painting inside JPanel(s) and not the JFrame.
I have to programm a game with the exact resolution of 128x128 but the Canvas dont want to match.
public class Window extends Canvas{
private static final long serialVersionUID = 1L;
private JFrame frame;
public Window(BufferedImage icon){
this.setMinimumSize(new Dimension(128, 128));
this.setMaximumSize(new Dimension(128, 128));
this.setPreferredSize(new Dimension(128, 128));
this.setSize(128, 128);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
if(icon != null){
frame.setIconImage(icon);
}
}}
The size of the Canvas (getWidth(), getHeight) is 134*128 instad of 128*128..
I would think getHeight would return the 134. That would. Be the title of the window. Is the Jframe obviously the bigger portion?
Don't call your class Window. There is an AWT component using that name so it is very confusing. Class names should be more descriptive.
Don't extend Canvas. When using Swing your would extend JComponent or JPanel for custom painting.
Don't create the frame in your class. The frame is not a property of the class and doesn't belong there.
I have to programm a game with the exact resolution of 128x128
Works fine for me on Windows.
Its definetly the getWidth(),
There is a minimum width of the frame. The frame must be able to paint all the buttons in the title bar and the left/right borders.
So I would guess that because you are using a BorderLayout, the frame is being resized to its minimum and then your custom class is resized based on the rules of the layout manager.
So if you want the preferred size of the panel to be respected, try a different layout manager on the frame. For example use a FlowLayout, it will respect the size of any component added to it.
Simple example:
import java.awt.*;
import javax.swing.*;
public class GamePanel extends Canvas
{
public GamePanel()
{
setBackground( Color.RED );
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(128, 128);
}
private static void createAndShowGUI()
{
GamePanel panel = new GamePanel();
JFrame frame = new JFrame("GamePanel");
frame.setLayout( new FlowLayout() );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
System.out.println(panel.getSize());
System.out.println(frame.getSize());
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
I know this question has been asked a lot, but ive read through about 10 different articles, all reccomending to different things such as "frame = this" nad frame.add(d)" Im not sure why, but none of these have been working. I typed something and the program worked fine, except the Jbuttons wouldnt show up until i clicked on the JFrame a few times. After some tweaking of that code, im back to the start. Now i just get a error:
Exception in thread "main" java.lang.NullPointerException
at Guis.Dynamic_JFrame.<init>(Dynamic_JFrame.java:37)
at Guis.Dynamic_JFrame.main(Dynamic_JFrame.java:46)
Heres my code:
public class Dynamic_JFrame extends JFrame{
static JFrame frame;
Graphics g;
Handler handler = new Handler();
JButton red = new JButton();
JButton green = new JButton();
JButton orange = new JButton();
public Dynamic_JFrame(){
red.setText("RED");
green.setText("GREEN");
orange.setText("orange");
add(green);
add(red);
add(orange);
red.addActionListener(handler);
green.addActionListener(handler);
orange.addActionListener(handler);
frame.setVisible(true);
}
public static void main(String[] args){
Dynamic_JFrame d = new Dynamic_JFrame();
frame = new JFrame("Changing colors");
frame.setPreferredSize(new Dimension(500,500));
frame.setMaximumSize(new Dimension(500,500));
frame.setMinimumSize(new Dimension(500,500));
frame.setLayout(new FlowLayout());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class Handler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==red){
getContentPane().setBackground(Color.RED);
}
if(e.getSource()==green){
getContentPane().setBackground(Color.GREEN);
}
if(e.getSource()==orange){
getContentPane().setBackground(Color.ORANGE);
}
}
}
}
New code, Minor Changes. Program works as intended except for the buttons not updating until i click where they should be:
JFrame frame;
public Dynamic_JFrame(){
frame = new JFrame();
frame = this;
red.setText("RED");
green.setText("GREEN");
frame.add(green);
frame.add(red);
frame.setVisible(true);
}
public static void main(String[] args){
Dynamic_JFrame d = new Dynamic_JFrame();
d.frame.setPreferredSize(new Dimension(500,500));
d.frame.setMaximumSize(new Dimension(500,500));
d.frame.setMinimumSize(new Dimension(500,500));
d.frame.setLocationRelativeTo(null);
d.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.frame.setLayout(new FlowLayout());
}
A number of things...
Firstly, Dynamic_JFrame extends from JFrame so I don't know why you've then gone and create another frame...
Secondly, when Dynamic_JFrame calls frame.setVisible in the constructor, frame is null as it has not being initialised.
From my perspective, the simplest solution would be to extend Dynamic_JFrame from something like JPanel instead and simply add it to an instance of JFrame
For example...
public class Dynamic_JFrame extends JPanel {
static JFrame frame;
// Not sure that this is a good idea...
Graphics g;
//...
public Dynamic_JFrame(){
// Don't use this...
//frame.setVisible(true);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
Dynamic_JFrame d = new Dynamic_JFrame();
frame = new JFrame("Changing colors");
frame.setLayout(new FlowLayout());
frame.add(d);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
public class Benim extends JFrame {
Container contentArea = getContentPane ();
public Benim(){
JFrame frame=new JFrame("Concentration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
setSize(800, 800);
JButton start=new JButton("Start");
JPanel pane=new JPanel();
pane.add(start);
setVisible(true);
frame.add(start);
frame.add(pane);
/* setContentPane(Container)
JRootPane createRootPane()*/
}
public static void main (String []args){
new Benim();
}
}
My code is that. I tried adding to panel first then adding panel to frame, adding to frame directly. Adding a rootpane but still my button doesnot appear. I am trying to learn for 2 days but i am still at same point.
The instance of JFrame that is shown does not have the JButton added.
Instead invoke setVisible on the JFrame directly
You almost never want to extend JFrame as no new functionality is added
Other points to note
Call setVisible after components have been added
setSize is unnecessary - let pack determine container size
This is the result
public class Benim extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Concentration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton start = new JButton("Start");
JPanel pane = new JPanel();
pane.add(start);
pane.add(start);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
});
}
}
Why another instance of JFrame? You are extending it, so just call super().
public class Benim extends JFrame {
Container contentArea = getContentPane ();
public Benim(){
super("Concentration");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(800, 800);
JButton start=new JButton("Start");
JPanel pane=new JPanel();
pane.add(start);
add(pane);
setVisible(true);
}
public static void main (String []args){
new Benim();
}
}
Reimeus also rightfully points out that you don't need to extend JFrame if you don't plan on extending functionality. See his example for an alternative implementation.
I dont know how to init the JFrame windows. What I need to write to init im?
I have created at the main this:
Panel Panel=new Panel();
Panel.init();
JFrame frame = new JFrame("Shape Project");
frame.add(Panel);
frame.setResizable(false);
frame.setSize(new Dimension(1200, 650));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
and in the JPanel class I have write this:
public class Panel extends JFrame
{
public void init()
{
}
}
But when I active the frame it's does not active. What I need to write at the init func that the windows will open?
Try pack(); method of JFrame. If you are planning to develop with Swing, I recommend you to follow this tutorial:
http://download.oracle.com/javase/tutorial/uiswing/index.html
You already have a JFrame (frame). so now you should add components for your panel (you may do it in the main class as well). Such components are JTextField, JButton, etc. (and even another JPanel) each component you can add to the panel using panel.add(component_name); it is also recommended to follow the tutorial as Erkan mentioned.
Your panel class should extend JPanel, not JFrame.
You can add components to JPanel such as JButton, JList, etc
This is an example code you need to init a JFrame if you don't create own classes:
public class LogMain
{
public static void main(String[] args)
{
JFrame window = new JFrame("Log");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
window.setResizable(false);
JPanel panel = new JPanel();
JButton openFile = new JButton("Btn1");
JButton openDir = new JButton("Btn2");
panel.add(openFile);
panel.add(openDir);
window.add(panel);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
this is my example about the init of JFrame :
public class Windows{
public static void main(String args[]){
SJFrame window = new SJFrame("NEWNEWNEW");
window.init();
}
}
public class SJFrame extends JFrame(){
public SJFrame(String s){
super(s);
}
void init(){
Container panel = this.getContentPane();
panel.setBackground(Color.green);
panel.setLayout(new GridLayout(5,1));
JLabel jl1 = new JLabel("UserName");
JLabel jl2 = new JLabel("PassWord");
this.add(jl1);
this.add(jl2);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) ;
this.setSize(300,100);
this.pack();
this.setVisible(true);
}
}