import java.awt.*;
import java.awt.event.*;
public class example3 extends Frame implements ActionListener
{
TextField t;
Button b;
example3()
{
t = new TextField();
t.setBounds(20,20,170,20);
add(t);
b = new Button("click me");
b.setBounds(100,120,80,30);
add(b);
b.addActionListener(this);
setLayout(null);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
t.setText("welcome");
}
public static void main(String args[])
{
example3 obj = new example3();
}
}
Initially I added a text field to the the frame object and I created a button also and added this to frame object but I am not getting correct output.
When I executed this program the text field does not appear. Why?
I got output like this:enter image description here
I would like to suggest only one change in line number 11, that increase the x-coordinate and y-coordinate value of setBounds as per your requirement because your code is working but that field is not at a visible location.
as example:-
your code:- t.setBounds(20,20,170,20);
after changing:- t.setBounds(50,50,170,20);
import java.awt.*;
import java.awt.event.*;
public class example3 extends Frame implements ActionListener
{
TextField t;
Button b;
example3()
{
t = new TextField();
// t.setBounds(20,20,170,20);
//Changes has been done,here.
t.setBounds(50,50,170,20);
add(t);
b = new Button("click me");
b.setBounds(100,120,80,30);
add(b);
b.addActionListener(this);
setLayout(null);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
t.setText("welcome");
}
public static void main(String args[])
{
example3 obj = new example3();
}
}
I hope, it will be helpful.
Related
I am trying to make a JOptionPane display on button click but I keep getting an error saying I'm missing a return statement in my constructor. Any help would be really appreciated. Thanks
This is my code so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class lab9Part1 extends JFrame implements ActionListener {
JButton button = new JButton("Show Message Dialog");
JFrame box = new JFrame();
public lab9Part1() {
super("lab9Part1");
Container c = getContentPane();
button.addActionListener(this);
c.add(button, BorderLayout.SOUTH);
setVisible(true);
setSize(500,500);
}
public static Void main (String [] args){
JFrame frame = new lab9Part1();
}
public void actionPerformed(ActionEvent e){
if (e.getSource()== button) {
JOptionPane.showMessageDialog(box,"hello","This is Cal and this is my first message dialog", JOptionPane.INFORMATION_MESSAGE);
}
}
}
You've used Void (uppercase V) instead of void (lowercase v) in your method declaration of main. It should be:
public static void main (String [] args){
JFrame frame = new lab9Part1();
}
My JLabel isn't being set to all of these text values.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MultipleLables{
static JFrame framey;
static JLabel lbl;
static JButton btn;
public static void GUIWindow () {
framey = new JFrame("Test");
framey.setSize(100, 100);
framey.setLayout(new FlowLayout());
lbl = new JLabel("Example Text");
btn = new JButton("Change Text");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
lbl.setText("First Text");
Thread.sleep(1000);
lbl.setText("Second Text");
Thread.sleep(1000);
lbl.setText("Third Text");
}catch (Exception e) {
//Don't really care if the program dies
}
}
});
framey.add(lbl);
framey.add(btn);
framey.setVisible(true);
}
public static void main(String[] args) {
GUIWindow();
}
}
The output would waits two seconds, then set the value of the JLabel to "Text Three" instead of displaying the three values one after another. I don't see what I'm doing wrong here.
From the question, It is not clear what is the problem you are facing. Please add an example of the code that others can run and test.
If you are trying to see the value of the variables a, b or 'c' in the JLabel , you will need code like
GUIWindow.text.setText(a.toString());
What you are doing now is setting the text "a" and not the value of variable a.
I am not sure what is the data type of the variables a, b or 'c'. If they do have a proper toString() implementation (they probably do as the System.out.println() is giving you the desired output), the above code should work. Else you might need to call the right method on these variables that will give you the desired text.
I was able to fix this issue by creating and running a swing timer. Here is the code, with the fix inside it.
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class MulttipleLables{
static JFrame framey;
static JLabel lbl;
static JButton btn;
static Timer t;
static int i;
public static void GUIWindow () {
framey = new JFrame("Test");
framey.setSize(100, 100);
framey.setLayout(new FlowLayout());
lbl = new JLabel("Example Text");
btn = new JButton("Change Text");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
t = new Timer (1000,new ActionListener() {
public void actionPerformed(ActionEvent e2) {
;
lbl.setText("First Text");
switch (i) {
case 1:
lbl.setText("Second Text");
i++;
break;
case 2:
lbl.setText("Third Text");
i++;
break;
default:
i++;
}
if (i == 3) {
t.stop();
i = 0;
}
}
});
t.start();
}
});
framey.add(lbl);
framey.add(btn);
framey.setVisible(true);
}
public static void main(String[] args) {
GUIWindow();
}
}
I don't have a compiler on me, but I am sure this code should work for what you need.
Like I said the JButton GR is set to the default size (size of window) when I click JButton MN.
When the program is started the JButton GR has the right size (200 by 20), when clicked the menu button appears also at the right size (200 by 20), but when the menu button is clicked the GR JButton is at its default size. When the full size GR JButton is clicked the Menu button reappears with the right size.
I'm using BlueJ (school dose not allow other IDEs).
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
public class MAIN
{
public static void main(String args[])
{
ActionClass actionEvent = new ActionClass();
//Main window
JFrame Program1 = new JFrame("Program1");
Program1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Program1.setPreferredSize(new Dimension(800, 600));
Program1.pack();
Program1.setVisible(true);
//menu button (returns to home Menu)
JButton MN = new JButton("MENU");
MN.setBounds(300, 10, 200, 20);
MN.setVisible(false);
Program1.add (MN);
//MN.setActionCommand("1");
// Enter GRC
JButton GR = new JButton("GRC");
GR.setBounds(300, 40, 200, 20);
GR.setVisible(true);
Program1.add (GR);
//GR.setActionCommand("2");
GR.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent GRH)
{
MN.setVisible(true);
GR.setVisible(false);
}
}
);
MN.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent MNH)
{
MN.setVisible(false);
GR.setVisible(true);
}
}
);
}
}
Like you said the Jbutton GR is ...
JFrame has BorderLayout as default LayoutManager in API,
use Java naming conventions
use LayoutManager instead of NullLayout and wrong way
setVisible(true); should be last code line, because your are in risk that all JComponents are added to already visible container (e.g. mouse over repainting those JComponents)
use Initial Thread
simplest as is possible
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
public class Main {
private JFrame myProgram1 = new JFrame("myProgram1");
private JButton myMN = new JButton("MENU");
private JButton myGR = new JButton("myGRC");
public Main() {
myGR.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent myGRH) {
myMN.setVisible(true);
myGR.setVisible(false);
}
});
myProgram1.add(myMN, BorderLayout.SOUTH);
myMN.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent myMNH) {
myMN.setVisible(false);
myGR.setVisible(true);
}
});
myProgram1.add(myGR, BorderLayout.NORTH);
myProgram1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myProgram1.pack();
myProgram1.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Main();
}
});
}
}
I am currently having trouble with being able to change the choiceDeclaration JLabel. My mindset behind the choiceDeclaration JLabel is to simply display text based on which JButton is clicked.
Here is my current code for the project:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.*;
public class prompt {
public static void main(String []args) {
/* Setting up the JPanel and its necessities for this program */
JFrame choicePrompt = new JFrame("Rock, Paper, Scissors Game");
JPanel choicePanel = new JPanel();
JButton rockButton = new JButton("ROCK");
JButton scissorsButton = new JButton("SCISSORS");
JButton paperButton = new JButton("PAPER");
JLabel choiceDeclaration = new JLabel();
choicePrompt.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
choicePrompt.setResizable(false);
choicePrompt.setSize(300, 300);
choicePrompt.setVisible(true);
choiceDeclaration.setVisible(true);
choicePrompt.add(choicePanel);
choicePanel.add(choiceDeclaration);
choicePanel.add(rockButton);
choicePanel.add(scissorsButton);
choicePanel.add(paperButton);
choiceDeclaration.setVerticalTextPosition(JLabel.TOP);
choiceDeclaration.setHorizontalTextPosition(JLabel.CENTER);
/* ActionListeners for the JButtons */
rockButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
/*I have not placed any code in here because I have not gotten that far*/
}
});
scissorsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
/*I have not placed any code in here because I have not gotten that far*/
}
});
paperButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
/*I have not placed any code in here because I have not gotten that far*/
}
});
}
}
I cannot use a setText method in my ActionListeners as shown below because it conflicts with my main class method a line below the declaration of my class. They are both using String in their parameters.
public static void main(String []args) {
rockButton.addActionListener(new ActionListener() {
setText(String text) {
}
}
}
My concluding thought was to make another class that could use the setText method to change the JLabel on that frame. However, since JLabels cannot be called from one class to another like variables or methods, I am having trouble trying to implement this idea.
Have a separate method for it...
public static void settext() {
setText(String text)
}
then call it in your action listener...
rockButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
settext();
}
});
I hope this is what you meant! :)
I was making a simple text editor where you can set font style,font size, clear all etc. To set font size I added JComboBox and implemented ItemListener. Here is my MainWindow class:
import javax.swing.*;
public class MainWindow extends JFrame{
Editor e = new Editor();
public MainWindow(){
super(".:My Text Editor:.");
getContentPane().add(e);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MainWindow();
}
});
}
}
Here is my Editor class:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Editor extends JPanel{
JPanel optionPanel = new JPanel();
JTextArea editArea = new JTextArea();
JButton boldBtn = new JButton("Bold");
JButton italicBtn = new JButton("Italic");
JButton plainBtn = new JButton("Plain");
JButton clearBtn = new JButton("Clear all");
String [] fontSizes = {"10","11","12","13","14","15","16","17","18","19","20"};
int fontSize;
JComboBox combo = new JComboBox(fontSizes);
public Editor(){
createUI();
addEvents();
}
public void createUI(){
optionPanel.add(boldBtn);
optionPanel.add(italicBtn);
optionPanel.add(plainBtn);
optionPanel.add(combo);
optionPanel.add(clearBtn);
setLayout(new BorderLayout());
add(optionPanel,BorderLayout.NORTH);
add(new JScrollPane(editArea),BorderLayout.CENTER);
setPreferredSize(new Dimension(640,480));
}
public void addEvents(){
boldBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.BOLD,fontSize));
}
});
italicBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.ITALIC,fontSize));
}
});
plainBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
}
});
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
int ind = combo.getSelectedIndex();
System.out.println(ind);
fontSize = Integer.parseInt(fontSizes[ind]);
editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
}
});
clearBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setText("");
}
});
}
}
Now, weird thing what happened is when I put System.out.println(ind); line just to see what index the getSelectedIndex() method returns me. Depending on which item I click, it returns me this:
1
1
0
0
2
2
3
3
Why is this happening? Shouldn't return me just 1 0 2 3? Thanks in advance.
JCombobox fire itemStateChanged twice for SELECTED and DESELECTED that you differentiate with ItemEvent.getStateChanged(). So wrap your code in an if like this:
public void itemStateChanged( ItemEvent event ) {
if( event.getStateChanged() == ItemEvent.SELECTED ) {
// code here
}
}
Whenever you change the selection in a JComboBox, the itemStateChanged event is triggered twice, once for DESELECT of the old selected item and once for SELECT for the new selected item.
If you only want your code to be executed once, just do:
if (e.getStateChange() == ItemEvent.SELECTED) {
...
}
Seems that itemStateChanged is triggered twice. I think that the ItemEvent parameter is not the same each time. Perhaps you should check the event type before doing something.
Sorry I can't check now, but I will do it later if you still need help.