Java is complaining!
cannot find symbol
symbol : constructor Bar()
location: class Bar
JPanel panel = new Bar();
^
QUESTION: Why am I getting this error?...everything seems to be correct.
this is the coding:
public class JFrameWithPanel
{
public static void main(String[] args)
{
JPanel panel = new Bar();
}
}
Bar( ) is
public class Bar extends JPanel
{
public Bar(final JFrame frame)
{
super(new BorderLayout());
String[] tests = { "A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package" };
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5, 10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select")
{
{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
JOptionPane.showMessageDialog(frame, "IT WORKS!");
}
});
}
});
}
}
The problem is that you have a constructor that expects a JFrame:
public Bar(final JFrame frame)
but you care calling it with no arguments:
JPanel panel = new Bar();
You need to pass Bar an instance of a JFrame.
You are calling the Bar() constructor, but you do not have a no arguments constructor. You need to pass the JFrame argument.
In addition to TofuBeer answer - consider using an IDE like eclipse or netbeans (just to name two examples). Those IDE's will show errors of this kind already on typing the code.
Related
public void show()
// it just some part of my code
{
JFrame frame = new JFrame("Candles");
GridLayout layout = new GridLayout(1, 2);
JMenuBar menuBar=new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menu=new JMenu("Menu");
JMenu menu2=new JMenu("Edit");
menuBar.add(menu);
menuBar.add(menu2);
JMenuItem menuItem1=new JMenuItem("Open");
menu.add(menuItem1);
menuItem1.addActionListener(new FileMenuHandler(frame));
JMenuItem menuItem2=new JMenuItem("Quit");
menu.add(menuItem2);
menuItem2.addActionListener(new FileMenuHandler(frame));
JMenuItem menuItem=new JMenuItem("Search");
menu2.add(menuItem);
menuItem.addActionListener(new EditMenuHandler(frame));
frame.add(unsortedCandles);
frame.add(sortedCandles);
frame.setLayout(layout);
frame.setSize(400, 400);
frame.setLocation(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);`
//frame.pack();
// use pack here will make the window very small
frame.setVisible(true);
}
public class EditMenuHandler implements ActionListener{
JFrame jFrame;
float inputNumber;
public EditMenuHandler (JFrame jf) {
jFrame = jf;
}
}
public void actionPerformed(ActionEvent event) {
String menuName = event.getActionCommand();
if(menuName.equals("Search")) {
double inputPrice = Double.parseDouble(JOptionPane.showInputDialog("Enter:"));
JTextArea newArea = new JTextArea();
newArea.append(sortedCandles.toStringbyPrice(inputPrice));
// i have some problem in this part .It shows The method toStringbyPrice(double) is undefined for the type JTextArea。I dont know how to deal with.
remove(unsortedCandles);
remove(sortedCandles);
add(newArea);
pack();
}
}
}
I have two files/class, the main class and the menubar class.
This is my main class:
public static void main(String[] args){
journalFrame = new JFrame("Journal Viewer");
journalFrame.setJMenuBar(menuBar = new JMenuBar());
journalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
journalFrame.setPreferredSize(new Dimension(500, 800));
journalFrame.pack();
journalFrame.setLocationRelativeTo(null);
journalFrame.setVisible(true);
}
This is my menubar class:
MenuBar(){
mMbar = new JMenuBar();
fileMenu = new JMenu("File");
aboutMenu = new JMenu("About");
openItem = new JMenuItem("Open");
openItem.addActionListener((ActionListener) this);
fileMenu.add(openItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener((ActionListener) this);
fileMenu.add(exitItem);
mMbar.add(fileMenu);
}
My output is just a plain window with no menubar. How do I make it appear?
You are not creating an object of your class, you're simply creating a JMenuBar object when you call menuBar = new JMenuBar(). Also if this is your full code it's got mutliple issues and honestly I can't be bothered to figure out why I'm getting compiler errors on dots and semicolons. Note that setJMenuBar(JMenuBar menuBar) can only be called with a JMenuBar object as parameter. So either Override that method to have it behave the way you want, or try something else.
I will join #AndrewThompson in his opinion that it would make a lot more sense to just create the menubar within the GUI class.
Here's what your code could (or maybe should) look like:
public class Main
{
private static JFrame journalFrame;
private static JMenuBar menuBar;
public static void main(String[] args)
{
journalFrame = new JFrame("Journal Viewer");
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu aboutMenu = new JMenu("About");
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// TODO: Your open action
}
});
fileMenu.add(openItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// TODO: Your exit action
}
});
fileMenu.add(exitItem);
menuBar.add(fileMenu);
menuBar.add(aboutMenu);
journalFrame.setJMenuBar(menuBar);
journalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
journalFrame.setPreferredSize(new Dimension(500, 800));
journalFrame.pack();
journalFrame.setLocationRelativeTo(null);
journalFrame.setVisible(true);
}
}
I am trying to develop a Java app with more JPanels. For now, I have all JPanels in the main constructor Graphics(), and in there, I am adding them all to a JFrame. I want to break the code, and move each container into its own class, but, when I am running that code, it goes over and over again, never stops generating JFrames. Any ideas on how to break my code?
Here is the code:
public Graphics() {
//====== GENERAL=========//
frame = new JFrame();
frame.setSize(1300, 700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
//====== GENERAL=========//
//====== MENU BAR=========//
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 1284, 21);
JMenu menuFile = new JMenu("File");
menuBar.add(menuFile);
JMenuItem itemMain = new JMenuItem(new AbstractAction("Main"){
public void actionPerformed(ActionEvent e) {
mainPage.setVisible(true);
}
});
menuFile.add(itemMain);
JMenuItem itemLog = new JMenuItem(new AbstractAction("Log off"){
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
loginPage.setVisible(true);
mainPage.setVisible(false);
passField.setText("");
lblWrongPassword.setVisible(false);
}
});
menuFile.add(itemLog);
JMenu menuHelp = new JMenu("Help");
menuBar.add(menuHelp);
JMenuItem menuHelpInfo = new JMenuItem(new AbstractAction("Directions"){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, helpInfo);
}
});
menuHelp.add(menuHelpInfo);
frame.setJMenuBar(menuBar);
//====== MENU BAR=========//
//====== MAIN PAGE=========//
mainPage = new JPanel();
mainPage.setBounds(0, 0, 1284, 662);
frame.getContentPane().add(mainPage);
mainPage.setLayout(null);
I have a small program already started with multiple menu items.
When an item is clicked, I would like for something to show on the frame, and when another item is clicked, the first disappears and the second appears.
I can print to console, but cannot seem to print to frame.
Any suggestions would be appreciated:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class melco extends JFrame{
public static void main(String[] args){
JFrame frame = new JFrame("Salesman Resources");
frame.setVisible(true);
frame.setSize(1000,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu orders = new JMenu("Orders");
menubar.add(orders);
JMenuItem bookedorders = new JMenuItem("Booked Orders");
orders.add(bookedorders);
JMenuItem backorders = new JMenuItem("BackOrders");
orders.add(backorders);
JMenu customers = new JMenu("Customers");
menubar.add(customers);
JMenuItem customersales = new JMenuItem("Customer Sales");
customers.add(customersales);
JMenuItem customeritems = new JMenuItem("Customer Items");
customers.add(customeritems);
JMenuItem customerprices = new JMenuItem("Customer Prices");
customers.add(customerprices);
JMenuItem customerlistings = new JMenuItem("Customer Listings");
customers.add(customerlistings);
JMenu inv = new JMenu("INV");
menubar.add(inv);
JMenuItem surplusinv = new JMenuItem("Surplus Inv");
inv.add(surplusinv);
JMenuItem stockinv = new JMenuItem("Stock Inv");
inv.add(stockinv);
JMenu search = new JMenu("Searh");
menubar.add(search);
JMenuItem itemsearch = new JMenuItem("Item Search");
search.add(itemsearch);
JMenuItem customersearch = new JMenuItem("Customer Search");
search.add(customersearch);
JMenu menulostsales = new JMenu("Lost Sales");
menubar.add(menulostsales);
JMenuItem lostsales = new JMenuItem("Lost Sales");
menulostsales.add(lostsales);
JMenu menumarginadvisor = new JMenu("Margin Advisor");
menubar.add(menumarginadvisor);
JMenuItem marginadvisor = new JMenuItem("Margin Advisor");
menumarginadvisor.add(marginadvisor);
JMenu menumakeandhold = new JMenu("Make and Hold");
menubar.add(menumakeandhold);
JMenuItem makeandhold = new JMenuItem("Make and Hold");
menumakeandhold.add(makeandhold);
makeandhold.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Make and Hold is pressed");
System.out.println("Hello World");
// This is Make and Hold Area
}
}
);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e){
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
}
You cannot print to frame because a frame doesn't have a print method. If you want to add the text to the frame directly you should draw it. That requires to override paint method by the technique below
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class melco extends JFrame{
String str;
int x = 100, y = 100;
public melco(String title){
super(title);
}
void print(String s){
str = s;
repaint();
}
public void paint(Graphics g){
super.paint(g);
if (str != null)
g.drawString(str, x, y);
}
public static void main(String[] args){
final melco frame = new melco("Salesman Resources");
frame.setVisible(true);
frame.setSize(1000,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu orders = new JMenu("Orders");
menubar.add(orders);
JMenuItem bookedorders = new JMenuItem("Booked Orders");
orders.add(bookedorders);
JMenuItem backorders = new JMenuItem("BackOrders");
orders.add(backorders);
JMenu customers = new JMenu("Customers");
menubar.add(customers);
JMenuItem customersales = new JMenuItem("Customer Sales");
customers.add(customersales);
JMenuItem customeritems = new JMenuItem("Customer Items");
customers.add(customeritems);
JMenuItem customerprices = new JMenuItem("Customer Prices");
customers.add(customerprices);
JMenuItem customerlistings = new JMenuItem("Customer Listings");
customers.add(customerlistings);
JMenu inv = new JMenu("INV");
menubar.add(inv);
JMenuItem surplusinv = new JMenuItem("Surplus Inv");
inv.add(surplusinv);
JMenuItem stockinv = new JMenuItem("Stock Inv");
inv.add(stockinv);
JMenu search = new JMenu("Searh");
menubar.add(search);
JMenuItem itemsearch = new JMenuItem("Item Search");
search.add(itemsearch);
JMenuItem customersearch = new JMenuItem("Customer Search");
search.add(customersearch);
JMenu menulostsales = new JMenu("Lost Sales");
menubar.add(menulostsales);
JMenuItem lostsales = new JMenuItem("Lost Sales");
menulostsales.add(lostsales);
JMenu menumarginadvisor = new JMenu("Margin Advisor");
menubar.add(menumarginadvisor);
JMenuItem marginadvisor = new JMenuItem("Margin Advisor");
menumarginadvisor.add(marginadvisor);
JMenu menumakeandhold = new JMenu("Make and Hold");
menubar.add(menumakeandhold);
JMenuItem makeandhold = new JMenuItem("Make and Hold");
menumakeandhold.add(makeandhold);
makeandhold.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Make and Hold is pressed");
frame.print("Make and Hold is pressed");
System.out.println("Hello World");
frame.print("Hello World");
// This is Make and Hold Area
}
}
);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e){
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
}
I can print to console, but cannot seem to print to frame.
What do you mean exactly?
If you want to set a title on the Frame you can use setTitle:
frame.setTitle("a title");
Otherwise, if you want to add some text you have at least to add some component to your JFrame.
Have a look at JLabel:
JLabel label = new JLabel();
frame.getContentPane().add(label);
when you need to change the text:
label.setText("some text");
Don't ever use the EDT (Event Dispatcher Thread) for other things apart from letting it handle the GUI. main() method in Java Gui exits after scheduling the construction of GUI to the Event Dispatcher Thread. So its the EDT which handles it.
Eg:
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
myframe.setVisible(true);
}
}
}
You need something like JLable for adding things like Title.
You can add a JPanel to the JFrame and then add a JTextField to it.
You don't print to a frame. Maybe you want to add a JTextArea and append data to it?
Okay, so you need something to display on the frame, something like a JLabel??
frame.setLayout(new BorderLayout());
JLabel lblMessage = new JLabel(); // You'll probably need to declare this as final
frame.add(lblMessage);
Then in your action listener could do something like
public void actionPerformed (ActionEvent e){
label.setText("This is a message from the menu item");
}
Why don't you add a MouseListener on the JMenuItems? You can hold the most recent "message" reference as a member variable and remove it from the JFrame when another MouseListener is fired.
Im not sure how to reference to JPanel when it was declared like this.
This is the coding of the entire program: Everything works but the layout is not how I want it. adding BorderLayouts to it doesnt seem to work.
class FrameDemo
{
public static void main(String[] args)
{
final JFrame frame = new JFrame("CIT Test Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(350, 250));
frame.add(new JPanel()
{{
String[] tests = {"A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package"};
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5,10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select")
{{
addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e )
{
frame.dispose();
JOptionPane.showMessageDialog(frame,"IT WORKS!");
}
});
}});
}});
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
Not sure how to reference to JPanel to use BorderLayout.
How would I go about doing this?
If you add a panel to a JFrame (using add() as you are doing here) , you can assume that it is being added as the contentPanel. However, it is much better to be explicit. Instead of this:
frame.add(new JPanel()
{}
use this:
JPanel panel = new JPanel(new BorderLayout());
// add your stuff to the panel;
frame.add(panel);
EDIT:
after looking at your edit, what is clear is that you are initializing an anonymous class. This is not generally bad practice, but here you are putting a lot of initialization code. The code you are putting in is in a double-braced block, which in essence puts it into a static initializer. It seems like a normal anonymous class, but it really isn't. With that much code, it deserves its own class. If you went so far as to code a new class, you should be fine. I would suggest that you then define it as an extension of JPanel and in your own constructor pass a new BorderLayout() to the super.
EDIT 2:
if you create a brand new file/class named Bar and you coded it like this:
public class Bar extends JPanel {
public Bar(final JFrame frame) {
String[] tests = { "A+ Certification", "Network+ Certification",
"Security+ Certification", "CIT Full Test Package" };
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5, 10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select") {
{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
JOptionPane.showMessageDialog(frame, "IT WORKS!");
}
});
}
});
}
All you would need to do to use it would be to call
JPanel panel = new Bar(frame);
however, the goal here is to use a BorderLayout, so I would suggest that you put this call in to start:
public Bar(final JFrame frame) {
super(new BorderLayout());
.... everything else
}
On top of all answers already given... Your program has fundamental flaw. All manipulation with Swing components has to be done on EDT thread. So your code should be slightly different
class FrameDemo
{
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable() {
void run() {
/// your code here
}
});
}
}
Otherwise what happens is unpredictable. You can read more about it at http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html
Adding BorderLayout to it doesn't seem to work.
The default layout of a new JPanel is FlowLayout, as can be seen by resizing the frame. To see the difference, replace
frame.add(new JPanel()
with
frame.add(new JPanel(new BorderLayout())
As #akf suggests, lengthy static initialization using double braces can be obscure.