JButton - How to connect? - java

I found this code online and modified it and it stopped working. I'm thinking it has something to do with when I added the Jpanel but what I am doing works best with a JPanel. How do I make it that the events in the action performed if statements work?
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener {
static JPanel panel = new JPanel(new GridLayout(5, 5, 1, 1));
public GUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setSize(100, 100);
//setLocation(100, 100);
//Button 1
JButton button1 = new JButton("1");
button1.addActionListener(this);
panel.add(button1);
//Button 2
JButton button2 = new JButton("2");
button2.addActionListener(this);
panel.add(button2);
//Button 3
JButton button3 = new JButton("3");
button3.addActionListener(this);
panel.add(button3);
//Button 2
JButton button4 = new JButton("4");
button4.addActionListener(this);
panel.add(button4);
//Button 2
JButton button5 = new JButton("5");
button5.addActionListener(this);
panel.add(button5);
//Button 2
JButton button6 = new JButton("6");
button6.addActionListener(this);
panel.add(button6);
//Button 2
JButton button7 = new JButton("7");
button7.addActionListener(this);
panel.add(button7);
//Button 2
JButton button8 = new JButton("8");
button8.addActionListener(this);
panel.add(button8);
//Button 2
JButton button9 = new JButton("9");
button9.addActionListener(this);
panel.add(button9);
panel.setVisible(true);
}
public static void main(String[] args) {
new GUI();
JFrame f = new JFrame("Calc");
f.setContentPane(panel);
f.setSize(1000, 1000);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
ArrayList numbers = new ArrayList();
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("button1")) {
myMethod();
numbers.add(1);
System.out.println("1");
}
if (command.equals("button1")) {
numbers.add(2);
System.out.println("2");
}
}
public void myMethod() {
JOptionPane.showMessageDialog(this, "Hello, World!!!!!");
System.out.println("Hey");
}
}

You need to change the part actionPerformed as:
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("1")) {
myMethod();
numbers.add(1);
System.out.println("1");
}
if (command.equals("2")) {
numbers.add(2);
System.out.println("2");
}
}
Here, when a button is clicked, your e.getActionCommand() will give the constructor string. i.e. "1", "2" ,"3" and so on
reference

I put comments in code, but the first thing you should do is to read official tutorials. How to use Buttons
public class GUI /*extends JFrame implements ActionListener*/ {
//don't extend JFrame is you don't have too neither implement ActionListener in top-container classes that breaks single responsability principle
private JPanel panel = new JPanel(new GridLayout(5, 5, 1, 1)); // why static??
public JPanel getPanel(){
return panel;
}
public GUI() {
//i use anonymous classes for this, then you don't have to use if-else
//Button 1
JButton button1 = new JButton("1");
button1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
myMethod();
numbers.add(1);
System.out.println("1");
}
});
panel.add(button1);
//Button 2
JButton button2 = new JButton("2");
button2.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
//put here logic for button2
}
});
panel.add(button2);
//and goo on with other buttons
//panel.setVisible(true); you don't need to call this!!
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI gui = new GUI();
frame.add(gui.getPanel());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private List<Integer> numbers = new ArrayList<>();//use generics!
public void myMethod() {
JOptionPane.showMessageDialog(this, "Hello, World!!!!!");
System.out.println("Hey");
}
}

Related

error: illegal start of expression 1 error

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.lang.*;
import java.util.*;
public class Life {
public static void main (String[] args){
JFrame frame = new JFrame("Interest Calculator");
frame.setVisible(true);
frame.setSize(300,100);
frame.setBackground(Color.magenta);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Simple Interest");
panel.add(button);
button.addActionListener (new Action1());
JButton button2 = new JButton("Compound Interest");
panel.add(button2);
button2.addActionListener (new Action2());
}
static class Action1 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Simple");
frame2.setVisible(true);
frame2.setSize(300,100);
JPanel panel = new JPanel();
frame2.add(panel);
JButton button = new JButton("Ordinary");
panel.add(button);
button.addActionListener (new Ordinary());
JButton button2 = new JButton("Exact");
panel.add(button2);
button2.addActionListener (new Exact());
}
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame3 = new JFrame("Compound Interest");
frame3.setVisible(true);
frame3.setSize(300,100);
JPanel panel = new JPanel();
frame3.add(panel);
JButton button = new JButton("Compounded");
panel.add(button);
JButton button2 = new JButton("Continously");
panel.add(button2);
}
}
static class Ordinary implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame4 = new JFrame("Simple");
frame4.setVisible(true);
frame4.setSize(300,100);
JPanel panel = new JPanel();
frame4.add(panel);
JButton button = new JButton("Approximate");
panel.add(button);
button.addActionListener (new Approximate());
JButton button2 = new JButton("Actual");
panel.add(button2);
}
}
static class Exact implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame5 = new JFrame("Exact");
frame5.setVisible(true);
frame5.setSize(300,100);
JPanel panel = new JPanel();
frame5.add(panel);
JButton button = new JButton("Approximate");
panel.add(button);
JButton button2 = new JButton("Actual");
panel.add(button2);
}
}
static class Approximate implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame6 = new JFrame("Simple");
frame6.setVisible(true);
frame6.setSize(300,100);
JPanel panel = new JPanel();
frame6.add(panel);
frame6.setVisible(true);
frame6.setSize(300,300);
frame6.setLayout(null);
frame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField P = new JTextField();
panel.add(P);
P.setText("Enter Principal");
JTextField r = new JTextField();
panel.add(r);
r.setText("Enter Rate");
JTextField t = new JTextField();
panel.add(t);
t.setText("Enter Year");
JButton button = new JButton("Calculate");
panel.add(button);
button.addActionListener (new button());
static class button implements ActionListener {
public void actionPerformed (ActionEvent e) {
float P,r,t,result ;
P = Float.parseFloat(P.getText());
r = Float.parseFloat(r.getText());
t = Float.parseFloat(t.getText());
result = P*r/100*t/360;
button.setText(String.valueof(result));
}
}
}
}
}
So this is my code to create a compound calculator but I have a problem it is the illegal start of expression.please help me for my project.
Below is partially fixed part of the code which causes problems. Next time include full error code, and try to show only relevant part of the code.
ActionListener listener = new ActionListener() {
public void actionPerformed (ActionEvent e) {
float P,r,t,result ;
//P = Float.parseFloat(P.getText()); // P makes no sens here - its float !!! and uninitialized
//r = Float.parseFloat(r.getText()); // same for r
//t = Float.parseFloat(t.getText()); // same for t
result = P*r/100*t/360;
button.setText(String.valueOf(result));
}
};
button.addActionListener (listener);

Issue with Jframe not showing anything until minimized

I am having a weird issue with my initial JFrame login. When I run the program it will only pull up the cancel JButton at first. Then if I minimize the frame it shows everything like it should. Is there a reason for this? If so, how do I fix it?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class example
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Login");
frame.setVisible(true);
frame.setSize(350,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("User Id: ");
JLabel label2 = new JLabel("Password: ");
JTextField txt2 = new JTextField(20);
JButton button = new JButton("login");
JTextField txt = new JTextField(20);
frame.add(label1);
frame.add(txt);
frame.add(label2);
frame.add(txt2);
String user = txt.getText();
String password = txt2.getText();
JButton button2 = new JButton("Cancel");
frame.add(button);
frame.add(button2);
button2.addActionListener (new Action2());
frame.setVisible(true);
frame.setLayout(new FlowLayout());
button.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
if ("abc".equals(txt.getText()) && "123".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Student");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
if ("def".equals(txt.getText()) && "456".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Instructor");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
if ("ghi".equals(txt.getText()) && "789".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Teacher");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
else
{
System.out.println("Invalid Password");
}
}
});
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
}
The setVisible(true) should be the last method call.
Also, it is recommended to run swing applications in their own thread, as specified by the Java spec:
Concurrency in Swing > Initial Threads: docs.oracle.com
So you should ideally run the Swing application as so:
//example from the referenced java documentation
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
For more information about this which might be clearer:
Will the real Swing Single Threading Rule please stand up?
The createAndShowGUI() method is not required, it is an arbitrary method name and its function is to call a method that basically does everything you've already written for your Swing application (with the modification of setVisible(true) invoked at the very end instead.)
So you should do something like this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class example
{
public static void main (String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
//you can alternatively replace 'createAndShowGUI()' with 'new example()'
}
});
}
public static void createAndShowGUI() {
new example();
}
public example() {
JFrame frame = new JFrame("Login");
//frame.setVisible(true);
frame.setSize(350,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("User Id: ");
JLabel label2 = new JLabel("Password: ");
JTextField txt2 = new JTextField(20);
JButton button = new JButton("login");
JTextField txt = new JTextField(20);
frame.add(label1);
frame.add(txt);
frame.add(label2);
frame.add(txt2);
String user = txt.getText();
String password = txt2.getText();
JButton button2 = new JButton("Cancel");
frame.add(button);
frame.add(button2);
button2.addActionListener (new Action2());
//frame.setVisible(true);
frame.setLayout(new FlowLayout());
button.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
if ("abc".equals(txt.getText()) && "123".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Student");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
if ("def".equals(txt.getText()) && "456".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Instructor");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
if ("ghi".equals(txt.getText()) && "789".equals(txt2.getText()))
{
JFrame frame2 = new JFrame("Teacher");
frame2.setVisible(true);
frame2.setSize(200,200);
frame.setVisible(false);
}
else
{
System.out.println("Invalid Password");
}
}
});
frame.setVisible(true);
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
}
You should as well edit the button.addActionListener(new ActionListener()
with the appropriate modifications as I have done in the public example() constructor (move the setVisible(boolean) method calls) and also add EXIT_ON_CLOSE for the new JFrames you have created here.

addActionListener not working

I followed a tutorial on how to do this - Here's the code I used:
package soundboard;
import javax.swing.*;
import java.awt.event.*;
public class Soundboard {
JButton Button1;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
Button1 = new JButton("1");
Button1.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(Button1);
frame.add(mainsPanel);
frame.setSize(183,245);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent event){
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}
}
The code seems to not be working. Could anyone explain why?
The JPanel is used as a background. The problem is in Button1.addActionListener(this); , as it says that "this" is not convertible to ActionListener or something like so.
If you want to add your class as an Onclicklistener:
Button1.addActionListener(this);
then your class must implement the appropriate interface ActionListener like this:
public class Soundboard implements ActionListener{
//...
#Override
public void actionPerformed(ActionEvent e){
//...
}
}
EDIT
If you have multiple buttons, that need separated implementation, you could f.e. use anonymous classes:
mybutton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
//does something, that probably interests only mybutton
//declare mybutton as **final** if you must use it
}
});
You need to implement the ActionListener interface if you want to override the actionPerformed method:
public class Soundboard implements ActionListener {
You can only add ActionListeners to a Component with addActionListener().
Your class has to implement ActionListener e.g.
public class Soundboard implements ActionListener {
I got it working. Here's how I implemented it, the other buttons don't do a thing quite yet.
All the code is in a class called Soundboard, which implements ActionListener, while javax.swing* and
java.awt.event* are also imported.
JButton loadButton;
JButton clearButton;
JButton Button1;
JButton Button2;
JButton Button3;
JButton Button4;
JPanel mainsPanel;
int times;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
loadButton = new JButton("Load...");
loadButton.setSize(80, 30);
loadButton.setLocation(4, 4);
clearButton = new JButton("Clear");
clearButton.setSize(80, 30);
clearButton.setLocation(92, 4);
Button1 = new JButton("1");
Button1.setSize(80, 80);
Button1.setLocation(4, 45);
Button2 = new JButton("2");
Button2.setSize(80, 80);
Button2.setLocation(92, 45);
Button3 = new JButton("3");
Button3.setSize(80, 80);
Button3.setLocation(4, 133);
Button4 = new JButton("4");
Button4.setSize(80, 80);
Button4.setLocation(92, 133);
loadButton.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(loadButton);
frame.add(clearButton);
frame.add(Button1);
frame.add(Button2);
frame.add(Button3);
frame.add(Button4);
frame.add(mainsPanel);
frame.setSize(183,245);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
#Override
public void actionPerformed(ActionEvent event){
times += 1;
System.out.println("Test successful - this was the #"
+ times + " press");
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}

Generating non-repeating random methods in GUI

I've been spending so long looking at my computer monitor because I really don't know what to do to prevent the frames on my program on appearing simultaneously when I click the Start button.
Here's my main class:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;
public class PopQuizDemo {
public static void main (String args[]){
PopQuizDemo();
}
public static void PopQuizDemo(){
final SimpleFrame frame = new SimpleFrame();
final JPanel main = new JPanel();
main.setSize(400,75);
main.setLayout(new GridLayout(3,1));
frame.add(main);
JLabel l1 = new JLabel("Welcome to POP Quiz!");
main.add(l1);
JLabel l2 = new JLabel("Enter your name:");
main.add(l2);
final JTextField name = new JTextField ();
main.add(name);
final JPanel panel = new JPanel();
panel.setSize(400,50);
panel.setLocation(0,225);
frame.add(panel);
JButton start = new JButton ("Start");
panel.add(start);
frame.setVisible(true);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
randomize();
}
});
}
public static void randomize(){
Questions q = new Questions();
int a=0;
Random randnum = new Random (System.currentTimeMillis());
java.util.HashSet<Integer> myset = new java.util.HashSet<>();
for (int count = 1; count <= 3; count++){
while (true) {
a = randnum.nextInt (3);
if(!myset.contains(a)) { myset.add(new Integer(a)); break;}
}
if(a==0){
q.one();
}
else if(a==1){
q.two();
}
else if(a==2){
q.three();
}
else{
break;
}
}
}
}
And here is the class Question where I get the methods one(), two(), and three():
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Questions {
public static void one(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("In computers, what is the smallest and basic unit of");
JLabel qu2 = new JLabel();
qu2.setText("information storage?");
p1.add(qu1);
p1.add(qu2);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Bit");
p2.add(a);
JButton b = new JButton("b. Byte");
p2.add(b);
JButton c = new JButton("c. Data");
p2.add(c);
JButton d = new JButton("d. Newton");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
public static void two(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("Machine language is also known as __________.");
p1.add(qu1);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Low level language");
p2.add(a);
JButton b = new JButton("b. Assembly language");
p2.add(b);
JButton c = new JButton("c. High level language");
p2.add(c);
JButton d = new JButton("d. Source code");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
public static void three(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("What is the shortcut key of printing a document for");
JLabel qu2 = new JLabel();
qu2.setText("computers using Windows?");
p1.add(qu1);
p1.add(qu2);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Ctrl + P");
p2.add(a);
JButton b = new JButton("b. Shift + P");
p2.add(b);
JButton c = new JButton("c. Shift + PP");
p2.add(c);
JButton d = new JButton("d. Alt + P");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
}
The only problem here is that the when I call the method randomize() in the action listener in the Start button, it shows all the frames. Yes, they are not repeating but it shows simultaneously. I don't know where the problem is. Is it with the method randomize, the looping, the questions? Can someone help me? Please? Big thanks.
PS:
This is the class SimpleFrame
import javax.swing.JFrame;
public class SimpleFrame extends JFrame{
public SimpleFrame(){
setSize(400,300);
setTitle("Pop Quiz!");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setResizable(false);
}
}
You can use model dialog to show one after another using a for loop as show below:
public static void main(String[] args) {
JFrame m = new JFrame("Hello");
m.setSize(200,200);
m.setVisible(true);
for(int i=0;i<3;i++) {
JDialog dlg = new JDialog(m,"Dialog",true);
dlg.setSize(100,100);
dlg.show();
}
}

Button variable cannot be referenced from a static context error

How do I prevent this error from happening with my current code? I apologize for my logic being very amateur.
public class jButExmp {
JFrame exmpFrame;
JButton Button1, Button2, Button3;
public jButExmp () {
exmpFrame.setLayout(new FlowLayout());
exmpFrame.setSize(250,150);
exmpFrame.setVisible(true);
exmpFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
exmpFrame.add(Button1);
exmpFrame.add(Button2);
exmpFrame.add(Button3);
}
public static void main(String[] args) {
exmpFrame = new JFrame ("Example Frame");
Button1 = new JButton ("1");
Button2 = new JButton ("2");
Button3 = new JButton ("3");
Button1.setSize(80, 30); //set size of button
Button1.setLocation(0,0);
Button1.setEnabled(true);
Button2.setSize(80,30);
Button2.setLocation(90, 0);
Button2.setEnabled(false);
}
}
import java.awt.FlowLayout;
import javax.swing.*;
public class ExmpFile {
JFrame exmpFrame;
JButton Button1, Button2, Button3;
public ExmpFile() {
exmpFrame = new JFrame ("Example Frame");
Button1 = new JButton ("1");
Button2 = new JButton ("2");
Button3 = new JButton ("3");
Button2.setEnabled(false);
exmpFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
// better to pack() to the size of content.. BNI
exmpFrame.setSize(250,150);
exmpFrame.setVisible(true);
exmpFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
exmpFrame.add(Button1);
exmpFrame.add(Button2);
exmpFrame.add(Button3);
exmpFrame.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new ExmpFile();
}
};
SwingUtilities.invokeLater(r);
}
}
In your code you are trying to access not static variables in static main method, so it will compilation errors.
Try this
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class jButExmp {
JFrame exmpFrame = new JFrame("Example Frame");
JButton button1, button2, button3;
public JButton getButton1() {
return button1;
}
public void setButton1(JButton button1) {
this.button1 = button1;
}
public JButton getButton2() {
return button2;
}
public void setButton2(JButton button2) {
this.button2 = button2;
}
public JButton getButton3() {
return button3;
}
public void setButton3(JButton button3) {
this.button3 = button3;
}
public jButExmp() {
exmpFrame = new JFrame("Example Frame");
exmpFrame.setLayout(new FlowLayout());
exmpFrame.setSize(250, 150);
exmpFrame.setVisible(true);
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
exmpFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
exmpFrame.add(button1);
exmpFrame.add(button2);
exmpFrame.add(button3);
}
public static void main(String[] args) {
jButExmp jButExmpRef = new jButExmp();
jButExmpRef.getButton1().setSize(80, 30); // set size of button
jButExmpRef.getButton1().setLocation(0, 0);
jButExmpRef.getButton1().setEnabled(true);
jButExmpRef.getButton2().setSize(80, 30);
jButExmpRef.getButton2().setLocation(90, 0);
jButExmpRef.getButton2().setEnabled(false);
}
}
This woud be the more idiomatic way to do it, though this code has some other problems, including the fact that you're not setting up button3 the way you are with button1 and button2, and the fact that you are setting the locations of the buttons while using the default FlowLayout (which doesn't support setting positions).
import javax.swing.JButton;
import javax.swing.JFrame;
public class JButExmp extends JFrame {
JButton button1;
JButton button2;
JButton button3;
public JButExmp (String title) {
super(title);
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
add(button1);
add(button2);
add(button3);
button1.setSize(80, 30); //set size of button
button1.setLocation(0,0);
button1.setEnabled(true);
button2.setSize(80,30);
button2.setLocation(90, 0);
button2.setEnabled(false);
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setSize(250,150);
setVisible(true);
}
public static void main(String[] args) {
JButExmp jButExmp = new JButExmp("Example Frame");
}
}

Categories