How do I change the JLabel text by clicking on my JButton? - java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class myAction extends JFrame implements ActionListener {
myAction() {
super("Lab 4 Question 1");
Container c = getContentPane();
JPanel p = new JPanel();
JPanel p1 = new JPanel();
JButton b = new JButton("Button 1");
JButton b1 = new JButton("Button 2");
JButton b2 = new JButton("Button 3");
Font newFont = new Font("SERIF", Font.BOLD, 16);
b1.setFont(newFont);
b1.addActionListener(this);
JLabel l = new JLabel("Hello.");
p.add(b);
p.add(b1);
p.add(b2);
p1.add(l);
c.add(p);
c.add(p1);
c.setLayout(new FlowLayout());
setSize(300, 300);
show();
}
public static void actionPerformed (ActionEvent e) {
if(e.getSource().equals(b))
{
this.l.setText("Testing");
}
}
public static void main(String[] args) {
myAction output = new myAction();
}
}
How do I make my JButton b1 change the value of my JLabel l ? I am fairly new to programming so I'm sorry for any mistakes that you guys notice! I just need it to change whenever I click the button, I thought I had it right but my symbols can't be found and I'm pretty sure I'm not supposed to pass them into the method :S

Well, you didn't even added ActionListener for your button and make actionPerformed method as non-static (just remove static). This solves a problem:
b.addActionListener(this);
Also, I recommend to use anonymous inner classes instead of implementing ActionListener directly to your class. Like this:
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//Do stuff
}
});
Also, make your JButton's vars. as instance variables (move them outside of constructor).

b is not visible to your actionPerformed method because it is defined in the constructor. You need to move the variable b outside of myAction().
class myAction extends JFrame implements ActionListener {
JButton b;
myAction() {
b = new JButton("Click");

Related

Why launching does it random?

When i run this program sometimes shows me all buttons, but sometimes only 2 or 3 or 4 or 5 or even just 1.. why is that??
I really do not get it. There should always be 6 buttons, but it doesnt show them. Is there any logical reason?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class testnet
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Knjigarna");
frame.setVisible(true);
frame.setSize(800,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JButton button1 = new JButton("Prikazi vse");
panel.add(button1);
button1.addActionListener (new Action1());
JButton button2 = new JButton("Prikazi knjigo");
panel.add(button2);
button2.addActionListener (new Action2());
JButton button3 = new JButton("Dodaj knjigo");
panel.add(button3);
button3.addActionListener (new Action3());
JButton button4 = new JButton("Brisi knjigo");
panel.add(button4);
button4.addActionListener (new Action4());
JButton button5 = new JButton("Uredi knjigo");
panel.add(button5);
button5.addActionListener (new Action5());
JButton button6 = new JButton("Izhod");
panel.add(button6);
button6.addActionListener (new Action6());
}
static class Action1 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame2 = new JFrame("Pikaz vseh knjig");
frame2.setVisible(true);
frame2.setSize(500,800);
JLabel label = new JLabel("Seznam vseh knjig:");
JPanel panel = new JPanel();
JTextField text1=new JTextField("Naslov: ");
JTextField text2=new JTextField("Avtor: ");
frame2.add(panel);
panel.add(label);
panel.add(text1);
panel.add(text2);
}
}
static class Action2 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame3 = new JFrame("Prikaz knjige");
frame3.setVisible(true);
frame3.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige:");
JPanel panel = new JPanel();
frame3.add(panel);
panel.add(label);
}
}
static class Action3 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame4 = new JFrame("Dodajanje knjige");
frame4.setVisible(true);
frame4.setSize(600,300);
JLabel label = new JLabel("Vpisi podtke o knjigi");
JPanel panel = new JPanel();
frame4.add(panel);
panel.add(label);
}
}
static class Action4 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame5 = new JFrame("Brisanje knjige");
frame5.setVisible(true);
frame5.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige, ki jo zelis brisati");
JPanel panel = new JPanel();
frame5.add(panel);
panel.add(label);
}
}
static class Action5 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame6 = new JFrame("Urejanje knjige");
frame6.setVisible(true);
frame6.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige, ki jo zelis urejati");
JPanel panel = new JPanel();
frame6.add(panel);
panel.add(label);
}
}
static class Action6 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
}
try something with layout. JFrame and or remove managed by inside with a content pane. content pane default layout is BorderLayout. so you need to try border layout stuff.
or you can try this code in you main method
frame.setLayout(new FlowLayout());
this will add component by one by one.
for more about layout you can get in here
This is just a fix and not really an explanation of why the problem is occurring.
Call frame.revalidate() after adding all the buttons.
From the Java Docs,
public Component add(Component comp)
This method
changes layout-related information, and therefore, invalidates the
component hierarchy. If the container has already been displayed, the
hierarchy must be validated thereafter in order to display the added
component.

JButton in separate class file

I wrote class which contain simple JFrame and I want to add button from other
class to this JFrame
When I'm trying to write
panel.add(new CancelButton())
I got an error: LoginWindow.java:29: error: no suitable method found for add(CancelButton)
Can you help me with it?
import javax.swing.*;
import java.awt.*;
public class LoginWindow {
public LoginWindow(){
//Login Window
JFrame frame = new JFrame("Movie date base");
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//label
JLabel label = new JLabel("Welcom to Movie Date Base! ");
label.setFont(new Font("Verdana", Font.PLAIN, 18));
//panel
JPanel panel = new JPanel(new GridBagLayout());
JPanel panel1 = new JPanel(new GridBagLayout());
//add panel to the frame
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.getContentPane().add(panel1, BorderLayout.SOUTH);
//Grid layout
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(100,10,10,10);
panel.add(label,c);
}
}
import javax.swing.*;
public class CancelButton {
public CancelButton(){
JButton cancel = new JButton("cancel");
}
}
If you want to treat your CancelButton class as a JButton, then it has to extend JButton.
public class CancelButton extends JButton{
public CancelButton(){
//call the parent-level constructor
super("cancel");
}
}
Note that I've gotten rid of your cancel variable here, since your CancelButton class is-a JButton now. You can use it anywhere you can use a JButton.
However, if instead you want to use the cancel variable in other classes, then you need to create some kind of getter function for it:
public class CancelButton {
JButton cancel;
public CancelButton(){
cancel = new JButton("cancel");
}
public JButton getButton(){
return cancel;
}
}
Then you would call that getButton() function, like this:
panel.add(new CancelButton().getButton())
Which approach you take really depends on exactly what you're trying to do, and how you want to organize your code.
If you'd like to keep it like it is, put the button into an array, and in the jframe, instantiate the cancel class and call from the array.

Java - Button in a button

I'm making a program with 2 JButton, however I have a MyFrame class and the buttons are in a different class named KnoppenPanel. The problem is, when I do this I will get a JButton in a JButton. So I have my 2 buttons and another Button surrounding these. How do I solve this?
MyFrame:
public class MyFrame extends JFrame {
Uithangbord u = new Uithangbord();
KnoppenPanel kp = new KnoppenPanel();
public MyFrame() {
setLayout(new FlowLayout());
add(u);
add(kp);
setSize(280, 180);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class KnoppenPanel extends JButton implements ActionListener {
private JButton b, b2;
private JPanel p1;
Uithangbord bord = new Uithangbord();
public KnoppenPanel() {
p1 = new JPanel();
add(p1);
b = new JButton("Open");
p1.add(b);
b.addActionListener(this);
b2 = new JButton("Gesloten");
p1.add(b2);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
JButton knop = (JButton)(event.getSource());
if (knop == b) {
b.setEnabled(false);
b2.setEnabled(true);
bord.maakOpen();
}
if (knop == b2) {
b2.setEnabled(false);
b.setEnabled(true);
bord.maakGesloten();
}
}
}
You class is extending JButton. Then when you add your JPanel (with 2 JButton) you are adding this in a JButton.
I think what do you want is the KnoppenPanel have only the 2 JButtons then you just need to change:
public class KnoppenPanel extends JButton implements ActionListener {
By
public class KnoppenPanel extends JPanel implements ActionListener {
If you do this change, you can also directly add the JButton in the KnoppenPanel like that:
public KnoppenPanel() {
b = new JButton("Open");
add(b);
b.addActionListener(this);
b2 = new JButton("Gesloten");
add(b2);
b2.addActionListener(this);
}

Frame JButton,ContentPane

i have a problem of my code because all i wanna do is to appear the textfield and in the bottom the button so i used:
setLayout(new FlowLayout()); but i got an error so i decided to change into getContenPane(); but only one will appear in my frame here is my code .
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Awe extends JFrame implements ActionListener {
JTextField c;
JButton b;
Container cont = getContentPane();
public Awe() {
c = new JTextField(15);
b = new JButton("Ok");
c.addActionListener(this);
cont.add(c);
b.addActionListener(this);
cont.add(b);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == c) {
}
}
public static void main(String[] args) {
Awe frm = new Awe();
frm.setTitle("Enter Your Character");
frm.setSize(300, 150);
frm.setVisible(true);
}
}
You are only getting one component in your frame because its default layout is BorderLayout, and its default position is CENTER, and you can only put one component in the center.
Add the text field with cont.add(c, BorderLayout.CENTER);, and add the button with cont.add(b, BorderLayout.SOUTH);, and that should solve the immediate problem.
We can't help you with errors unless you tell us what they are, preferably with a stack trace and the code that produced them.
You need to set a layout for your frame, so the components can be arranjed, instead of being positioned on top of each other.
In your constructor add, for example:
setLayout(new FlowLayout());
And most important, read: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Fully compilable, no errors:
public class Awe extends JFrame implements ActionListener {
JTextField c;
JButton b;
Container cont = getContentPane();
public Awe() {
setLayout(new FlowLayout());
c = new JTextField(15);
b = new JButton("Ok");
c.addActionListener(this);
cont.add(c);
b.addActionListener(this);
cont.add(b);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == c) {
}
}
public static void main(String[] args) {
Awe frm = new Awe();
frm.setTitle("Enter Your Character");
frm.setSize(300, 150);
frm.setVisible(true);
}
}
As #rcook answer, your default layout is BorderLayout which set's all components to CENTER.
Following are the ways you can do it:
Make a JPanel, add your components to JPanel then add JPanel to cont as:
JPanel panel=new JPanel();
panel.add(c);
panel.add(b);
cont.add(panel);
OR you can just use a FLOWLAYOUT as:
setLayout(new FlowLayout());
set this before adding any component to cont.
OR you can just position your components in BorderLayout as:
cont.add(c,BorderLayout.CENTER);
cont.add(b,BorderLayout.SOUTH);
Useful Links
BorderLayout
FlowLayout
the best way i always do is to use setBounds() methods
import java.awt.Dimension;
import javax.swing.*;
public class Frame extends JFrame{
JButton okBtn = new JButton ("OK");
JTextField filed = new JTextField();
public Frame(){
super.setSize(new Dimension(200,200));
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setResizable(false);
super.setLayout(null);
super.getContentPane().add(okBtn);
super.getContentPane().add(filed);
filed.setBounds(10, 10, 150, 30);
okBtn.setBounds(10, 45, 70, 30);
super.validate();
}
public static void main(String[] args) {
new Frame();
}
}
and this the result

how can I make this class an applet

I have the following class which is a simple gui, and I would like to make it an applet so it can be displayed in the browser. I know how to embed the code into an html page(got that done)... but how can make my class an applet? Also, I assuming I don't need a web server just to display the applet in my browser...
package tester1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PanelTest implements ActionListener {
JFrame frame;
JLabel inputLabel;
JLabel outputLabel;
JLabel outputHidden;
JTextField inputText;
JButton button;
JButton clear;
JButton about;
public PanelTest() {
frame = new JFrame("User Name");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3, 2, 10, 10));
//creating first row
JPanel row1 = new JPanel();
inputLabel = new JLabel("Your Name");
inputText = new JTextField(15);
// FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
// row1.setLayout(flow1);
row1.add(inputLabel);
row1.add(inputText);
frame.add(row1);
//creating second row
JPanel row2 = new JPanel();
button = new JButton("Display");
clear = new JButton("Clear");
about = new JButton("About");
button.addActionListener(this);
clear.addActionListener(this);
about.addActionListener(new displayAbout());
row2.add(button);
row2.add(clear);
row2.add(about);
frame.add(row2);
//creating third row
JPanel row3 = new JPanel();
outputLabel = new JLabel("Output:", JLabel.LEFT);
outputHidden = new JLabel("", JLabel.RIGHT);
// FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
// row3.setLayout(flow2);
row3.add(outputLabel);
row3.add(outputHidden);
frame.add(row3);
frame.pack();
frame.setVisible(true);
}
//same method listen for two different events
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("Display")) {
outputHidden.setText(inputText.getText());
}
if(command.equals("Clear")) {
outputHidden.setText("");
inputText.setText("");
}
}
//another way to listen for events
class displayAbout implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Username 1.1 \n by Jorge L. Vazquez");
}
}
public static void main(String[] args) {
PanelTest frameTest = new PanelTest();
}
}
Use a JApplet rather than a JFrame. Make sure you read the relevant Java Tutorial, which covers the applet lifecycle methods like init, start, stop, and destroy.
As a side note, you should not be building your UI outside of the event dispatch thread.
Use a JApplet instead of a JFrame like veer said, but you must also remove frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);, frame.pack();, and frame.setVisible(true);
Also, replace main(String[] args) with init().

Categories