the code down here only calls the class(class "oefen") whith the code that makes a jframe and a ball(using the painComponent method)and if you press the arrow keys it moves in that direction...it works fine but when i add the target(jlabel) to the jframe only the target shows and nothing else..please help me to put the target and ball in the same frame
public class theFrame implements KeyListener {
public static JFrame j = new JFrame();
public static ImageIcon tar = new ImageIcon("c://fruit//target.png");
public static JLabel target = new JLabel(tar);
public static JPanel p = new JPanel();
public static void main(String args[]){
j.setSize(500,600);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
oefen o = new oefen();
j.add(o);
target.setLayout(null);
target.setSize(100,100);
target.setLocation(250,0);
j.add(target);
}
}
thanks
public class theFrame extends JComponent {
public static JFrame j = new JFrame();
public static ImageIcon tar = new ImageIcon("c://fruit//target.png");
public static JLabel target = new JLabel(tar);
public static JPanel p = new JPanel();
public static oefen o = new oefen();
public static void main(String args[]){
j.setSize(500,600);
j.setLayout(null);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
target.setLayout(null);
target.setSize(0,0);
target.setLocation(0,0);
j.add(o);
j.add(target);
}
}
if i set the JFrame Layout to null notihng shows...the code above shows how i set the JFrameLayout cuz i don't know if its wrong.
please help
try setting the JFrames layout to null too, because you use absolute positioning.
Related
Does anyone know why my image is not loading? I tried many things and so far nothing, first of all the folder "IMGFiles" is already like Source Folder.
package Main;
import javax.swing.*;
public class Menu extends JFrame {
public Menu()
{
ImageIcon imagem = new ImageIcon(Menu.class.getResource("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png"));
JLabel logo = new JLabel();
logo.setIcon(imagem);
}
public static void main(String[] args) {
//new Menu();
JFrame janela = new JFrame();
janela.setSize(816, 419);
janela.setUndecorated(true);
janela.setVisible(true);
janela.setLocationRelativeTo(null);
}
}
I'm assuming you are using UNIX (for the shape of the path you are using).
Here is an approach for you:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import com.apple.eawt.Application;
public class Menu extends JFrame {
public Menu(){ }
public static void main(String[] args) {
Application.getApplication().setDockIconImage(new ImageIcon("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png").getImage());
//new Menu();
JFrame janela = new JFrame();
janela.setSize(816, 419);
janela.setUndecorated(true);
janela.setVisible(true);
janela.setLocationRelativeTo(null);
}
}
Output:
This happens because you prepare the JLabel inside Menu constructor and you do not initiate any instance of Menu class. Also, you do not add the JLabel into the frame (content pane).
Check this sample:
public class Menu extends JFrame {
public Menu()
{
ImageIcon imagem = new ImageIcon(Menu.class.getResource("/LiturgisGame/IMGFiles/LiturrgisLogoLoad.png"));
JLabel logo = new JLabel();
logo.setIcon(imagem);
setSize(816, 419);
setUndecorated(true);
setLocationRelativeTo(null);
getContentPane().add(logo); //Add the label to the content pane
}
public static void main(String[] args) {
SwingUtilities.invokeLater(()->{
new Menu().setVisible(true);
});
}
}
I'm a beginner of Java and trying to figure out building a JScrollPane. I have following codes so far but it's having a problem calling JScrollPane. Please help. Thanks in advance.
public class DemoTest {
public String sTEXT = null;
public JTextArea jTEXTAREA = null;
public JScrollPane jPANE = null;
public JFrame jFRAME = null;
public static void main(String[] args) {
DemoTest demo = new DemoTest();
}
public DemoTest() {
setText();
setPane();
setFrame();
}
public void setFrame() {
JFrame jFRAME = new JFrame("Demo");
jFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFRAME.setSize(350, 300);
jFRAME.setLocationRelativeTo(null);
jFRAME.setVisible(true);
jFRAME.add(jPANE); // **THIS SEEMS TO BE THE PROBLEM**
}
public void setPane() {
JScrollPane jPANE = new JScrollPane(jTEXTAREA);
}
public void setText() {
JTextArea jTEXTAREA = new JTextArea();
jTEXTAREA.setText("Hello World!");
jTEXTAREA.setEditable(false);
}
}
When you attempted to create the various Swing Objects, you were creating new local variables instead of assigning to the member variables (class level). Note that if you have a member variable and local variable of the same name, you may assign to the member variable by using this.VAR_NAME = ...;. For a better understanding of variable scope, read this Scope of Variables in Java post which explains the basics well.
public class DemoTest {
public String sTEXT = null;
public JTextArea jTEXTAREA = null;
public JScrollPane jPANE = null;
public JFrame jFRAME = null;
public static void main(String[] args) {
DemoTest demo = new DemoTest();
}
public DemoTest(){
setText();
setPane();
setFrame();
}
public void setFrame(){
jFRAME = new JFrame ("Demo");
jFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFRAME.setSize(350,300);
jFRAME.setLocationRelativeTo(null);
jFRAME.setVisible(true);
jFRAME.add(jPANE);
}
public void setPane(){
jPANE = new JScrollPane(jTEXTAREA);
}
public void setText(){
jTEXTAREA= new JTextArea();
jTEXTAREA.setText("Hello World!");
jTEXTAREA.setEditable(false);
}
}
public class HandleUI {
public static void setUpUI(){
JPanel jPan = new JPanel();
FlowLayout flow = new FlowLayout();
jPan.setLayout(flow);
txtFld = new JTextField();
txtFld.setSize(550,5);
jPan.add(txtFld);
jPan.setSize(10,200);
MainClass.mainFrame.add(jPan);
int gapX = MainClass.mainFrame.getX()-(txtFld.getX()/2);
}
//Instance variables.
public static JTextField txtFld;
public JButton [] buttons;
}
public class MainClass {
public static void main (String [] args){
int frameX = Constants.FRAME_WIDTH;
int frameY = Constants.FRAME_HEIGHT;
mainFrame = new JFrame();
mainFrame.setSize(frameX,frameY);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
HandleUI.setUpUI();
}
//Instance variables
public static JFrame mainFrame;
}
It's supposed to show JTextField, but as you might have guessed - JFrame shows nothing. I didn't type in imports on purpose, but they are all there. I can't find the problem. Can anyone help?
1.) Simply write:
JTextField tField = new JTextField(10);
Here In the constructor you are passing the number of columns, which is sufficient for a layout like FlowLayout to set the size of the JTextField
2.) The line mainFrame.setVisible(true); must be the last line of the main method. You need to put the code at main() method, inside SwingUtilities.invokeLater(...) thingy.
3.) Instead of setting size on the JFrame use JFrame.pack(), to set the window to the preferred size.
4.) Creation of unnecessary static members is a design flaw. Try to keep yourself away from such thingies.
5.) Read a bit about Concurrency in Swing
One Example Program for help(Use the order of lines as specified in this answer):
import java.awt.*;
import javax.swing.*;
public class Example {
private void displayGUI() {
JFrame frame = new JFrame("Example Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
JTextField tField = new JTextField(10);
contentPane.add(tField);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
You have to call setVisible(true) on your JFrame AFTER having initialised your UI.
Simply pulling the following line:
HandleUI.setUpUI();
... right before:
mainFrame.setVisible(true);
... will do the trick.
As a side note, I'd like to point out that setting the size of your text field won't really work like you did. You probably would use setPreferredSize(Dimension) instead. Or, even better, organise your UI only using Layouts and not manually setting any component's size.
The following code produces a window with buttons, but an error message pops up when I run i and actally press the button. According to the Spring tooltip:
Cannot make a static reference to the non-static method setBackground(Color) from the type JComponent
This program is literally entered from my Java textbook line for line, as far as I can tell. It's an older book, so there might be incompatibility, but it doesn't seem likely.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String[] args)
{
final ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("Button Test");
setSize(Default_width, Default_height);
//panel
ButtonPanel panel = new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int Default_width = 300;
public static final int Default_height = 200;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction= new ColorAction(Color.YELLOW);
ColorAction redAction = new ColorAction(Color.RED);
ColorAction blueAction = new ColorAction(Color.BLUE);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
}
class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
ButtonPanel.setBackground(backgroundColor);
}
private Color backgroundColor;
}
One approach is to nest ColorAction as an inner class in ButtonPanel, where it has implicit access to the enclosing panel.
Addendum: As noted in comments by #Andrew Thompson and #nachokk, the implicit accessibility can be made explicit by qualifying this using the enclosing class name. See JLS ยง15.8.4. Qualified this for details. In this example, these two invocations are equivalent:
setBackground(backgroundColor);
ButtonPanel.this.setBackground(backgroundColor);
As an more general alternative, consider encapsulating the target panel and color in an Action, as outlined here.
class ButtonPanel extends JPanel {
public ButtonPanel() {
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction redAction = new ColorAction(Color.RED);
ColorAction blueAction = new ColorAction(Color.BLUE);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener {
public ColorAction(Color c) {
backgroundColor = c;
}
#Override
public void actionPerformed(ActionEvent event) {
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
ButtonPanel.setBackground()
is not a static method so you can't call it as one. You need a concrete instance of ButtonPanel to set the background.
ButtonPanel bp = new ButtonPanel();
bp.setBackground(backgroundColor);
Also change in look and feel can help:
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
I want to add an Image to the JFrame, the add( ) method is generating an error which says "cannot find symbol: method add(JLabel)" ... How can I fix that?
** I still haven't called the ImageLoading( ) method from the main method.
import javax.swing.*;
public class NetworkingGame {
NetworkingGame(){
JFrame jfrm = new JFrame("Angry Painters");
jfrm.setSize(800, 480);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
}
public void ImageLoading(){
ImageIcon i = new ImageIcon("angry-painters.jpg");
JLabel jl = new JLabel(i);
add(jl); //The error is in this line
}
public static void main(String[] args) throws Exception{
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
new NetworkingGame();
}
});
}
}
Visibility of JFrame jfrm is limited by constructor of NetworkingGame. So add method does not exist in NetworkingGame. Make your JFrame member of NetworkingGame.
public class NetworkingGame {
private JFrame jfrm;
NetworkingGame(){
jfrm = new JFrame("Angry Painters");
...
JLabel jl = new JLabel(i);
jfrm.add(jl);
Probably you should write
jfrm.add(jl);
and have jfrm as a member.
In you case you really calling this.add() which is not exist.
You are calling the add method as if it was an instance method in your class NetworkingGame and your class doesn't have any method, so you are getting the compiler error as the compiler can't find any method of such a name in your class NetworkingGame, so add it to the Jframe instead.
NetworkingGame(){
JFrame jfrm = new JFrame("Angry Painters");
jfrm.setSize(800, 480);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
ImageIcon i = new ImageIcon("angry-painters.jpg");
JLabel jl = new JLabel(i);
jfrm.add(jl);
}