I am trying code a JPanel in a class (classA) and trying to instantiate it from a class (classB) (where the main method is).
But when I try to code the classA the content assist does not supports. It does not resolves panel related codes, shows syntax error.
What could the problem be?
import javax.swing.*;
import java.awt.*;
public class gui1 {
JFrame j = new JFrame("MY Menu");
j.setSize(900, 700);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setResizable(false);
}
You cannot call methods outside of a method / static initialization block. Try initializing it in the constructor:
public class Gui1 {
JFrame my_frame;
public Gui1()
{
my_frame = new JFrame("MY Menu");
my_frame.setSize(900, 700);
my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
my_frame.setResizable(false);
}
}
Related
today i try to do a example of a "Window" on Java. I try to Concat the Title but my "GetTitle()" don't work! Anyone can help me with this?
And why "public class MiVentana extends JFrame {" and "MiVentana Frame = new MiVentana("Titulo");" says warning?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MiVentana extends JFrame {
public MiVentana (String Titulo){
this.setTitle(Titulo);
this.setSize(300,400);
this.setLocation(160,80);
this.setLayout(new FlowLayout());
this.ConfigurarVentana();
this.setVisible(true);
}
public void ConfigurarVentana(){
JPanel panel = new JPanel();
JButton boton = new JButton ("OK");
boton.addActionListener(new EscuchadorBoton());
panel.add(boton);
this.add(panel);
}
class EscuchadorBoton implements ActionListener{
public void actionPerformed(ActionEvent a){
this.setTitle(this.getTitle().concat(this.getTitle()));
}
}
public static void main(String[] args) {
MiVentana Frame = new MiVentana("Titulo");
//frameTest.setVisible(true);
}
}
EDIT: I'm working on Ubuntu 14.04 IDE Eclipse 3.8
Using this inside the ActionListener refers to the EscuchadorBoton listener, not the instance of MiVentana - your JFrame.
Using MiVentana.this should refer to the window, not the listener and you'll be able to get and set the title with that.
This post describes what is happening a bit better - basically you want this from the enclosing class, not the enclosed class.
Basically instead of doing this:
this.setTitle(this.getTitle().concat(this.getTitle()));
You need to do this:
MiVentana.this.setTitle(MiVentana.this.getTitle().concat(MiVentana.this.getTitle()));
I have a program which displays two buttons and changes the image of one of the buttons on roll over. I am getting an error on my
press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
part, And it looks like this: The method setDefaultCloseOperation(int) is undefined for the type ButtonClass. Even with the exit on close commented out there are more errors, please help.
Main class (with error):
package Buttons;
import javax.swing.JFrame;
public class Main_buttons{
public static void main(String[] args) {
ButtonClass press = new ButtonClass();
press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
press.setSize(300,200);
press.setVisible(true);
}
}
ButtonClass class:
package Buttons;
import java.awt.FlowLayout; //layout proper
import java.awt.event.ActionListener; //Waits for users action
import java.awt.event.ActionEvent; //Users action
import javax.swing.JFrame; //Window
import javax.swing.JButton; //BUTTON!!!
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane; //Standard dialogue box
public class ButtonClass extends JButton {
private JButton regular;
private JButton custom;
public ButtonClass() { // Constructor
super("The title"); // Title
setLayout(new FlowLayout()); // Default layout
regular = new JButton("Regular Button");
add(regular);
Icon b = new ImageIcon(getClass().getResource("img.png"));
Icon x = new ImageIcon(getClass().getResource("swag.png"));
custom = new JButton("Custom", b);
custom.setRolloverIcon(x); //When you roll over the button that says custom the image will change from b to x
add(custom);
Handlerclass handler = new Handlerclass();
regular.addActionListener(handler);
custom.addActionListener(handler);
}
private class Handlerclass implements ActionListener { // This class is inside the other class
public void actionPerformed(ActionEvent eventvar) { // This will happen
// when button is
// clicked
JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand()));//Opens a new window with the name of the button
}
}
}
I have searched everywhere for this problem and found nothing. Please tell me how to resolve this issue about exiting my window.
Thanks!
You're a little confused as you're creating a class that extends JButton, and calling setVisible(true) on it as if it were a top-level window such as a JFrame or JDialog, and that doesn't make sense. Since it isn't a top-level window it also makes sense to not have a default close operation or understand what that means.
I suggest that you call this method only on top-level windows such as on a JFrame or JDialog and the like. As a side recommendation, I usually avoid setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); and instead more often do setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); which gives it a little more flexibility.
Edit: actually, just change your class to extends JFrame not extends JButton.
Make sure your image path to your resources is correct. For example:
that method is defined for JFrame, not JButton. You're calling it on an instance of a class that extends JButton
The JFrame.Exit_on_close must be used in a JFrame, and you are extending from JButton.
To set a JButton to close a JFrame its something like this.
public class MyClass extends JFrame implements ActionListener{
private JButton button = new JButton("Button");
private JPanel panel = new JPanel();
public static void main(String args[]) {
new MyClass();
}
public MyClass() {
setSize(300, 300);
button.addActionListener(this);
panel.add(button);
add(panel);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
this.dispose();
}
}
Suppose to pull up 3 different windows with different Labels and Strings in the frames. Below is a picture of the projected output
I have three classes.
P1Panel that extends JPanel
P1Frame that extends JFrame
P1Driver used for main
----------------------------------------------------------P1Panel class below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P1Panel extends JPanel
{
private String s0;
public P1Panel(String s0){
{
add(new P1Panel(s0));
}
}
}
---------------------------------------------This is P1Frame class below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P1Frame extends JFrame
{
private String s1;
public P1Frame (String s1){
this.s1 = s1;
{
add(new P1Panel(s1));
}
P1Frame p1 = new P1Frame(s1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1.setSize(300,200);
p1.setVisible(true);
}
}
----------------------------------------This is P1Driver class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P1Driver
{
public static void main(String [] args)
{
P1Frame p1 = new P1Frame("This is window 1");
//JFrame f2 = new JFrame("This is window 2");
//JFrame f3 = new JFrame("This is window 3");
p1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1.setSize(300,200);
p1.setVisible(true);
}
}
I believe my P1Panel class is correct in that I called a constructor and added it to itself. The text in my label is passing the P1Panel constructor as a parameter
My P1Frame class I am having difficult with. In the constructor I am wanting to make a P1Panel object and add it to the P1Frame. I think I need to pass a string into the P1Frame constructor as a parameter and then pass string to P1Panel?
I believe my Driver class is correct too as I am just putting main here and setting items size and visibility.
I believe my fix is a small one, but I am stuck and unsure in how to do so. When I run the program as is, it runs infinite with nothing popping up.
You have an infinitely recursive constructor in P1Panel class and that is why your program gets stuck. Just remove the line
add(new P1Panel(s0));
when you execute this line in the constructor you are essentially calling it again by doing
new P1Panel(s0)
so the constructor never returns and it keeps calling itself recursively. Plus why are you not adding any Swing components to your P1Panel? It will be empty without Swing components. If you want to display a String inside the panel, I suggest you do the below in the constructor.
setLayout(new BorderLayout());
JLabel label = new JLabel(s0);
add(label, BorderLayout.CENTER);
I bought a new book called "learning Java" and I am struggling with the following problem.
It asked me the type the code below, however now it says we are going to replace the JLabel with our own graphical class. HelloCompoent is the new graphical class that he wants me to create and it should display Hello Java.
import javax.swing.*;
public class Helloworld {
public static void main(String[]args){
JFrame frame = new JFrame ("Hello, Java");
JLabel label = new JLabel("Hello world", JLabel.CENTER);
frame.add(label);
frame.setSize(300,300);
frame.setVisible(true);
}
}
I tried
import javax.swing.*;
import java.awt.*;
public class Helloworld {
public static void main(String[]args){
JFrame frame = new JFrame ();
class HelloComponent extends JComponent{
public void paintComponent (Graphics g){
g.drawString("Hello, Java", 123, 95);
frame.add(new HelloComponent());
}
}
}
}
After declaring your class you need to create an instance of it and add it to the frame:
HelloComponent helloComponent = new HelloComponent( );
frame.add(helloComponent);
frame.setSize(300,300);
frame.setVisible(true);
and you probably should remove the
frame.add(new HelloComponent());
from the paintComponent-method.
The paintComponent will only be called if an instance of the class is added to the component hierarchy of a visibile frame. So you have to add it to the frame before calling the paintComponent. Therefore you can remove it from that method.
I would like to create a simple GUI in Java. I know the basics of creating JLabel, etc. However, I cannot find why my JLabel is not displayed on the screen. Here is my code:
package test;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class A1Panel extends JPanel implements ActionListener {
JLabel firstInt;
public void init() {
makeComponents();
makeLayout();
}
private void makeComponents() {
firstInt = new JLabel("First argument");
firstInt.setFont(new Font("Helvetica", Font.BOLD, 16));
firstInt.setBackground(Color.lightGray);
firstInt.setVisible(true);
firstInt.setHorizontalAlignment(SwingConstants.CENTER);
}
private void makeLayout() {
add(firstInt);
}
public void actionPerformed(ActionEvent e) {
}
}
I then add my JPanel to my JFrame using a different class called GUI:
import test.A1Panel;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame("Testing GUI");
frame.setLayout( new GridLayout(1,3));
JPanel panel = new A1Panel();
panel.setBorder( BorderFactory.createRaisedBevelBorder() );
frame.add( panel);
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
}
When I hit compile, what I get is a simple frame with three empty panels. I do not understand why my JLabel is not in the first panel since I have added it to my frame. Am I missing something?
The frame is not empty, the panel is. Nowhere in your code do I see a call to the methods init() or makeComponents(). In fact, I would turn your init() method into a constructor, like so:
public A1Panel() {
makeComponents();
makeLayout();
}
Another alternative to this would be to call panel.init() after declaring JPanel panel = new A1Panel()
After you instance A1Panel, you haven't called A1Panel.init()
I would suggest removing init() and adding all the code to the constructor of A1Panel. If, however, you wanted to keep the init() function, you would want to call it after JPanel panel = new A1Panel()
The code to add the label was not actually called in the main, was it? So look carefully, when is init actually called?
Look at the
private void makeLayout()
method.
If I replace public void init() by A1Panel(), it does the job. Thank you for your help.