I have the next problem: I'm calling a class from my Main, that have to show a JFrame. I can't even continue with my program because when I try to run it, the JFrame doesn't show.
I'm using Eclipse.
Main:
package System;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.Menu;
import java.awt.*;
import javax.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
new Menu();
}
}
Second class:
package System;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.*;
import javax.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Menu {
private JFrame ventana = new JFrame("Sistema de Productos QuĂmicos");
private JButton sup = new JButton("Supervisor");
private JButton oper = new JButton("Operario");
Menu()
{
ventana.setSize(500,500);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.add(sup);
ventana.add(oper);
ventana.setVisible(true);
}
}
Thanks!
because your jframe class name is Menu and you have import
import java.awt.Menu;
this create a new awt menu instead of your Menu class which create a jframe
new Menu();
to fix this change the name of the Menu class to something different.
for instance
public class MyMenu { //
If you play around with System.out.println("Say something random"), which is something to do when your program is not working and it is not syntax error, you find out that the Main class is not even calling the Menu class, so that "cuts down" your area of search on what's srong, I have not found the problem, and will update the answer when I do.
Related
I've been fiddling with TabbedPanes, and I wish to be able to use a method in another class to set which tab is selected.
However, when attempting to do this, I am provided with two errors.
First, it tells me that the parameter 'name' that I have provided the method in the second class has 'private access in Component'
error: name has private access in Component
TabbedPanes.name.setSelectedIndex(0);
Second, it tells me that it cannot find the symbol for the method I wish to call from within the second class' custom method.
error: cannot find symbol
TabbedPanes.name.setSelectedIndex(0);
^
symbol: method setSelectedIndex(int)
location: variable name of type String
My first class, TabbedPanes, can be found below:
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class TabbedPanes extends JFrame implements ActionListener
{
JTabbedPane tabs;
JPanel pan1, pan2, pan3, pan4, pan5, pan6, pan7, pan8;
JFrame frame;
JScrollPane scroll1, scroll2, scroll3, scroll4, scroll5, scroll6, scroll7, scroll8;
public void Tabs()
{
tabs = new JTabbedPane();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JScrollPane scroll1 = new JScrollPane(pan1);
scroll1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
pan2 = new JPanel();
pan3 = new JPanel();
pan4 = new JPanel();
pan5 = new JPanel();
pan6 = new JPanel();
pan7 = new JPanel();
pan8 = new JPanel();
JScrollPane scroll8 = new JScrollPane(pan8);
scroll8.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll8.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Container pane = frame.getContentPane();
pane.add(tabs);
tabs.add("Mon", scroll1);
tabs.add("Tue", pan2);
tabs.add("Wed", pan3);
tabs.add("Thu", pan4);
tabs.add("Fri", pan5);
tabs.add("Sat", pan6);
tabs.add("Sun", pan7);
tabs.add("Notes", scroll8);
Dates datesObject = new Dates();
datesObject.tabOnStartup("tabs");
frame.setSize(400,400);
frame.setVisible(true);
}
public static void main(String[] args)
{
TabbedPanes TrueTabs = new TabbedPanes();
TrueTabs.Tabs();
}
}
The second class, Dates, can be found below:
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class Dates
{
public void tabOnStartup(String name)
{
TabbedPanes.name.setSelectedIndex(0);
}
Any help will be greatly appreciated, and I hope my code is clean enough to read well.
If I got you right, you have to change the variable JTabbedPane tabs in class TabbedPanes to static:
static JTabbedPane tabs;
Also in class Dates you have to use
TabbedPanes.tabs.setSelectedIndex(0);
instead of
TabbedPanes.name.setSelectedIndex(0);
It seems that you have not implemented a method in class TabbedPanes :
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
If you are using IDE like Eclipse, NetBeans, etc. you should've received an error message like
- The type TabbedPanes must implement the inherited abstract method
ActionListener.actionPerformed(ActionEvent)
After these ammendments the result is:
I am trying to create an object of the class Color in awt to add it to the background of my JFrame ..but JCreator keep giving me this error message :
.java:23: error: cannot find symbol
getContentPane().setBackground(Color.color);
^
symbol: variable color
location: class Color
1 error
even when I am importing the Color class ... here is part of my code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import java.awt.event.*;
import javax.swing.BorderFactory;
import java.awt.Font;
import java.awt.Color;
public class myframe extends JFrame implements ActionListener
{
public myframe()
{
setLayout(null);
Color color = new Color(135,206,235);
// Color skyblue = new Color((float)135,(float)206,(float)235);
getContentPane().setBackground(Color.color);
ImageIcon icon = new ImageIcon("horizntal logo.gif");
setIconImage(icon.getImage());
Trying to Place Order nothing shows up on screen but I want to show a textarea saying:
Here's Sandwhich with the topping u provided
Can you help me?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public void setCheese(String s) {
cheese = s;
}
public String getCheese() {
return cheese;
}}
You should be getting a NullPointerException makeASandwih as sandwich is null, then you try and call an instance method
protected void makeASandwich() {
Sandwich sandwich = null;
sandwich.setCheese(jcbcheese.getItemAt(jcbcheese.getSelectedIndex()));
Try creating a new instance of Sandwich
protected void makeASandwich() {
Sandwich sandwich = new Sandwich();
sandwich.setCheese(jcbcheese.getItemAt(jcbcheese.getSelectedIndex()));
I made a sample code to start one project just refactoring another one.
This the refactored one:
package com.sh.st;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class Main extends JFrame implements ActionListener {
/**
*
*/
JMenuBar bar;
JMenu file, register;
JMenuItem close, search;
ImageIcon figure1= new ImageIcon("C:/Users/Victor/Downloads/Untitled.jpg");
//ImageIcon figure2= new ImageIcon("C:/Victor Rocha/carroicon.jpg");
JLabel Ibimagem1,Ibimagem2;
/**
*
*/
public Main()
{
bar= new JMenuBar();
file= new JMenu("file");
register= new JMenu("register");
register.setMnemonic(KeyEvent.VK_R);
file.setMnemonic(KeyEvent.VK_F);
close= new JMenuItem("Close");
close.addActionListener(this);
search= new JMenuItem("Search");
search.addActionListener(this);
Ibimagem1= new JLabel(figure1, JLabel.CENTER);
Ibimagem1.setVerticalTextPosition(SwingConstants.CENTER);
bar.add(file);
bar.add(register);
file.add(close);
register.add(search);
//register.add(carro);
//register.add(cliente);
//register.add(funcionario);
getContentPane().add(Ibimagem1);
setSize(800,600);
setTitle("SHST");
setJMenuBar(bar);
setDefaultCloseOperation(0);
//setIconImage(figure2.getImage());
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
addWindowListener(J);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==search){
Search s= new Search();
s.setVisible(true);
}
}
}
This is the original one:
package com.professordelphi.locadora;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class Principal extends JFrame implements ActionListener {
JMenuBar barra;
JMenu arquivo, cadastro;
JMenuItem fechar, cliente, funcionario, carro;
ImageIcon figura1= new ImageIcon("C:/Victor Rocha/carro.jpg");
ImageIcon figura2= new ImageIcon("C:/Victor Rocha/carroicon.jpg");
JLabel Ibimagem1,Ibimagem2;
public Principal()
{
barra= new JMenuBar();
arquivo= new JMenu("Arquivo");
cadastro= new JMenu("Cadastro");
cadastro.setMnemonic(KeyEvent.VK_C);
arquivo.setMnemonic(KeyEvent.VK_A);
fechar= new JMenuItem("Fechar");
fechar.addActionListener(this);
carro= new JMenuItem("Carro");
carro.addActionListener(this);
cliente= new JMenuItem("Cliente");
cliente.addActionListener(this);
funcionario= new JMenuItem("Funcionario");
funcionario.addActionListener(this);
Ibimagem1= new JLabel(figura1, JLabel.CENTER);
Ibimagem1.setVerticalTextPosition(SwingConstants.CENTER);
barra.add(arquivo);
barra.add(cadastro);
arquivo.add(fechar);
cadastro.add(carro);
cadastro.add(cliente);
cadastro.add(funcionario);
getContentPane().add(Ibimagem1);
setSize(800,600);
setTitle("Sistema de Cadastro");
setJMenuBar(barra);
setDefaultCloseOperation(0);
setIconImage(figura2.getImage());
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
addWindowListener(J);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==fechar){
System.exit(0);
}
if(e.getSource()==carro){
Carro k = new Carro();
k.setVisible(true);
}
if(e.getSource()==cliente){
Cliente c = new Cliente();
c.setVisible(true);
}
if(e.getSource()==funcionario){
Funcionario f= new Funcionario();
f.setVisible(true);
}
}
}
The thing is, the original e building and the refactored is not. The error I receive from the refactored is that "Selection does not contain a main type". I saw a lot of posts regarding this theme but none of them solve my problem. Here is one little list of the things I've tried;
Source: Editor does not contain a main type
clean your workspace and rebuild your Project.
make sure you add your source folder in the project properties -> java build path -> source.
close your project and reopen it.
Trying to run as a Java Application with Eclipse, anyone has suggestions of what should I do?
You dont have a main function defined in the class. The main function is the function that will be called when you run the file.
Try adding
public static void main(String [] args)
{
}
and create and show an object of your JFrame in the main method.
Right click on your project ->Properties -> Java Build Path -> Source -> Add Folder
Now Select the src folder and click on OK
You should define a main method in your class (either one) with the following signature:
public static void main(String args[])
This method is the start point of program.
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class Main extends JFrame implements ActionListener {
public static void main(String args[]){
// from here the program execution starts
}
....
your other code
.....
}
The entry point for Java programs is the main method.
Does your class contain main method like below ?
public static void main(String[] args) {
//Code
}
If you do not have this, your program will not run.
What I'm trying to do is simple. I have a JLayeredPane with two panels inside of it. Panel 2 (higher Z index) has a transparent background and only displays a Line2D element that goes from Y = 0 to Y = max. I need the X value to increment every so many milliseconds, and then redraw the line. I have everything set up to do so, except I can't figure out how to do the bar movement via timing.
I've done some research and every time I saw mentions of the timer class (Which I feel would be able to accomplish what I'm trying to do) people recommend not using it. I can't figure out an alternative to using the timer class in order to slide my bar across the screen.
Hope this code helps you. It does exactly what you want.
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Arrays;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class FloorPlaner extends JFrame {
public int x=0;
public FloorPlaner(){
super("FloorPlaner");
requestFocus();
setContentPane(new DrawingPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setResizable(true);
setVisible(true);
while (true) {
x++;
repaint();
try {
Thread.sleep(40); //25 FPS
} catch(InterruptedException bug) {
Thread.currentThread().interrupt();
System.out.println(bug);
}
}
}
class DrawingPane extends JPanel { //Where you actually draw on
public void paintComponent(Graphics g) { //Drawing method
g.drawLine(x,0,x,400);
}
}
public static void main(String args[]) {
new FloorPlaner(); //Start it
}
}