I am coding on Android and using terminal IDE to compile my code. However, for some reason when I compile, it says the Button code is wrong.
package BlahBlahBlah;
import javax.swing.JButton;
import javax.swing.JFrame;
public class blahblahblah extends JFrame{
JFrame w = new JFrame();
w.setVisible(true);
w.setSize(1366, 768);
Button sb = new JButton();
sb.addListener(this);
add(sb);
}
It keeps saying illegal start of type or identifier expected which as you see there's a identifier in the Button.
Button sb = new JButton();
A "Button" without the "J" is not the same as a "JButton".
In Swing components start with a "J".
You should put your code inside a method.
public class blahblahblah extends JFrame{
public static void main(String[] args) {
JFrame w = new JFrame();
w.setVisible(true);
w.setSize(1366, 768);
Button sb = new JButton();
sb.addListener(this);
add(sb);
}
}
You can either remove sb.addListener(this); or implement our class with ActionListener and add it's umimplemented methods to your class. Also, do some changes like:
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class blahblahblah extends JFrame implements ActionListener
{
public blahblahblah()
{
JFrame w = new JFrame();
w.setVisible(true);
w.setSize(1366, 768);
JButton sb = new JButton();
sb.addActionListener(this);
add(sb);
}
public static void main(String[] args) {
blahblahblah b = new blahblahblah();
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Your Stuff
}
}
Related
I want to be able to call the Introduction.Intro() method into my main file code, but it tells me I am unable to call a non-static method intro from a static context. Since I am still fairly new to coding I'm not entirely sure what the problem is. I've added my codes down below. I've tried countless online methods but sadly none have seemed to work.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Start extends JFrame implements ActionListener
{
private JFrame Main;
private JPanel PanelA, PanelB, PanelC;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public Start ()
{
//Button
Button = new JButton("Start");
Button.addActionListener(new ButtonListener());
//Text
Text = new JLabel("Welcome To The Game"); //ADD NAME OF THE GAME
//Image
Image = new ImageIcon(getClass().getResource("download.jfif")); //ADD THE IMAGE FOR WELCOME
ImageL = new JLabel(Image);
//Top Panel (PanelA) - Image
PanelA = new JPanel();
PanelA.setBorder(BorderFactory.createEmptyBorder(0,200,150,200));
PanelA.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelA.add(ImageL);
//Middle Panel (PanelB) - Text
PanelB = new JPanel();
PanelB.setBorder(BorderFactory.createEmptyBorder(50,200,10,200));
PanelB.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelB.add(Text);
//Bottom Panel (PanelC) - Buttons
PanelC = new JPanel();
PanelC.setBorder(BorderFactory.createEmptyBorder(0,200,20,200));
PanelC.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelC.add(Button);
//Main Frame
Main = new JFrame ();
Main.add(PanelA, BorderLayout.NORTH);
Main.add(PanelB, BorderLayout.CENTER);
Main.add(PanelC, BorderLayout.SOUTH);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main.setTitle("GAME TITLE"); //ADD THIS LATER
Main.pack();
Main.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae)
{
}
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == Button)
{
Introduction.Intro1(); //THESE LINE RIGHT HERE
return null; //THESE LINE RIGHT HERE
}
}
}
public static void main(String[] args)
{
new Start();
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Introduction
{
private JFrame Main;
private JPanel PanelD;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public void Intro()
{
Image = new ImageIcon(getClass().getResource("guy.jfif"));
ImageL = new JLabel(Image);
PanelD = new JPanel();
PanelD.setBorder(BorderFactory.createEmptyBorder(0,100,10,100));
PanelD.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelD.add(ImageL);
PanelD.setVisible(true);
Main.add(PanelD, BorderLayout.NORTH);
}
}
EDIT: So I made another method in the Introduction class where I added this line of code, it managed to fix the error, however, the panel isn't being saved and my JFrame is outputting blank.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
If you are looking to initialize the Introduction class in main method of Start class, You can add belo code in main method after Start()
Introduction M = new Introduction();
You main method becomes :
public static void main(String[] args)
{
new Start();
Introduction M = new Introduction();
m.Intro
}
Looking at this set of code, It looks like there is incompatible issue, as you have declare JFrame as return type, while you are returning instance of Introduction.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
I am trying to create a piano program where you click the key and using an actionlistener is plays the note from the jfugue library. For some reason after about 18 clicks without changing anything the buttons stop working. I cut down the code to analyze why this might happen, thus only two notes.
Thanks in advance for any advice!
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Color;
import javax.swing.JLayeredPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.*;
import org.jfugue.*;
public class ChordPlayer2 extends JComponent{
public ChordPlayer2(){
final Player player = new Player();
JFrame frame = new JFrame();
JButton cButton, csharpButton;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(0, 0);
buttonPanel.setSize(1700, 1000);
csharpButton = new JButton("");
csharpButton.setLocation(100, 150);
csharpButton.setSize(100,520);
buttonPanel.add(csharpButton);
cButton = new JButton("");
cButton.setLocation(0, 150);
cButton.setSize(160, 800);
buttonPanel.add(cButton);
class cClicker implements ActionListener {
public void actionPerformed(ActionEvent event) {
player.play("C");
}
}
class csClicker implements ActionListener {
public void actionPerformed(ActionEvent event) {
player.play("C#");
}
}
ActionListener c = new cClicker();
cButton.addActionListener(c);
ActionListener cs = new csClicker();
csharpButton.addActionListener(cs);
buttonPanel.setOpaque(true);
//return buttonPanel;
frame.add(buttonPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1700, 1000);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
//JFrame.setDefaultLookAndFeelDecorated(true);
ChordPlayer2 demo = new ChordPlayer2();
}
}
This is a known bug in JFugue:
https://code.google.com/p/jfugue/issues/detail?id=49
The most recent version is claimed to fix this:
https://code.google.com/p/jfugue/downloads/detail?name=jfugue-4.1.0-20120125.jar&can=2&q=
it seems as though i have found a new problem in my code. my code has no (visible) errors, but i still get no frame. Please help
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class hello{
//Int's and things
static JButton Play = new JButton("<PLAY>");
static JFrame pane = new JFrame("CIrCUT 0.0.2");
static JLabel Title = new JLabel("CIrCUT");
static JLabel none = new JLabel(" ");
static JPanel panel = new JPanel(new GridLayout(10,10,10,10));
static JButton Options = new JButton("<OPTIONS>");
static JPanel panel2 = new JPanel(new GridLayout(10,10,10,10));
static String b[] = new String[3];
static int panelLoct =1;
JComboBox optionlist = new JComboBox();
void initialize(){
b[0] = "High";
b[1] = "Medium";
b[2] = "Low";
//title
pane.setTitle("CIrCUT 0.0.2");
//drop down
optionlist .setModel(new DefaultComboBoxModel(new String[] {"Option", "High", "Medium", "Low"}));
optionlist.setSelectedIndex(3);
optionlist.addActionListener((ActionListener) this);
//other pane-related things
if(panelLoct==1){
pane.setLayout(new GridLayout(10,10));
panel.setMaximumSize(new Dimension(500,500));
pane.setSize(500,500);
pane.setMaximumSize(new Dimension(500,500));
panel.add(Title);
panel.add(none);
panel.add(Play);
panel.add(Options);
panel2.add(optionlist);
Play.setSize(new Dimension(500,450));
pane.setLocation(500,50);
pane.setBackground(Color.lightGray);
pane.setContentPane(panel);
pane.pack();
pane.setMinimumSize(new Dimension(500,500));
pane.setContentPane(panel);
OptionButtonHandler cbHandler = new OptionButtonHandler();
Options.addActionListener(cbHandler);
pane.setVisible(true);
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
private static class OptionButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e){
pane.remove(panel);
pane.add(panel2);
}
}
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
cb.getSelectedItem();
}
public static void main(String args[])
{
hello a = new hello();
a.initialize();
}
}
any help at all would be much appreciated. here is the error i get when i try to run it
Exception in thread "main" java.lang.ClassCastException: hello cannot be cast to java.awt.event.ActionListener
at hello.initialize(hello.java:39)
at hello.main(hello.java:83)
It is probably a novice mistake, but could find nothing on how to solve it.
Your program does have a visible error, precisely where it's failing:
optionlist.addActionListener((ActionListener) this);
this is a reference to an instance of hello. The hello class doesn't implement ActionListener - so how would you expect that cast to succeed?
The only implementation of ActionListener you've given is OptionButtonHandler. Perhaps you meant:
optionlist.addActionListener(new OptionButtonHandler());
?
optionlist.addActionListener((ActionListener) this);
Just because you will something doesn't mean it is possible. In this case, hello is not an ActionListener, so this will fail.
If you find yourself casting, you likely have a problem.
You have to implement ActionListener.
public class Hello implements ActionListener{
}
I have created a simple VLCJ project that consists of a simple embedded player and a button to exit.
The code is as follows:
package test;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class Demo {
private final JFrame frame;
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
private JPanel videoPane;
private JPanel buttonPane;
private Button exitButton;
private ActionListener a;
private static String vlc_location = "C:\\Program Files\\VideoLAN\\VLC";
public static void main(String[] args) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlc_location);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().run();
}
});
}
public Demo() {
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
a = new MyActionListener();
exitButton = new Button("Exit");
exitButton.setActionCommand("Exit app");
exitButton.addActionListener(a);
buttonPane = new JPanel();
buttonPane.setLayout(new BorderLayout());
buttonPane.setBackground(Color.black);
buttonPane.add(exitButton, BorderLayout.CENTER);
videoPane = new JPanel();
videoPane.setLayout(new BorderLayout());
videoPane.setBackground(Color.black);
videoPane.add(mediaPlayerComponent, BorderLayout.CENTER);
videoPane.add(buttonPane, BorderLayout.PAGE_END);
frame = new JFrame("vlcj demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.setSize(1200, 800);
frame.setContentPane(videoPane);
frame.setVisible(true);
}
public void run() {
mediaPlayerComponent.getMediaPlayer().playMedia(video_file);
}
class MyActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
String s = arg0.getActionCommand();
if (s.equals("Exit")) {
System.exit(0);
}
}
}
}
The problem is that the button does show up but it cannot be clicked. When i removed the videoPane, it was back to clickable! Any ideas if I'm missing something?
I am using the version 2.1.0 for vlcj.
Thanks!
Thanks MadProgrammer for your advise. I went on to think about it and tried commenting away the line of code in run(). The JButton came back!
However, when i un-commented the code in run(), the JButton disappeared. I was thinking maybe the Swing runnable was causing issue with the creation of the JButton.
Hence, what i did was to comment away the whole Swing runnable and just use:
final Demo demo = new Demo();
demo.run();
The demo can now play video and display the Exit button, thanks!
Ok guys, I've made some changes in my code as told by you. I have 3 classes:
The second class (and first GUI): I have 4 JButtons - Simulare, CazParticular, Start and HandSelection, some JLabels and 3 JTextFields; When I press the HandSelection button another frame it's created with different content.
The 3rd class (and second GUI): I have 2 JButtons - Ok and Cancel and other stuff. When I press the Ok button I want to the access to the JTextField(QuesHandText) from the first Gui and use the method setText(). I can't figure this out, I'm thinking on this like 4-5 days and still can't get the answer. Please help me!
What code should I write in if statement to be able to modify the text in the JTextField from 2nd class (first GUI) ?
First class:
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
//other stuff
GuiMain gui = new GuiMain();
gui.frame1.setLocation(150,150);
gui.frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.frame1.setSize(400,250);
gui.frame1.setResizable(false);
gui.frame1.setVisible(true);
//other stuff
}
}
Second class:
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class GuiMain {
public static GuiMain instance;
public static GuiMain getInstance(){
if(GuiMain.instance == null){GuiMain.instance = new GuiMain();}
return GuiMain.instance;
}
public JFrame frame1 = new JFrame();
public JTextField QuesHandText, FlopTurnRiverText, RezultatText;
public JButton Simulare, CazParticular, Start, HandSelection;
public int w1,h1;
public JLabel someText;
static int u=0;
public int j=0;
public GuiMain(){
frame1.setTitle("HoldemTool");
frame1.setLayout(null);
QuesHandText = new JTextField(4);
Simulare = new JButton("Simulare");
CazParticular = new JButton("Caz particular");
Start = new JButton("Start");
HandSelection = new JButton(new ImageIcon(getClass().getResource("GuiPic.png")));
Handler handler1 = new Handler();
CazParticular.addActionListener(handler1);
Simulare.addActionListener(handler1);
HandSelection.addActionListener(handler1);
Start.addActionListener(handler1);
QuesHandText.setEditable(false);
FlopTurnRiverText.setEditable(false);
RezultatText.setEditable(false);
frame1.add(Welcome1);
frame1.add(Welcome2);
frame1.add(QuesHand);
frame1.add(FlopTurnRiver);
frame1.add(Rezultat);
frame1.add(QuesHandText);
frame1.add(FlopTurnRiverText);
frame1.add(RezultatText);
frame1.add(Simulare);
frame1.add(CazParticular);
frame1.add(Start);
}
public JTextField getQuesHandText(){
return QuesHandText;
}
public class Handler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==Simulare)
{
}
if(e.getSource()==CazParticular){
QuesHandText.setEditable(true);
FlopTurnRiverText.setEditable(true);
QuesHandText.setText("");
FlopTurnRiverText.setText("");
RezultatText.setText("");
frame1.setSize(470, 250);
Start.setBounds(3*FlopTurnRiverText.getX(), QuesHand.getY(), 65, h1);
HandSelection.setBounds(3*FlopTurnRiverText.getX(), FlopTurnRiverText.getY(), 65, h1);
frame1.add(HandSelection);
frame1.add(Start);
}
if(e.getSource()==Start){
QuesHandText.setText("Text");
}
if(e.getSource()==HandSelection){
GuiSelection gui2 = new GuiSelection();
gui2.frame2.setVisible(true);
}
}
}}
3rd class
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class GuiSelection extends GuiMain {
JFrame frame2 = new JFrame();
GuiMain guiMain;
public JButton Ok,Cancel;
//other stuff
public GuiSelection(){
guiMain = new GuiMain();
frame2.setTitle("Hand selection");
frame2.setSize(1135,535);
frame2.setLayout(null);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
frame2.setResizable(false);
//other stuff
Handler2 handler2 = new Handler2();
Ok.addActionListener(handler2);
Cancel.addActionListener(handler2);
frame2.add(Ok); frame2.add(Cancel);
}
public class Handler2 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==Cancel){
frame2.hide();
}
if(e.getSource()==Ok)
{
GuiMain.getInstance().getQuesHandText().setText("From Ok");
//When I prees this button "Ok" I want to get access to the JTextField(QuesHandText) in the GuiMain class, and .setText();
//somothing like QuesHandtText.setText("someText");
}
}
}
}
Add to your first GUI the method public JTextField getQuesHandText() and a static method public static JFrame getInstance() which returns the instance of the first GUI. Now you can call SecondClass.getInstance().getQuesHandText() from anywhere to get the JTextField instance. Note that with this method you can only have a single instance of SecondClass at any time.
Your getInstance() method will look as follows:
public class SecondClass extends JFrame {
private static SecondClass instance;
public static SecondClass getInstance() {
if(SecondClass.instance == null)
SecondClass.instance = new SecondClass();
return SecondClass.instance
}
}
Note that you shouldn't create an instance of SecondClass manually.
Use Composition,
1. Create the instance of the class which contains the JFrame, whose JTextField you need to access.
2. Then on that instance call the setter or getter method of the JTextField.
Edited:
Make Sure you have implemented Singleton principle on the Main class, else you will get
a new instance , which you dont want......
In Second class.
public class GuiMain{
Main m = new Main();
m.getText();
m.setText();
// Other stuffs
}
Perhaps you do not really need two Windows. What you need is a Dialog which can be achieved by the class JOptionPane.
Here is a demo code.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class YourGui implements ActionListener {
private JFrame frame;
private JTextField text;
private JButton takeInput;
public YourGui() {
frame = new JFrame();
frame.setLayout(new GridLayout(2, 1));
text = new JTextField();
takeInput = new JButton("Take Input!");
frame.add(text);
frame.add(takeInput);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 100);
takeInput.addActionListener(this);
}
public void show() {
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
int selection =
JOptionPane.showConfirmDialog(frame, "Select Hand", "Select",
JOptionPane.OK_CANCEL_OPTION);
if (selection == JOptionPane.OK_OPTION) {
text.setText("You selected ok");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
YourGui gui = new YourGui();
gui.show();
}
});
}
}
Use the instance of the initiated Class to get access to its public variables. So you should do something like:
GuiMain main=new GuiMain();
...
main.QuesHandtText.setText("someText");
Alternatively if your JTextField is private (though its not) use an instance method that has public access to change its contents - this is the preferred method:
Class A:
class A {
private JTextField tf;
public void setFieldText(String text) {
tf.setText(text);
}
}
Class B:
class B {
A a = new A();
a.setText("hello");
}
In the handler of GuiMain, pass itself (the main JFrame) as an argument to the constructor of GuiSelection:
GuiSelection gui2 = new GuiSelection(this);
And then change the constructor of GuiSelection from
public GuiSelection(){
guiMain = new GuiMain();
...
to
public GuiSelection(GuiMain guiMain){
this.guiMain = guiMain;
...
Also, it seems quite odd that GuiSelection is a subclass of GuiMain. Probably both of these should be direct subclasses of JFrame.
Also, you should wrap everything in the main method within SwingUtilities.invokeLater since everything related to Swing should run on the event-dispatch thread.
Also, you should never use public member variables: it is very un-Java.